android下tcp之client测试

		new Thread(new Runnable() {
			@Override
			public void run() {
				int count = 0;
				while (true) {
					try {
						Socket socket = new Socket("192.168.10.101", 60000);
						OutputStream os = socket.getOutputStream();
						String string = "tcp client count = "+count++;
						os.write(string.getBytes());
						os.flush();
						Thread.sleep(100);
						os.close();
						socket.close();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} 
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();

注意设置权限:

结果:

【Receive from 192.168.10.110 : 46736】:
tcp client count = 0
【Receive from 192.168.10.110 : 59447】:
tcp client count = 1
【Receive from 192.168.10.110 : 45333】:
tcp client count = 2
【Receive from 192.168.10.110 : 40047】:
tcp client count = 3
【Receive from 192.168.10.110 : 32905】:
tcp client count = 4
【Receive from 192.168.10.110 : 59586】:
tcp client count = 5
【Receive from 192.168.10.110 : 35389】:
tcp client count = 6
【Receive from 192.168.10.110 : 46435】:
tcp client count = 7
【Receive from 192.168.10.110 : 48257】:
tcp client count = 8



你可能感兴趣的:(Android)