Java UDP套接字配置

时间:2015-01-04 11:16:31

标签: java sockets udp

我一直在努力让一个简单的聊天室客户端/服务器与java UDP一起使用我在将我的代码从localhost更改为我已经转发端口的IP地址时遇到了一些麻烦。

当客户端尝试从服务器接收数据时,会出现此问题。根据我的测试,我可以看到数据最初从客户端到服务器,服务器说它发送了数据,但我不知道我是通过网络丢失数据还是我的IP地址混淆了或者如果客户端无法接收数据。

客户端

public class Client
{
    DatagramSocket clientSocket;
    InetAddress serverIP;
    int serverPort;

    public Client()
    {
        try
        {
            this.serverIP = InetAddress.getByName("my.ip.add.ress");
            this.serverPort = 6969;

            this.clientSocket = new DatagramSocket();//create client socket
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }

    public void start() throws IOException
    {
        Scanner scanner = new Scanner(System.in);

        while(true)
        {
            //get client message
            String message = scanner.nextLine();

            //convet to byte array
            byte[] sendData = new byte[1024];
            sendData = message.getBytes();

            //send data
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverIP, serverPort);
            clientSocket.send(sendPacket);

            //receive data
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);

            //print
            System.out.println(new String(receivePacket.getData()));
        }
    }
}

服务器

public class Server
{
DatagramSocket serverSocket;

ArrayList<InetAddress> clientIPs;
ArrayList<Integer> clientPorts;

public Server()
{
    try
    {
        //create and bind server socket
        serverSocket = new DatagramSocket(null);
        serverSocket.bind(new InetSocketAddress(InetAddress.getByName("comp.local.ip.add"), 6969));//the ip is port forwarded

        clientIPs = new ArrayList<InetAddress>();
        clientPorts = new ArrayList<Integer>();
    }
    catch (SocketException e)
    {
        e.printStackTrace();
    }
    catch (UnknownHostException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void start() throws IOException
{
    while(true)
    {
        //receive data
        byte[] receiveData = new byte[1024];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String message = new String(receivePacket.getData());
        System.out.println("From Client: " + message);
        InetAddress ipAddress = receivePacket.getAddress();
        int port = receivePacket.getPort();

        //add client ip and port to arrays if they dont already exist
        if(!(clientIPs.contains(ipAddress) && clientPorts.contains(port)))
        {
            clientIPs.add(ipAddress);
            clientPorts.add(port);
            System.out.println("Client added");
        }

        //create send data
        byte[] sendData = new byte[1024];
        sendData = message.toUpperCase().getBytes();

        //send data to all clients
        for(int i = 0; i < clientIPs.size(); i++)
        {
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientIPs.get(i), clientPorts.get(i));
            serverSocket.send(sendPacket);
            System.out.println("sent to " + clientIPs.get(i).getHostAddress() + ":" + clientPorts.get(i));
        }
    }
}
}

0 个答案:

没有答案