欢迎来到天天文库
浏览记录
ID:17006750
大小:23.50 KB
页数:4页
时间:2018-08-26
《socket长连接客户端服务端》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packagecom.socket;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.io.Reader;importjava.net.Socket;importjava.nio.CharBuffer;/***Socket长连接客户端*/publicclassClient03{privateStringhost="127.0.0.1";privateint
2、port=5055;/***数据发送线程*/classSendThreadimplementsRunnable{privateSocketsocket;publicSendThread(Socketsocket){this.socket=socket;}publicvoidrun(){while(true){try{PrintWriterpw=newPrintWriter(newOutputStreamWriter(socket.getOutputStream()));pw.write("thisisclient");pw.flush();Thread.sl
3、eep(2000);}catch(Exceptione){e.printStackTrace();}}}}/***数据接收线程otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAf
4、terthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand*/classReceiveThreadimplementsRunnable{privateSocketsocket;
5、publicReceiveThread(Socketsocket){this.socket=socket;}publicvoidrun(){while(true){try{Readerreader=newInputStreamReader(socket.getInputStream());CharBuffercharBuffer=CharBuffer.allocate(8192);@SuppressWarnings("unused")intcharIndex=-1;while((charIndex=reader.read(charBuffer))!=-1){
6、charBuffer.flip();System.out.println("client-->"+charBuffer.toString());}}catch(IOExceptione){e.printStackTrace();}}}}privatevoidstart(){try{Socketsocket=newSocket(host,port);//创建SocketnewThread(newSendThread(socket)).start();//启动读线程newThread(newReceiveThread(socket)).start();//启动收
7、线程}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){newClient03().start();}}otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrgani
8、zation,SecretaryoftheSpeci
此文档下载收益归作者所有