//<--------------服务端代码--------------------> public class socketreadlister implements runnable { private final int tcpport=9999; private serversocket serversocket; @override public void run() { try { serversocket = new serversocket(this.tcpport); while(true){ socket socket = serversocket.accept(); //socket.setsotimeout(5*1000);//设置读取数据超时时间为5s new thread(new socketreadthread(socket)).start(); } }catch (exception e){ e.printstacktrace(); } } public static void main(string[] args) throws exception{ new thread(new socketreadlister()).start(); } } public class socketreadthread implements runnable { private socket socket; public socketreadthread(socket socket) { this.socket = socket; } @override public void run() { byte[] data = new byte[1024]; try { inputstream is=socket.getinputstream(); int length=0; int num=is.available(); while((length = is.read(data)) != -1){ string result = new string(data); system.out.println("数据available:"+num); system.out.println("数据:"+result); system.out.println("length:" + length); } system.out.print("结束数据读取:"+length); }catch (sockettimeoutexception sockettimeoutexception){ try { thread.sleep(2*1000); }catch (exception e) { e.printstacktrace(); } run(); } catch (exception e){ e.printstacktrace(); try { socket.close(); }catch (ioexception io){ io.printstacktrace(); } } } }
//<---------------------客户端代码----------------------------> public class socketclient implements runnable { private final int tcpport=9999; private socket socket; @override public void run() { string msg = "ab23567787hdhfhhfy"; byte[] bytemsg = msg.getbytes(); try { socket = new socket("127.0.0.1", 9999); outputstream out = socket.getoutputstream(); inputstream inputstream=socket.getinputstream(); out.write(bytemsg); thread.sleep(10*1000); char[] chars=msg.tochararray(); string str=""; /*out.flush();*/ for(int i=0;i<msg.length();i++) { str=chars[i]+"-"+i; out.write(str.getbytes()); thread.sleep(1*1000); } byte[] bytes=new byte[8]; while(true) { if(inputstream.available()>0) { if(inputstream.read(bytes)!=-1) { system.out.println(new string(bytes)); } } thread.sleep(10*1000); } } catch (exception e) { e.printstacktrace(); try { socket.close(); } catch (ioexception e2) { e2.printstacktrace(); } } } public static void main(string[] args) { new thread(new socketclient()).start(); } }
while(true){ if(is.available()>0){ is.read(data); } }
if (nrecv < nrecvneed){ int nsize = 0; wsabuf=new byte[nrecvneed-nrecv]; int readcount = 0; // 已经成功读取的字节的个数 try { while (readcount < wsabuf.length) { //thread.sleep(100);//读取之前先将线程休眠,避免循环时,程序占用cpu过高 try { availablenum=inputstream.available(); if(availablenum>0){ readcount += inputstream.read(wsabuf, readcount, (wsabuf.length - readcount));//避免数据读取不完整 } }catch (sockettimeoutexception timeout){ system.out.println("读取超时,线程执行休眠操作,2秒后再读取"); thread.sleep(2*1000); } } }catch (exception e){ system.out.println("读取数据异常"); e.printstacktrace(); close();//关闭socket连接 break; } nsize=wsabuf.length; nrecv+=nsize; }
如对本文有疑问, 点击进行留言回复!!
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论