UDP聊天程序错误?

时间:2015-12-03 21:32:42

标签: java udp chat

我是编程的初学者,所以我希望你能帮助我解决我的问题。我正在编写一个基于UDP的聊天程序,它有一个服务器和客户端类,并且没有任何问题的迹象,但是当我尝试运行该程序时,它会抛出“java.net.BindException:地址已在使用中:无法绑定“public class Server extends JFrame实现ActionListener {

服务器:

  public class Server extends JFrame implements ActionListener {

 byte[] serverBuffer,clientBuffer;
 DatagramSocket client,server;
 JTextField textField;
 JTextArea textArea;
 JButton button;


 public Server (){

   this.setSize(400,400);
   this.setTitle("Server");
   textField = new JTextField(100);
   textField.setBackground(Color.white);
   textField.setForeground(Color.black);
   this.add(textField,BorderLayout.NORTH);
   textArea = new JTextArea();
   this.add(textArea,BorderLayout.CENTER);
   textArea.setBackground(Color.white);
   textArea.setForeground(Color.green);
   button = new JButton("Send Message");
   this.add(button,BorderLayout.SOUTH);
   button.addActionListener(this);
   this.setVisible(true);

 serverBuffer = new byte[1024];
 clientBuffer = new byte[1024];

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try{
        client = new DatagramSocket();
        server = new DatagramSocket(9999);

       while(true){
           DatagramPacket dataPacket = new DatagramPacket(clientBuffer,clientBuffer.length);
           server.receive(dataPacket);
           String message = new String(dataPacket.getData());
           textArea.append("\nClient's message:" + message); 

       } 
    }catch(Exception e){
      e.printStackTrace();
    }

}

@Override
public void actionPerformed(ActionEvent e) {

    try{
        if(e.getActionCommand()=="Send Message"){
       String message = textField.getText();
       serverBuffer = message.getBytes();
       DatagramPacket sendPack = new DatagramPacket(serverBuffer,serverBuffer.length,InetAddress.getLocalHost(),9998);
       client.send(sendPack);
       textArea.append("\nMyself: " + message);
       textField.setText(" ");

     }  
   }catch(Exception a){
       a.printStackTrace();
   }

}

public static void main(String[] args){
   Server server = new Server();
 }

 } 

客户端:

  public class Client extends JFrame implements ActionListener {

byte[] clientBuffer,serverBuffer;
DatagramSocket server,client;
String sendData;
JTextField textField;
JTextArea textArea;
JButton button;


public Client(){  

   this.setSize(400,400);
   this.setTitle("Client");
   textField = new JTextField(100);
   textField.setBackground(Color.white);
   textField.setForeground(Color.black);
   this.add(textField,BorderLayout.EAST);
   textArea = new JTextArea();
   this.add(textArea,BorderLayout.CENTER);
   textArea.setBackground(Color.white);
   textArea.setForeground(Color.white);
   button = new JButton("Send Message");
   this.add(button,BorderLayout.NORTH);
   button.addActionListener(this);
   this.setVisible(true);

    serverBuffer = new byte[1024];
    clientBuffer = new byte[1024];

    setDefaultCloseOperation(EXIT_ON_CLOSE);


    try{
        client = new DatagramSocket();
        server = new DatagramSocket(9998);
    while(true){
        DatagramPacket dataPacket = new DatagramPacket(clientBuffer,clientBuffer.length);
        server.receive(dataPacket);
        String message = new String(dataPacket.getData());
        textArea.append("\nServer: " + message);

    }

    }catch(Exception e){
        e.printStackTrace();
    }

  }

@Override
public void actionPerformed(ActionEvent e) {
     try{
         if(e.getActionCommand()=="Send Message"){

        String message = textField.getText();
        clientBuffer = message.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(serverBuffer,serverBuffer.length,InetAddress.getLocalHost(),9999);
        client.send(sendPacket);
        textArea.append("\nClient: " + message);
        textField.setText("");
     }   
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

public static void main(String[] args){
    Client obj = new Client();
  }


  }

1 个答案:

答案 0 :(得分:0)

您是否正在运行其他服务器进程?在这种情况下,您需要终止该过程,然后再次启动它。

错误意味着您正在使用的套接字已在使用中,很可能是另一个Java进程。进入任务管理器(如果您在PC上)应该有助于查看当前正在执行的其他进程。