473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Address Stucture for recvfrom vs. sendto

158 New Member
Is there a difference between the way the function recvfrom and sendto use the address structure.

When i use sento it works fine, but when I try and use recvfrom with the same variables it errors out with (bad address).

Or is there a better way to receive packets from a socket that i just sent to.

Theres some of my source code.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     struct sockaddr_in a;
  3.       a.sin_family = AF_INET;
  4.       a.sin_addr.s_addr = htonl(INADDR_ANY);
  5.       a.sin_port = htons(DefaultPort);
  6.       int sock;
  7.       sock = socket(AF_INET, SOCK_DGRAM, 0);  
  8.  
  9.       int fBroadcast = 1;
  10.       int err = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, "C", 5);
  11.       if (err == -1) { perror("setsockopt");     exit(1); }        
  12.  
  13.       //Binding the address to the socket
  14.       err = bind(sock, (struct sockaddr *)&a, sizeof(a));
  15.       if (err == -1) { perror("bind"); exit(1); }    
  16.  
  17.  
  18.       //Putting the ip address in the socket output address structure
  19.       a.sin_addr.s_addr = inet_addr(ip);
  20.  
  21.  
  22.       //Sending the information to the specified port
  23.       if (sendto(sock, packet, len, 0,    (struct sockaddr *)&a, sizeof(a)) == -1)
  24.       { perror("sendto"); exit(1);  }
  25.  
  26.       int rcv;
  27.       char buf[300];
  28.  
  29.        // Recieve
  30.       rcv = recvfrom(sock, buf, 208, MSG_WAITALL, (struct sockaddr *)&a , sizeof(a));
  31.     if(rcv == -1) { perror("recvfrom"); exit(1); }  
  32.  
  33.  
Jul 11 '08 #1
2 5606
weaknessforcats
9,208 Recognized Expert Moderator Expert
The variable a is a struct sockaddr_in.

You typecast the address of a to a struct sockaddr*.

The only way this will work is if struct sockaddr_in and struct sockaddr have the same members and each member has the same size.

Otherwise, you can't make a struct sockaddr out of a struct sockaddr_in just by typecasting an address.
Jul 12 '08 #2
thespartan
1 New Member
Hi kardon33, did you find out the cause of the error. I also face same problem. The sendto with similar signature is able to send out packets, but recvfrom throws bad address, any one ideas?
Apr 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8268
by: Wim Deprez | last post by:
Hi group, I am trying to port a reliable multicast framework for UNIX to Win32 and so far so good, but I stumbled on the next problem: in the original code, the programmers use the recvmsg() function and as far as I know, there is no Windows-version of this function. So I guess I will have to translate it to a recvfrom(), but that gives some problems. First of all, in the man pages I read the following:
2
3265
by: William | last post by:
I have declared the following struct: struct PropagateInfo { string type; // "registration", "gossip", "termination" int initiatePeerID; }; which I used as follows: PropagateInfo* initiatePeerInfo = new PropagateInfo; initiatePeerInfo->type = type; // type is set to "registration"
2
10130
by: SWJust | last post by:
Hi... is it possible to send data from a socket to at physical address (MAC) on f.x another PC or Router ??? I have a IP header with information about another client, and this cannot be altered, so I want to send the whole IP package to the server / router using OSI layer 2 (datalink), but is it possible in C#, or do i have to write a lowlevel driver. Thanks /Søren
2
2068
by: D.Frangiskatos | last post by:
Hi, I have been working for a few months in project that deals raw sockets. However recently, and while trying to examine the contents of the buffer used in recvfrom i was a bit confused. The buffer was allocated using malloc as it can be seen next: do { ..............
7
12038
by: Shoveler | last post by:
I'm trying to send an ARP to determine different devices on the network. I need to see the MAC address of my devices on my LAN. With the MAC I can determine if I need to talk to that devices. I figured the ARP is the best we to find who is out there. I'm using C# and the Raw SocketType. I run into the WSACancelBlockingCall problem. Here's my code: Socket MyPing = new Socket(AddressFamily.InterNetwork, SocketType.Raw ,...
1
6005
by: Jack | last post by:
Hi guys, I can't figure this out. rec = recvfrom(sdUDP, buf, BUFSIZE, 0, (struct sockaddr *)&connectChannel, &chanSizeUDP ); while(1){ if (rec 0){ snt = sendto(sdUDP, buf, rec, 0, (struct sockaddr *)&connectChannel, chanSizeUDP ); rec = recvfrom(sdUDP, buf, BUFSIZE, 0, (struct sockaddr
1
1677
by: windub | last post by:
Hi... I would like to capture an email address from a form and send a copy of the mail to that user. Can anyone give the correct code? Below is the script I am using. Thanks for your help. <?php
0
1225
by: Jeff | last post by:
Hi, Fairly new to python, messing with some socket and pcap sniffing and have come across the following issue while trying to do a pcap_loop (via pcapy http://oss.coresecurity.com/projects/pcapy.html). I believe i recall seeing similar stuff using other pcap libs with python in the past (such as pylibpcap) in a nutshell, blocking on a read using socket.recvfrom(buf), which strace shows me is sitting in recvfrom(2) allows ctrl-c to be...
3
6104
by: jkvbe | last post by:
Hi, I get a "Bad address" error message when trying to receive an ICMP timestamp packet using recvfrom in cygwin. The packet is OK as I see it arriving when sniffing the network. Any ideas what can be wrong? Thanks,
0
8838
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8613
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.