android下tcp之client测试

来源:互联网 发布:9860计算器怎么编程 编辑:程序博客网 时间:2024/06/02 05:31
new Thread(new Runnable() {@Overridepublic 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();

注意设置权限:

<uses-permission android:name="android.permission.INTERNET"/>
结果:

【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



0 0
原创粉丝点击