美文网首页
2018-09-08

2018-09-08

作者: 张威先森 | 来源:发表于2018-09-08 14:18 被阅读0次

一个服务端对应多个客户端

publicstaticvoidmain(String[] args)throwsIOException{// TODO Auto-generated method stubScanner scan=newScanner(System.in);//创建服务端 设置端口ServerSocket ss=newServerSocket(8088);        System.out.println("服务端已启动...");while(true){//socket可以获取网络输入流  - 得到客户端的消息Socket socket=ss.accept();            ServerThread st=newServerThread(socket);            Thread t=newThread(st);            t.start();        }    }

publicclassServerThreadimplementsRunnable{privateSocket socket;publicServerThread(Socket socket){this.socket = socket;    }@Overridepublicvoidrun(){// TODO Auto-generated method stubstart();    }publicvoidstart(){        Scanner scan=newScanner(System.in);        InputStream is;try{            is = socket.getInputStream();            InputStreamReader isr=newInputStreamReader(is);char[] chff=newchar[20];intlen;            String message="";while((len=isr.read(chff))!=-1){                message=message+newString(chff,0,len);            }if("exit".equals(message)){                socket.close();return;            }//getInetAddress() 获取客户端IPSystem.out.println("客户端"+socket.getInetAddress()+":"+message);//对客户端做出响应OutputStream os=socket.getOutputStream();            OutputStreamWriter osw=newOutputStreamWriter(os);            System.out.println("回复客户端:");            String mse=scan.next();//回复给客户端的内容osw.write(mse);            osw.flush();            socket.shutdownOutput();        }catch(IOException e) {// TODO Auto-generated catch blocke.printStackTrace();        }    }}

publicclassClientA{publicstaticvoidmain(String[] args)throwsUnknownHostException, IOException{// TODO Auto-generated method stubScanner scan=newScanner(System.in);intport =8088;        String ip="localhost";                System.out.println("客户端A已连接...");while(true){            Socket socket=newSocket(ip,port);            System.out.println("请发送消息:");            String message=scan.next();//客户端发送给服务端OutputStream os=socket.getOutputStream();            OutputStreamWriter osw =newOutputStreamWriter(os);            osw.write(message);            osw.flush();            socket.shutdownOutput();//本次输出流结束if("exit".equals(message)){                socket.close();break;            }//接收服务端响应的信息InputStream is=socket.getInputStream();            InputStreamReader isr=newInputStreamReader(is);char[] chff=newchar[20];intlen;            String mes="";while((len=isr.read(chff))!=-1){                mes=mes+newString(chff,0,len);            }            System.out.println("服务端回复:"+mes);        }    }}

publicclassClientB{publicstaticvoidmain(String[] args)throwsUnknownHostException, IOException{// TODO Auto-generated method stubScanner scan=newScanner(System.in);intport =8088;        String ip="localhost";                System.out.println("客户端B已连接...");while(true){            Socket socket=newSocket(ip,port);            System.out.println("请发送消息:");            String message=scan.next();//客户端发送给服务端OutputStream os=socket.getOutputStream();            OutputStreamWriter osw =newOutputStreamWriter(os);            osw.write(message);            osw.flush();            socket.shutdownOutput();//本次输出流结束if("exit".equals(message)){                socket.close();break;            }//接收服务端响应的信息InputStream is=socket.getInputStream();            InputStreamReader isr=newInputStreamReader(is);char[] chff=newchar[20];intlen;            String mes="";while((len=isr.read(chff))!=-1){                mes=mes+newString(chff,0,len);            }            System.out.println("服务端回复:"+mes);        }    }}

作者:陈先森chy

链接:https://www.jianshu.com/p/8a9025a62efd

來源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

相关文章

网友评论

      本文标题:2018-09-08

      本文链接:https://www.haomeiwen.com/subject/llnkgftx.html