当前位置: 代码迷 >> Android >> Android网络连接三——基于Socket
  详细解决方案

Android网络连接三——基于Socket

热度:81   发布时间:2016-04-28 03:55:56.0
Android网络连接3——基于Socket

Andoird中能够使用java中封装的Socket通信机制。

服务端:

?

public class SimpleServer {	public static void main(String[] args) throws IOException {		// 创建一个ServerSocket,用于监听客户端Socket的连接请求		ServerSocket ss = new ServerSocket(30000);		// 采用循环不断接受来自客户端的请求		while (true) {			// 每当接受到客户端Socket的请求,服务器端也对应产生一个Socket			Socket s = ss.accept();			OutputStream os = s.getOutputStream();			os.write("Hello , this is Server !\n".getBytes("utf-8"));			// 关闭输出流,关闭Socket			os.close();			s.close();		}	}}

? Android客户端的简单实现 :

?

?

public class SimpleClient extends Activity {	EditText show;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		show = (EditText) findViewById(R.id.show);		try {			Socket socket = new Socket("192.168.1.120", 30000);			BufferedReader br = new BufferedReader(new InputStreamReader(					socket.getInputStream()));			String line = br.readLine();			show.setText("来自服务器的数据:" + line);			br.close();			socket.close();		} catch (IOException e) {			e.printStackTrace();		}	}}

?==========================

?

加入线程后:

服务端:

public class MyServer {	public static void main(String[] args) throws IOException {		ServerSocket ss = new ServerSocket(30000);		while (true) {			Socket s = ss.accept();			// 每当客户端连接后启动一条ServerThread线程为该客户端服务			new Thread(new ServerThread(s)).start();		}	}}public class ServerThread implements Runnable {	// 定义当前线程所处理的Socket	Socket s = null;	// 该线程所处理的Socket所对应的输入流	BufferedReader br = null;	public ServerThread(Socket s) throws IOException {		this.s = s;		// 初始化该Socket对应的输入流		br = new BufferedReader(new InputStreamReader(s.getInputStream(),				"utf-8")); // ②	}	public void run() {		try {			String content = null;			while ((content = br.readLine()) != null) {				OutputStream os = s.getOutputStream();				os.write((content + "\n").getBytes("utf-8"));			}		} catch (IOException e) {			e.printStackTrace();		}	}}

?

客户端代码:

public class MultiThreadClient extends Activity {	EditText input, show;// 定义界面上的两个文本框	Button send;// 定义界面上的一个按钮	OutputStream os;	Handler handler;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		input = (EditText) findViewById(R.id.input);		send = (Button) findViewById(R.id.send);		show = (EditText) findViewById(R.id.show);		Socket s;		handler = new Handler() {			@Override			public void handleMessage(Message msg) {				// 如果消息来自于子线程				if (msg.what == 0x123) {					// 将读取的内容追加显示在文本框中					show.append("\n" + msg.obj.toString());				}			}		};		try {			s = new Socket("192.168.1.120", 30000);			// 客户端启动ClientThread线程不断读取来自服务器的数据			new Thread(new ClientThread(s, handler)).start(); // ①			os = s.getOutputStream();		} catch (Exception e) {			e.printStackTrace();		}		send.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				try {					os.write((input.getText().toString() + "\r\n")							.getBytes("utf-8"));					// 清空input文本框					input.setText("");				} catch (Exception e) {					e.printStackTrace();				}			}		});	}}

?客户端处理线程

?

public class ClientThread implements Runnable {	private Socket s;	private Handler handler;	BufferedReader br = null;	public ClientThread(Socket s, Handler handler) throws IOException {		this.s = s;		this.handler = handler;		br = new BufferedReader(new InputStreamReader(s.getInputStream()));	}	public void run() {		try {			String content = null;			// 不断读取Socket输入流中的内容。			while ((content = br.readLine()) != null) {				// 每当读到来自服务器的数据之后,发送消息通知程序界面显示该数据				Message msg = new Message();				msg.what = 0x123;				msg.obj = content;				handler.sendMessage(msg);			}		} catch (Exception e) {			e.printStackTrace();		}	}}

?

  相关解决方案