Advertisement

07.06.2004 at 02:42PM PDT, ID: 21049786
[x]
Attachment Details

Problem sending large UDP packets with sendto

Asked by matman13 in Microsoft Visual C++.Net

Tags: udp, sendto, packet

I've got a test program (see code below) that sends and receives 1400 bytes using UDP.  The first time through the while loop, the timeout will occur, causing the buffer to be sent.  During the subsequent wait, the socket should become "readable," causing the buffer to be received.  This looping construct will continue until I kill the program.

The problem I am encountering is that this bit of code does not work uniformly across Win2K/XP machines for packets above 1272 bytes.  I've got 2 Win2k machines and 1 XP machine that it seems to work on regardless of packet size.  However, I've got 3 XP machines that it works on so long as the packet size is less than or equal to 1272.  Any ideas?  Does this have anything to do with the UDP MTU?  I've seen info online that would indicate that adjusting the MTU settings in the registry might fix this, but there's been nothing definitive.  In the end, I have an application that needs to be able to send packets of up to 1400 bytes without fragmentation.

/********************
**  TEST PROGRAM
********************/

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <memory.h>
#include <stdio.h>

void main ()
{
    // Get the size of the buffer from the user.
    int size = 0;
    ::printf("Enter the size of the send/receive buffer:\n");
    ::scanf("%ld", &size);
    char *sendbuf = new char[size];
    char *recvbuf = new char[size];

    // Initialize the Winsock DLL.
    WORD versionRequested = MAKEWORD(2,2);
    WSADATA wsadata;
    ::WSAStartup(versionRequested, &wsadata);

    // Create the socket.
    SOCKET s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    // Set the socket for broadcast and non-blocking mode.
    unsigned long on = 1;
    ::setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char const *)&on, sizeof(on));
    ::ioctlsocket(s, FIONBIO, &on);

    // Bind the socket.
    unsigned short port = 3000;
    SOCKADDR_IN addr;
    addr.sin_family = AF_INET;
    addr.sin_port = ::htons(port);
    addr.sin_addr.s_addr = ::htonl(INADDR_ANY);
    ::bind(s, (PSOCKADDR)&addr, sizeof(addr));

    // Create the 'readable' socket event.
    WSAEVENT wsaevent = ::WSACreateEvent();
    ::WSAEventSelect(s, wsaevent, FD_READ);

    // Primary loop
    while (true) {
        switch (::WSAWaitForMultipleEvents(1, &wsaevent, false, 1000, false)) {
        case WSA_WAIT_EVENT_0:
        {
          WSANETWORKEVENTS wsaNetworkEvents;
          ::WSAEnumNetworkEvents(s, wsaevent, &wsaNetworkEvents);
          if (wsaNetworkEvents.lNetworkEvents & FD_READ) {
              int bytesRemaining = size, bytesRecv = 0, ix = 0;

              // Receive the buffer.
              while (bytesRemaining > 0) {
                  bytesRecv = ::recvfrom(s, &recvbuf[ix], bytesRemaining, 0, 0, 0);
                  if (bytesRecv == SOCKET_ERROR) {
                      if (::WSAGetLastError() != WSAEWOULDBLOCK) {
                          throw;
                      }
                      break;
                  }
                  bytesRemaining -=bytesRecv;
                  ix += bytesRecv;
              }

              ::printf("Received %ld bytes...\n", ix);
          }
          break;
        }
        case WSA_WAIT_TIMEOUT:
        {
          // Send 1400 'G' characters on the socket.  'G' is arbitrary.
          ::memset(sendbuf, 'G', size);

          SOCKADDR_IN dest;
          dest.sin_family = AF_INET;
          dest.sin_port = ::htons(port);
          dest.sin_addr.s_addr = ::htonl(INADDR_BROADCAST);

          ::sendto(s, sendbuf, size, 0,
                   (SOCKADDR *)&dest, sizeof(dest));

          ::printf("Sent %ld bytes...\n", size);

          break;
        }
        default:
          throw;
        }
    }

    ::closesocket(s);

    ::WSACleanup();

    delete [] sendbuf;
    delete [] recvbuf;

    return;
}


Start Free Trial
[+][-]07.06.2004 at 04:01PM PDT, ID: 11486777

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.06.2004 at 04:05PM PDT, ID: 11486806

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.06.2004 at 04:53PM PDT, ID: 11487096

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.06.2004 at 07:00PM PDT, ID: 11487692

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 08:25AM PDT, ID: 11492777

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 01:39PM PDT, ID: 11496357

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 02:38PM PDT, ID: 11496841

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 03:37PM PDT, ID: 11497247

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 05:28PM PDT, ID: 11497731

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual C++.Net
Tags: udp, sendto, packet
Sign Up Now!
Solution Provided By: drichards
Participating Experts: 2
Solution Grade: B
 
 
[+][-]07.07.2004 at 07:00PM PDT, ID: 11498095

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2004 at 08:15PM PDT, ID: 11498390

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32