By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Problem with sending UDP packets

P: n/a
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/libr...48(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();
}
Aug 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"GregII" wrote:
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/libr...48(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();
}
Aug 10 '08 #2

P: n/a
Xargon, you are great! It works! Thanks!
"Xargon" wrote:
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

"GregII" wrote:
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/libr...48(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();
}
Aug 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.