Problem with sending UDP packets

G

GregII

Hi All,
I have some problems with sending UDP packets using Winsock. I tried to send
some to a closed port, and according to the documentation on Microsoft MSDN
site (http://msdn.microsoft.com/en-us/library/ms740148(VS.85).aspx)
subsequent calls of sendto function should result in returning WSAECONNRESET
error code caused by returning ICMP destination unreachable message.
My problem is that I do not see such effect, the sendto always returned 0
indicating that there were no ICMP error messages. I captured the traffic and
saw that actually UDP packets were sent and ICMP messages received. It seems
that the sendto function for some reason does not see ICMP.
What I am doing wrong? Please find below the code, simplified as much as
possible. I tried it on Windows XP and Server 2003.

Any hints are greatly appreciated.

Greg


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

void main() {
WSADATA wsaData;
char *Data = "Some data";
struct sockaddr_in RecvAddr;

// Initialize Winsock
printf("WSAStartup: %d\n", WSAStartup(MAKEWORD(2, 2), &wsaData));

// Create a socket for sending data
SOCKET DestSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
printf("Socket: %d\n", DestSocket);

// Set up IP address and port
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_addr.s_addr = inet_addr("10.10.0.209");
RecvAddr.sin_port = htons(atoi("80"));

// Send an UDP packet
for (int i = 1; i < 3; i++) {
printf("\n");
int BytesSent = sendto(DestSocket, Data, strlen(Data), 0, (sockaddr
*)&RecvAddr, sizeof (RecvAddr));
printf("Bytes sent: %d\n", BytesSent);
printf("WSAGetLastError: %d\n", WSAGetLastError());
printf("Wait a while and press Enter\n"); _fgetchar();
};

// Close the socket and cleanup
closesocket(DestSocket);
WSACleanup();
}
 
X

Xargon

Greg,

There is a mistake on MSDN website in description of the sendto function. In
fact sendto returns 0 (success) regardless of if the reciptient ID address or
port exists or not. To examine the returning ICMP port unreachable message
call the recv function (you may need to use a non-blocking socket). This
function returns the WSAECONNRESET value in such a case.

Hope this helps.
Xargon
 
G

GregII

Xargon, you are great! It works! Thanks!


Xargon said:
Greg,

There is a mistake on MSDN website in description of the sendto function. In
fact sendto returns 0 (success) regardless of if the reciptient ID address or
port exists or not. To examine the returning ICMP port unreachable message
call the recv function (you may need to use a non-blocking socket). This
function returns the WSAECONNRESET value in such a case.

Hope this helps.
Xargon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top