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(); } } } }
相关文章: