public class Service {  //服务器

    public static void main(String[] args) {
      ServerSocket serverSocket=null;
      Socket socket=null;
      //既然是 双方的通信   输入和输出流 必须有
      OutputStream os=null;
      InputStream  is=null;
      BufferedReader  br=null;
      byte [] buf=new byte[1024];
      int   say;  
      String  word=null;
          try {
              serverSocket=new ServerSocket(8800);
              while(true){ //始终保持 畅通
            socket=serverSocket.accept();
            //实现多个窗口聊天
            ThreadSocket socket2=new ThreadSocket(socket);
            socket2.start();
            /*//打开所有的输入输出流
            is=socket.getInputStream();
            os=socket.getOutputStream();
            //接收客户端的信息
            say=is.read(buf);
            if (say!=0) {
                word =new String(buf, 0, say);
            }
            System.out.println(word);
            //给客户的回应
            System.out.println("====请您输入需要回复的信息====");
            br=new BufferedReader(new InputStreamReader(System.in));
            os.write(br.readLine().getBytes());  //保证都是 最新的回复
*/              }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                br.close();
                os.close();
                is.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
          
      }
    
}
服务器端代码

相关文章:

  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-12-05
  • 2021-10-27
猜你喜欢
  • 2021-12-23
  • 2021-06-13
  • 2021-09-23
  • 2022-12-23
  • 2021-07-28
相关资源
相似解决方案