在一台机器上运行服务器和客户端(netbeans 8.1)

时间:2016-04-16 15:39:18

标签: java macos netbeans

我在一台(mac)计算机上运行服务器和客户端时遇到问题。我可以运行服务器,但是当我运行客户端时,它会给我一个错误java.net.BindException: Address already in use at java.net.PlainDatagramSocketImpl.bind0(Native Method) 据我所知,有一些需要使用的sshthing调用ssh,但我不知道如何使用它来解决这个问题。 谢谢

public class WRRCourseWork {
public static void main(String[] args) {
    try {
        DatagramSocket IN_socket = new DatagramSocket(3000);
        DatagramSocket OUT_socket = new DatagramSocket(5000);
        IN_socket.setSoTimeout(0);
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while (true) {
            //recive the message 

            IN_socket.receive(packet);
            String message = new String(buffer);
            System.out.println("Got message: " + message.trim());

            // send the message 
            String host = "";
            InetAddress addr = InetAddress.getByName(host);
            DatagramPacket OUT_Packet = new DatagramPacket(message.getBytes(), message.getBytes().length, addr, 5000);
            OUT_socket.send(OUT_Packet);
            System.out.println("Sending Message: "+ message.trim());
        } 

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

...客户

    public class Messages {
    public static void main(String [] args) {
        System.out.println("hiiiiiii");
        //String host = "localhost";
        try {
            while (true) {
                InetAddress addr = InetAddress.getLocalHost();
                String message = "Hello World";
                DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, addr, 4000);
                DatagramSocket socket = new DatagramSocket(4000);
                socket.send(packet);
                //socket.close();
            }
        } catch(Exception error) {
            // catch all errors
            error.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的服务器正在侦听端口3000,因此请将您的客户端更改为使用端口3000,并且仅在端口3000中指定端口3000,而不是在套接字中定义。

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Messages {

public static void main(String [] args) {
    System.out.println("hiiiiiii");
    //String host = "localhost";
    DatagramSocket socket = null;
    try {
        while (true) {
            InetAddress addr = InetAddress.getLocalHost();
            String message = "Hello World";
          DatagramPacket packet = 
                  new DatagramPacket(message.getBytes(), 
                          message.getBytes().length, addr, 3000);
          socket = new DatagramSocket();
          socket.send(packet);
          socket.close();
        }
    } catch(Exception error) {
        // catch all errors
        error.printStackTrace();
    } 
}
}

服务器上的结果应为:

Got message: Hello World
Sending Message: Hello World
Got message: Hello World
Sending Message: Hello World
Got message: Hello World
Sending Message: Hello World
. . .