0
votes

Ok. So I have a simple client program sending images over UDP to a specified IP address. At the specified IP a server program is listening and should receive the images.

When I try both programs on localhost (i.e. they are both running at 127.0.0.1) it works and the server receives the images. When I try to put the client and the server programs on different PCs in the same network it does not work.

147.232.24.163 is the server IP, 147.232.24.150 is the client IP.

Here is my client program:

  // Initialize UDP.    
  struct sockaddr_in server;    
  int n_sent;
  int socketId = socket(AF_INET, SOCK_DGRAM, 0);    
  if (socketId < 0)    
  {    
    cout << "Problem creating socket." << endl;    
  }    
  server.sin_family = AF_INET;    
  server.sin_addr.s_addr = inet_addr("147.232.24.163");    
  // Establish the server port number - we must use network byte order!    
  server.sin_port = htons(42001);

  for ( int iter = 0; iter < 60; iter++ )    
  {
      // load image into jpegBuf

      n_sent = sendto(socketId, reinterpret_cast<char*> (&jpegBuf[0]), jpegBuf.size(), 0, (struct sockaddr*) &server, sizeof(server));    
      if (n_sent < 0) {    
        cout << "Problem sending data." << endl;    
      }    
  }

  close(socketId);

Here is my server program:

int main()
{
    int bufferSize = 1024000;
    int iSockFd = -1;
    int iLength = 0;
    struct sockaddr_in servAddr, cliAddr;
    char buff[bufferSize];
    iSockFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    memset(&servAddr, 0, sizeof(servAddr));
    memset(&cliAddr, 0, sizeof(cliAddr));

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr("147.232.24.163"); 
    servAddr.sin_port = htons(42001);
    int cliAddrLen = sizeof(struct sockaddr_in);
    int bindRet = bind(iSockFd, (struct sockaddr*)&servAddr, sizeof(servAddr));
    cout << "Bind returned " << bindRet << endl;

    int i = 0;
    while (true)
    {
        int iRcvdBytes=recvfrom(iSockFd, buff, bufferSize, 0, 
        (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);

        if (0 == i % 5)
        {
            cout << "Received " << iRcvdBytes << " bytes from the client" << endl;
        }


        i++;
    }
    close(iSockFd);
    return 0;
}

Any ideas why it does not work? I don't get any error messages.

1
I have simplified the programs so there is only the essential socket related stuff. I know it's a pain to read very long code snippets. - Richard Knop
Make sure any firewalls on the two machines are disabled... lots of firewalls will filter incoming UDP packets. - Jeremy Friesner
@Jeremy Friesner I made sure of that. To be 100% sure I have uninstalled firewalls completely on both PCs. - Richard Knop
Why are you printing the number of bytes received only on every fifth iteration? (Maybe you're just very unlucky - four out of five messages succeed, but every fifth is failing.) Have you stepped through it in a debugger? - Ciaran Keating
@Ciaran Keating Well, because the images are supposed to be coming very fast (25-30 per second) so I want to be able to see with my eyes what's happening. - Richard Knop

1 Answers

2
votes

This is not a solution but your code should be checking iRcvdbytes for error in the same way as the send code checks the result of sendto.

A return of 0 means the socket was closed gracefully (if connection-oriented - that should not apply here), SOCKET_ERROR means an error (in WinSock2 at least).

The socket on the send side needs to be created with IPPROTO_UDP, not 0. This could definitely be causing your failure.