packagecom.huhai.demo.socket;importjava.io.BufferedR"> packagecom.huhai.demo.socket;importjava.io.BufferedR" />
欢迎来到天天文库
浏览记录
ID:37915942
大小:39.00 KB
页数:4页
时间:2019-06-02
《java sockect客户端,服务端代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Javasockect服务端代码packagesockecttest;//服务端packagecom.huhai.demo.socket;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.concurre
2、nt.ExecutorService;importjava.util.concurrent.Executors;publicclassSocketServer{privateServerSocketserverSocket;privateSocketsocket;privateintport=8006;privateExecutorServiceexecuteService=Executors.newFixedThreadPool(10);publicvoidSocketServerss(){try{serverSocket=newServerSocket(po
3、rt);//serverSocket=newServerSocket(port,10,InetAddress.getByName("192.168.1.102"));System.out.println("SocketServerhavebeenstarted.["+serverSocket+"]");while(true){socket=serverSocket.accept();executeService.submit(newRunnable(){publicvoidrun(){try{BufferedReaderin=newBufferedReader(
4、newInputStreamReader(socket.getInputStream()));PrintWriterout=newPrintWriter(socket.getOutputStream(),true);Stringline=in.readLine();System.out.println(line);Stringstr="ok";out.println(str);out.close();in.close();}catch(Exceptione){e.printStackTrace();}finally{try{socket.close();}cat
5、ch(IOExceptione){System.out.println("closesocketerror.");e.printStackTrace();}}}});}}catch(Exceptione){e.printStackTrace();}finally{try{if(null!=serverSocket){serverSocket.close();System.out.println("serverSocketclose");}}catch(Exceptione){e.printStackTrace();}}}publicstaticvoidmain(
6、String[]args){SocketServers=newSocketServer();s.SocketServerss();}}客户端代码packagesockecttest;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.Socket;publicclassSocketClient{privateSocketsocket;privateintport=1400;privateStringhostIp=
7、"192.168.1.12";publicSocketClient(){try{Stringmessage=null;BufferedReaderin=null;PrintWriterout=null;BufferedReaderline=null;while(!SocketCommand.CONST_QUIT.equals(message)){socket=newSocket(hostIp,port);in=newBufferedReader(newInputStreamReader(socket.getInputStream()));out=newPrint
8、Writer(socke
此文档下载收益归作者所有