Java - 使用GUI进行客户端到客户端的通信。返回消息问题

时间:2017-09-11 15:26:50

标签: java

我试图建立客户端到客户端的通信,但我遇到了一些麻烦。

目前,我有一个作为服务器的行为,另一个作为客户端,我正努力让服务器以简单的短信形式将连接确认发送回客户端在GUI中。

我的代码目前使客户端向服务器发送简单的问候消息。注释掉的代码是我最近尝试将连接确认消息发送回客户端,但它没有工作。

这是服务器代码:

JMenuItem SeederItem = new JMenuItem("Seeder mode");
SeederItem.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        // Seeder mode

        try
        {
            // Waiting to be connected to
            System.out.println("Seeder waiting for leecher..");
            DatagramSocket SeederSocket = new DatagramSocket(2345);
            byte[] SeederBuffer = new byte[512];
            DatagramPacket DGP = new DatagramPacket(SeederBuffer,SeederBuffer.length);

            // Receiving connection attempt
            String message = "";
            SeederSocket.receive(DGP);
            message = new String(SeederBuffer);

            System.out.println(message);

            /*
            // Response to connection attempt
            String returnGreeting = "You have successfully connected to the seeder!";
            SeederBuffer = returnGreeting.getBytes();
            DatagramPacket DGPResponse = new DatagramPacket(SeederBuffer,SeederBuffer.length);
            SeederSocket.send(DGPResponse);
            */
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
});

以下是客户的代码:

JButton btnConnect = new JButton("Connect to seeder");
btnConnect.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            // Attempting to connect
            InetAddress MyAddress = InetAddress.getByName(txtHostAddress.getText());
            DatagramSocket LeecherSocket = new DatagramSocket();

            String Greeting = "Hello from the leecher";
            byte[] LeecherBuffer = Greeting.getBytes();

            DatagramPacket dp = new DatagramPacket(LeecherBuffer,LeecherBuffer.length,MyAddress,Integer.parseInt(txtPort.getText()));
            LeecherSocket.send(dp);

            System.out.println("Activation test");

            //????

            /*
            // Receiving successful connection response
            DatagramPacket DGPReceiveResponse = new DatagramPacket(LeecherBuffer,LeecherBuffer.length);

            String message = "";
            LeecherSocket.receive(DGPReceiveResponse);
            message = new String(LeecherBuffer);

            System.out.println(message);
            */
        }
        catch (UnknownHostException e1)
        {
            e1.printStackTrace();
        }
        catch (SocketException e1)
        {
            e1.printStackTrace();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
    }
});

如果注释掉的代码被取消注释,它甚至会破坏从客户端向服务器发送客户端消息的代码。它返回此错误:

  

线程中的异常" AWT-EventQueue-0" java.lang.NullPointerException:null地址||空缓冲区 -

它还提到了这个问题:

  

ClientFrame1 $ 1.actionPerformed(ClientFrame1.java:83), which refers to this line: SeederSocket.send(DGPResponse);

1 个答案:

答案 0 :(得分:1)

您需要提供数据包所在位置的IP地址。就像你发送第一个数据包时所做的那样。 http://download.java.net/jdk7/archive/b123/docs/api/java/net/DatagramPacket.html#getAddress()

在您执行SeederSocket.recieve(DCP)时创建的DGP实例中使用此函数;

所以:

String returnGreeting = "You have successfully connected to the seeder!";
                SeederBuffer = returnGreeting.getBytes();
                DatagramPacket DGPResponse = new DatagramPacket(SeederBuffer,SeederBuffer.length, DGP.getAddress(), DGP.getPort());
                SeederSocket.send(DGPResponse);