473,385 Members | 1,732 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,385 software developers and data experts.

Address Stucture for recvfrom vs. sendto

158 100+
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 5586
weaknessforcats
9,208 Expert Mod 8TB
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
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
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()...
2
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*...
2
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,...
2
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...
7
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...
1
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...
1
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 ...
0
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...
3
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...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.