han199341lu
han199341lu
2012-06-23 22:12

一个关于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条回答

  • iteye_878 iteye_878 9年前

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

    点赞 评论 复制链接分享

相关推荐