Ikky
Ikky

Reputation: 2856

Problem with java socket, won't listen for more than one datagram package

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

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

Upvotes: 2

Views: 1529

Answers (1)

Paul Wagland
Paul Wagland

Reputation: 29116

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?

Upvotes: 1

Related Questions