473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("WSAStar tup: %d\n", WSAStartup(MAKE WORD(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_fa mily = AF_INET;
RecvAddr.sin_ad dr.s_addr = inet_addr("10.1 0.0.209");
RecvAddr.sin_po rt = htons(atoi("80" ));

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

// Close the socket and cleanup
closesocket(Des tSocket);
WSACleanup();
}
Aug 8 '08 #1
2 3045
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("WSAStar tup: %d\n", WSAStartup(MAKE WORD(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_fa mily = AF_INET;
RecvAddr.sin_ad dr.s_addr = inet_addr("10.1 0.0.209");
RecvAddr.sin_po rt = htons(atoi("80" ));

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

// Close the socket and cleanup
closesocket(Des tSocket);
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("WSAStar tup: %d\n", WSAStartup(MAKE WORD(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_fa mily = AF_INET;
RecvAddr.sin_ad dr.s_addr = inet_addr("10.1 0.0.209");
RecvAddr.sin_po rt = htons(atoi("80" ));

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

// Close the socket and cleanup
closesocket(Des tSocket);
WSACleanup();
}
Aug 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
14497
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 headers). I can see this because I'm running Ethereal and watching the packets. I'm defining the packets as shown below: $text_msg = "Hello,...
1
2063
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 client breaks a file up into sections and sends them to the server, through teh gateway which just forwards info. Now upon getting the info, the...
3
1666
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 receive bye packet. and once i receive bye.. i am poping all packets from the list and
0
1444
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 data independent of a receive. The send happens just fine, but when I receive the next set of data from the client (in a size, size, data set of...
10
12179
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 sender's IP. Using debug files on the server I see the server receive the request from my application on my machine and I see the server send to the...
0
1258
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 . I am porting a C++ tunnel library into C# . It has 2 functions WriteOut: I make a header and send it then send the actual data to the server and...
9
3683
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 weird thing on Windows XP. I'm using Python 2.5 with latest pywin32 from Mark Hammond.
11
12452
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 does not work at all. I wrote a simple case test: client and server. The client sends 2200 packets within 0.137447118759 secs. The tcpdump received...
1
5179
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 instead, otherwise the results aren't very meaningful. I gave this a try on an AMD64 3200+ running a 32 bit Linux installation. Here's the...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.