一个关于java数据报做的简单聊天的问题

数据只能从Client端发送到Server端,Server端发送到Client端不仅重复发送,而且Client端貌似接收不到,请问问题出在哪里,怎么解决?
PS:希望能在Client端之间聊天 :)
[code="java"]
public class Server extends JFrame implements Runnable
{
final static int MEMBER_COUNT = 2,PORT_NUMBER =5000;//最大连接数目
private JTextArea displayArea; // displays packets received
private DatagramSocket socket; // socket to connect to client
Member[] members;
int number = 0;//已经连接到的client数目
// 创建 GUI 和 DatagramSocket
public Server()
{
super( "Server" );
members =new Member[Server.MEMBER_COUNT];
displayArea = new JTextArea(); // create displayArea
add( new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 400, 300 ); // set size of windows
this.addWindowListener(new WindowAdapter(){

    @Override
    public void windowClosing(WindowEvent e) {
        // TODO Auto-generated method stub
        socket.close();
    }

  });
  setVisible( true ); // show window

  try // create DatagramSocket for sending and receiving packets
  {
     socket = new DatagramSocket( Server.PORT_NUMBER );
  } // end try
  catch ( SocketException socketException ) 
  {
     socketException.printStackTrace();
     System.exit( 1 );
  } // end catch

} // end Server constructor
//连接到服务器
public boolean connectTo(InetAddress address)
{
for(int i=0;i<server.member_count if members new member number this.displaymessage return true false private boolean isfirst a for i="0;i&lt;Server.MEMBER_COUNT;i++)" public void waitforpackets while try receive packet display contents copy to client byte data set up datagrampacket receivepacket="new" data.length socket.receive wait this.connectto system.out.printf string sendpackettoclient send end catch ioexception displaymessage ioexception.tostring ioexception.printstacktrace method echo throws create sendpacket receivepacket.getlength socket.send manipulates displayarea in the event-dispatch thread final messagetodisplay swingutilities.invokelater runnable run updates displayarea.append message anonymous inner class call todo auto-generated stub server extends jframe implements jtextfield enterfield entering messages jtextarea displaying datagramsocket socket connect name gui and myname super here enterfield.addactionlistener actionlistener actionperformed actionevent event get from textfield message.getbytes convert bytes inetaddress.getlocalhost enterfield.settext addactionlistener add borderlayout.north jscrollpane borderlayout.center setsize window size setvisible show sending receiving packets socketexception socketexception.printstacktrace system.exit constructor arrive system.out.println ok receivepacket.getdata exception exception.tostring exception.printstacktrace int port inetaddress address this.port="port;" this.address="address;" servertest static main args application="new" client1="new" executorservice e="Executors.newFixedThreadPool(3);" e.execute></server.member_count>

1个回答

Server.connetTo方法不对啊
members[i] = new Member(Server.PORT_NUMBER, address);
这里每个member的端口都是5000?显然不对了。这样等于server不停地在给自己5000端口发,再收,再发,死循环了。
第一次client创建连接时,可以把自己的ip和端口先发给server。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!