Java Socket多线程编程示例

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

客户端代码如下:

    import java.io.*;  
    import java.net.*;  
    import java.util.Scanner;  
      
    public class SimpleChatClient {  
        BufferedReader reader;  
        PrintWriter writer;  
        Socket sock;  
          
        public void go(){  
            System.out.println("客户端启动!");  
            setUpNetworking();  
            //发送数据到服务器  
            Scanner scanner=new Scanner(System.in);  
            String sendString=scanner.nextLine();  
            while(sendString!=null&&!sendString.equals("byebye")){  
                writer.println(sendString);  
                writer.flush();  
                sendString=scanner.nextLine();  
            }  
            scanner.close();  
            System.out.println("客户端退出!");  
        }  
          
        public void setUpNetworking(){  
            try {  
                sock=new Socket("127.0.0.1",5000);  
                InputStreamReader streamReader=new InputStreamReader(sock.getInputStream());  
                reader=new BufferedReader(streamReader);  
                writer=new PrintWriter(sock.getOutputStream());  
                System.out.println("networking established");  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
              
          
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
            SimpleChatClient client=new SimpleChatClient();  
            client.go();  
        }  
      
    }  

服务器端代码如下:
    import java.io.*;  
    import java.net.*;  
    public class SimpleChatServer {   
        //内部类  
        public class ClientHandler implements Runnable{  
            BufferedReader reader;  
            Socket sock;  
          
            public ClientHandler(Socket clientSocket){  
                sock=clientSocket;  
                InputStreamReader isReader;  
                try {     
                    isReader = new InputStreamReader(sock.getInputStream());  
                    reader=new BufferedReader(isReader);  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                  
            }     
            @Override  
            public void run() {  
                // TODO Auto-generated method stub  
                String messageString;  
                try {  
                    while((messageString=reader.readLine())!=null){  
                        System.out.println("read "+messageString);  
                    }  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block            
                        System.out.println("有一个连接断开");  
                }  
            }  
              
        }  
        public void go(){  
            System.out.println("服务器启动成功!");  
            ServerSocket serverSock;  
            try {  
                serverSock = new ServerSocket(5000);  
                while(true){  
                    Socket clientSocket=serverSock.accept();  
                    Thread thread=new Thread(new ClientHandler(clientSocket));  
                    thread.start();  
                    System.out.println("got a connection!");  
                }  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
              
        }  
          
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
            new SimpleChatServer().go();  
        }  
      
    }  

标签: 代码 服务器 服务器端

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java执行外部程序并返回结果

下一篇:一个简单的java死锁示例