问题描述
服务器端:packagecom.eesina1;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.HashMap;importjava.util.Map;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importcom.eesina1.ChatServer;publicclassChatServerimplementsActionListener,Runnable{JTextAreashowArea;JTextFieldmsgText;JFramemainJframe;JButtonsentBtn;JScrollPaneJSPane;JPanelpanel;Containercon;Threadthread=null;ServerSocketserversocket;SocketconnectToClient;DataInputStreaminFromClient;DataOutputStreamoutToClient;publicChatServer(){mainJframe=newJFrame("聊天-服务器端");con=mainJframe.getContentPane();showArea=newJTextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=newJScrollPane(showArea);msgText=newJTextField();msgText.setColumns(30);msgText.addActionListener(this);sentBtn=newJButton("发送");sentBtn.addActionListener(this);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(msgText);panel.add(sentBtn);con.add(JSPane,BorderLayout.CENTER);con.add(panel,BorderLayout.SOUTH);mainJframe.setSize(500,400);mainJframe.setVisible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try{//创建服务套接字serversocket=newServerSocket(5500);showArea.append("正在等待用户请求n");//侦听客户端的连接while(true){connectToClient=serversocket.accept();System.out.println("侦听到来自客户端的socket"+connectToClient);inFromClient=newDataInputStream(connectToClient.getInputStream());outToClient=newDataOutputStream(connectToClient.getOutputStream());//启动线程在后台来接收对方的消息thread=newThread(this);thread.setPriority(Thread.MIN_PRIORITY);thread.start();}}catch(IOExceptione){showArea.append("对不起,不能创建服务器n");msgText.setEditable(false);sentBtn.setEnabled(false);}}publicstaticvoidmain(String[]args){newChatServer();}//响应按钮事件,发送消息给对方publicvoidactionPerformed(ActionEvente){Strings=msgText.getText();if(s.length()>0){try{outToClient.writeUTF(s);System.out.println("服务器发送了消息!");outToClient.flush();showArea.append("我说:"+msgText.getText()+"n");msgText.setText(null);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}}//本线程负责将客户机传来的信息显示在对话区域publicvoidrun(){while(true){try{System.out.println("服务器端准备收信息!");showArea.append("对方说:"+inFromClient.readUTF()+"n");System.out.println("服务器收到了消息!");thread.sleep(1000);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}客户端:packagecom.eesina1;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.Socket;importjava.net.UnknownHostException;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importcom.eesina1.ChatClient;publicclassChatClientimplementsActionListener,Runnable{JTextAreashowArea;JTextFieldmsgText;JFramemainJframe;JButtonsentBtn;JScrollPaneJSPane;JPanelpanel;Containercon;Threadthread=null;SocketconnectToServer;DataInputStreaminFromServer;DataOutputStreamoutToServer;publicChatClient(){mainJframe=newJFrame("聊天-客户端");con=mainJframe.getContentPane();showArea=newJTextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=newJScrollPane(showArea);msgText=newJTextField();msgText.setColumns(30);msgText.addActionListener(this);sentBtn=newJButton("发送");sentBtn.addActionListener(this);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(msgText);panel.add(sentBtn);con.add(JSPane,BorderLayout.CENTER);con.add(panel,BorderLayout.SOUTH);mainJframe.setSize(500,400);mainJframe.setVisible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//创建套接字连接到服务器try{connectToServer=newSocket("localhost",5500);inFromServer=newDataInputStream(connectToServer.getInputStream());outToServer=newDataOutputStream(connectToServer.getOutputStream());}catch(UnknownHostExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}showArea.append("连接成功,请说话n");//创建线程在后台处理对方的消息thread=newThread(this);thread.setPriority(Thread.MIN_PRIORITY);thread.start();}publicstaticvoidmain(String[]args){newChatClient();}//响应按钮事件,发送消息给对方publicvoidactionPerformed(ActionEvente){Strings=msgText.getText();if(s.length()>0){try{System.out.println("客户端准备发消息!");outToServer.writeUTF(s);System.out.println("客户端像服务器发出了消息!");outToServer.flush();showArea.append("我说:"+msgText.getText()+"n");msgText.setText(null);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}}publicvoidrun(){while(true){try{showArea.append("对方说:"+inFromServer.readUTF()+"n");System.out.println("客户端从服务器收到了消息!");thread.sleep(1000);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}同时开一个服务器端和两个客户端,两个客户端向服务器端发消息,每个客户端都分别只能发一条服务器端能接收到的消息,然后还是可以发送消息,但是服务器端不能接收了。各位大侠帮帮忙啊!
解决方案
解决方案二:
好长啊。老夫无力。
解决方案三:
线程管理服务端
解决方案四:
从程序逻辑看,肯定是有问题的。举一例,ChatServerr中定义了三个field,SocketconnectToClient;DataInputStreaminFromClient;DataOutputStreamoutToClient;
那楼主的例子中,两个Client,那么这三个到底对应的是哪个Client?
解决方案五:
给个完整的聊天程序供楼主参考,
解决方案六:
呵,好长啊啊。。