java套接字的问题,不会侦听多个数据报包

发布于 2024-08-19 00:06:17 字数 2218 浏览 4 评论 0原文

我正在创建一个服务器(现在称为 Server1),它与我拥有的另一台服务器(现在称为 Server2)进行通信。

  • Server1 向 Server2 发送数据报包。
  • Server2 应该发回两个数据报包,但我只收到一个。
  • Server2 没有问题,并且发送了两个包。

我使用 Wireshark 来嗅探离开和到达 Server1 的包。

包一(来自wireshark): “5955 11994.023974 192.168.1.3 192.168.1.2 UDP源 端口:50000 目标端口:50004"

包二(来自wireshark): 5958 11994.045830 192.168.1.3 192.168.1.2 ICMP 目标不可达(端口不可达)

Server1 运行一个正在侦听数据报包的线程。

代码 Server1:

     while (m_keepRunning)
    {
        try
        {
         TermMsg receivedMessage = null;
         receivedMessage = receive();   //See this function further down
         if (receivedMessage != null)
            {
                if (receivedMessage.getMsgType().equals(TermMsgType.ACK))
                {
                 System.out.println("This is an ack!");
                }
                else
                {

                 System.out.println("This is a response");
                }
            }
         else
         {
          System.out.println("This is nothing");
         }

        }

接收函数:

      private TermMsg receive() throws IOException, TermMsgException
{
  byte[] inBuf = new byte[BUF_SIZE_RX];
  DatagramPacket inData = new DatagramPacket(inBuf, BUF_SIZE_RX);

    if(true == firstEncounter)
    {
            StartMessage startReq = getStartMsg(false);
     DatagramPacket p = makeDatagramP(startReq);
     socket.send(p);
     firstEncounter = false;
    }

    socket.receive(inData);
    if (inData.getLength() > 0)
    {
        Msg msg;
        try
        {
            msg = Msg.createFromUdp(inData.getData());
            return msg;
        }
        catch (TermMsgException e1)
        {
            return null;
        }
    }
    else
    {
     try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {

        }
        return null;
    }
}

有人知道吗? 顺便说一句...我还使用:

DatagramSocket socket;
try {
  socket = new DatagramSocket(50004);
}

我是否必须使用服务器套接字来使其侦听多个数据报包?

概括: - 端口无法访问 - 无法收到第二个包裹

希望有人可以帮助我。 提前致谢

I'm creating a server(now called Server1) which is communicating with another server i've got(now called Server2).

  • Server1 sends a datagrampackage to Server2.
  • Server2 is supposed to send two datagram packages back, but i only get one back.
  • Server2 is not the problem, and sends two packages.

I use Wireshark to sniff the packages that leaves and comes to Server1.

Package one(from wireshark):
"5955 11994.023974 192.168.1.3 192.168.1.2 UDP Source
port: 50000 Destination port: 50004"

Package two(from wireshark):
5958 11994.045830 192.168.1.3 192.168.1.2 ICMP Destination unreachable (Port unreachable)

Server1 runs a Thread which is listening for datagram packages.

Code Server1:

     while (m_keepRunning)
    {
        try
        {
         TermMsg receivedMessage = null;
         receivedMessage = receive();   //See this function further down
         if (receivedMessage != null)
            {
                if (receivedMessage.getMsgType().equals(TermMsgType.ACK))
                {
                 System.out.println("This is an ack!");
                }
                else
                {

                 System.out.println("This is a response");
                }
            }
         else
         {
          System.out.println("This is nothing");
         }

        }

Receive function:

      private TermMsg receive() throws IOException, TermMsgException
{
  byte[] inBuf = new byte[BUF_SIZE_RX];
  DatagramPacket inData = new DatagramPacket(inBuf, BUF_SIZE_RX);

    if(true == firstEncounter)
    {
            StartMessage startReq = getStartMsg(false);
     DatagramPacket p = makeDatagramP(startReq);
     socket.send(p);
     firstEncounter = false;
    }

    socket.receive(inData);
    if (inData.getLength() > 0)
    {
        Msg msg;
        try
        {
            msg = Msg.createFromUdp(inData.getData());
            return msg;
        }
        catch (TermMsgException e1)
        {
            return null;
        }
    }
    else
    {
     try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {

        }
        return null;
    }
}

Does anyone have a clue?
btw... i also use:

DatagramSocket socket;
try {
  socket = new DatagramSocket(50004);
}

Do i have to use server socket to make it listen for more than one datagram package?

Summary:
- Port unreachable
- Can't receive package number two

Hope someone can help me.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-08-26 00:06:17

第一个问题,正如该问题的评论中已经提到的,您假设以下两行是正在发送的两个 UDP 数据包。

Package one(from wireshark): "5955 11994.023974 192.168.1.3 192.168.1.2 UDP Source port: 50000 Destination port: 50004"

Package two(from wireshark): 5958 11994.045830 192.168.1.3 192.168.1.2 ICMP Destination unreachable (Port unreachable)

事实上,这可能是不完整的跟踪,因为这显示了两个包:

Package one(from wireshark): "5955 11994.023974 192.168.1.3 192.168.1.2 UDP Source port: 50000 Destination port: 50004"

这是一个 UDP 数据包,从 server2 (192.168.1.3) 到 server1 (192.168.1.2)。它被发送到 server1 上的端口 50004。

Package two(from wireshark): 5958 11994.045830 192.168.1.3 192.168.1.2 ICMP Destination unreachable (Port unreachable)

这是从 server2 发送到 server1 的控制消息,表示由于目标端口未打开,无法传递先前的数据包。这是之前从 server1 到 server2 的数据包传送尝试的答案,但没有成功。这可能有助于解释为什么您没有收到预期的所有数据包?

The first problem, as already mentioned in the comments on the question, is that you are assuming that the following two lines are two UDP packets being sent.

Package one(from wireshark): "5955 11994.023974 192.168.1.3 192.168.1.2 UDP Source port: 50000 Destination port: 50004"

Package two(from wireshark): 5958 11994.045830 192.168.1.3 192.168.1.2 ICMP Destination unreachable (Port unreachable)

In actual fact, this is probably an incomplete trace, since this is showing two packages:

Package one(from wireshark): "5955 11994.023974 192.168.1.3 192.168.1.2 UDP Source port: 50000 Destination port: 50004"

This is a UDP packet, from server2 (192.168.1.3) to server1 (192.168.1.2). It is being sent to port 50004 on server1.

Package two(from wireshark): 5958 11994.045830 192.168.1.3 192.168.1.2 ICMP Destination unreachable (Port unreachable)

This is a control message being sent from server2 to server1 saying that a previous packet could not be delivered because the destination port was not open. This is an answer from a previous packet delivery attempt from server1 to server2, that did not work. This might help to explain why you are not getting all the packets that you expect?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文