Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java UDP消息服务_Java_Sockets_Udp_Ip Address_Datagram - Fatal编程技术网

Java UDP消息服务

Java UDP消息服务,java,sockets,udp,ip-address,datagram,Java,Sockets,Udp,Ip Address,Datagram,我必须创建一个程序,向远程IP地址发送和接收消息。所以基本上它应该模拟一个消息传递服务。在程序中,必须输入IP地址,数据将发送到该特定地址。目前,我可以发送,但在远程IP地址机上,没有收到任何内容,反之亦然。请你帮我理解为什么它不起作用 public class UDPchat extends Thread { private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.i

我必须创建一个程序,向远程IP地址发送和接收消息。所以基本上它应该模拟一个消息传递服务。在程序中,必须输入IP地址,数据将发送到该特定地址。目前,我可以发送,但在远程IP地址机上,没有收到任何内容,反之亦然。请你帮我理解为什么它不起作用

public class UDPchat extends Thread
{
   private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   int port=1234;                             // port to send/receive datagrams on
   String remoteIPaddress= null;      // IP to send datagrams

   // constructor, parameter is command line parameters
   public UDPchat(String IPAdrr) throws Exception
    {

    System.out.println("chat program: IP address " + InetAddress.getLocalHost().toString() + " port " + port );

    start();        // start thread to receive and display datagrams
    // loop waiting for keyboard input, send datagram to remote IP                
    while(true)
      try
        {
        String s = in.readLine();                       // read a String
        System.out.println("Sending to " + IPAdrr + " socket " + port + " data: " + s);
        byte[] data = s.getBytes();                                     // convert to byte array
        DatagramSocket theSocket = new DatagramSocket();                // create datagram socket and the datagram
        DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(IPAdrr), port);
        theSocket.send(theOutput);                                      // and send the datagram
        System.out.println("send everything");
        start();

       }
      catch (Exception e) {System.out.println("Eroor sending datagram " + e);
   // thread run method, receives datagram and display contents as a string
   public void run()                
        {
          try
              {
              // open DatagramSocket to receive 
              DatagramSocket ds = new DatagramSocket(port);
              // loop forever reading datagrams from the DatagramSocket
              while (true)
                 {
                 byte[] buffer = new byte[65507];   
                 // array to put datagrams in
                 DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // DatagramPacket to hold the datagram
                 ds.receive(dp);
                 // wait for next datagram
                 String s = new String(dp.getData(),0,dp.getLength());        // get contenets as a String
                 System.out.println("UDP datagram length " + s.length()+ "  from IP " + dp.getAddress() + " received: " + s );
                 }
              }
          catch (SocketException se) {System.err.println("chat error " + se); }
          catch (IOException se) {System.err.println("chat error " + se);}
          System.exit(1);                                                       // exit on error
        }

public static void main(String args[]) throws Exception
{
    Scanner in = new Scanner(System.in);
    String remoteIPaddress = in.nextLine();
   UDPchat c=new UDPchat(remoteIPaddress);
}

}

首先,对于消息传递服务,您应该使用TCP作为其 可靠性

UDP用于实时通信,其中最新数据更为重要

考虑到您的问题,如果您的计算机位于不同的网络中,可能是因为路由器进行了网络地址转换

我相信您正在将数据包发送到正确的IP地址,但端口不正确。您可能要将数据包发送到的端口是计算机的本地端口。 您需要将数据包发送到路由器指定的端口(或外部端口)

当数据包通过路由器从计算机a传送到计算机B时,路由器将计算机的本地端口映射到某个随机的外部端口

因此,如果计算机B需要向计算机a发送数据包,则计算机B需要通过路由器分配给计算机a的外部IP:端口进行应答。然后路由器将它转发到计算机A的本地IP端口

我建议您首先了解UDP时如何创建此映射 数据包通过网络内外的路由器传输

在使用TCP/UDP之前,请仔细阅读一些网络概念,如网络地址转换、UDP holepunching

这些来源可能有助于:

你也可以在这里参考我对相同/类似问题的一些回答:


如果您的客户端和服务器都在本地计算机上,该功能是否有效?是的,我可以向自己发送和接收消息,当我需要向远程计算机发送/接收消息时会出现问题。对于未引用的文本,不使用引号格式。