Java Socket多线程编程示例详解编程语言

客户端代码如下:

    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();   
        }   
       
    }  

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10773.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论