473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with sending UDP packets

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
2 3026
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: coder_1024 | last post by:
I'm trying to send a packet of binary data to a UDP server. If I send a text string, it works fine. If I attempt to send binary data, it sends a UDP packet with 0 bytes of data (just the...
1
by: Adam Balgach | last post by:
I am having a bit of a problem, and it might have to do with my understanding of how select works... basically what i am trying to simulate is a system: client->gateway->server model. The...
3
by: emanshu | last post by:
Hi all, i am facing some problems with list.. i am receiving some data from some sender and i am storing this data in to list. i am receiving and storing all the packets in the list till i...
0
by: Lynne | last post by:
I am using the C# asynchronous socket functionality for a server, and it appears to work fine if I just receive data and echo it back to the client. The problem occurs when I try to handle sending...
10
by: Jim H | last post by:
I have a UDP socket that sends out a request on a multicast socket and waits for a response. This client is not listening on a multicast IP but the local IP. The server (UNIX) responds to the...
0
by: raza | last post by:
Hi, I have been programmin in C# for quite some time now and recently got into sockets programming in C# though i have done it in C++ on windows/linux . I have an interesting problem at hand . ...
9
by: Irmen de Jong | last post by:
Hello Sorry this might be a bit offtopic but I don't really know where else to post this question. If you could point me in the right direction that is much appreciated. I'm running into a...
11
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach...
1
by: Jean-Paul Calderone | last post by:
On Fri, 21 Nov 2008 00:20:49 -0200, Gabriel Genellina <gagsl-py2@yahoo.com.arwrote: If you want to try this program out on POSIX, make sure you change the time.clock() calls to time.time() calls...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.