473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with recvfrom

I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.

The client's source code:

#include "reversi.h"
-------------------------------------------------------------------
int main() {
int socket_id = socket(PF_INET, SOCK_DGRAM,0);
struct sockaddr_in adres;
adres.sin_famil y = PF_INET;
adres.sin_port = htons(PORT_NR);
adres.sin_addr. s_addr = inet_addr("127. 0.0.1");
cli_msg msg;
msg.type = CMT_LOGIN;
sendto(socket_i d,&msg,sizeof(c li_msg),0,(stru ct sockaddr*)&adre s,sizeof
(struct sockaddr));
printf("wyslale m na 127.0.0.1\n");
ser_main_msg ser_msg;
unsigned int rozmiar;
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );
printf("otrzyma lem cos a adresu %s",inet_ntoa(a dres.sin_addr)) ;
printf("type = %d\n",ser_msg.t ype);
return 1;
}
-------------------------------------------------------------------
All needed header files are included in reversi.h

Server is rather long, but the inportant part is, the socket is datagram
socket:
-------------------------------------------------------------------
bzero(&klient1, sizeof(struct sockaddr_in));
int ret = recvfrom(sockid ,&msg_kli1,size of(cli_msg),0,( struct sockaddr*)
&klient1,&rozmi ar1);
printf("receive d %s, %d, %d\n",inet_ntoa (klient1.sin_ad dr), ret,
errno);
if (msg_kli1.type == CMT_LOGIN) {
msg.type = SMMT_WAIT;
sendto(sockid,& msg,sizeof(ser_ main_msg),0,(st ruct sockaddr*)
&klient1,rozmia r1);
printf("send SMMT_WAIT to address %s\n",inet_ntoa
(klient1.sin_ad dr));
kli_nr++;
-------------------------------------------------------------------
serwer should print:
received 127.0.0.1, 16, 0
send SMMT_WAIT to addrress 127.0.0.1

but it prints:
reveived 0.0.0.0., 16, 0
send SMMT_WAIT to address 0.0.0.0

It happens only when I run the program on my computer, when I tried on
friends computer problem doesn't occur. Any ideas?

Omega
Nov 21 '05 #1
9 8463
Omega <Omega@NO_SPAM. pl> writes:
I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.

[snip]

There is no recvfrom() function in standard C. Followups redirected.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '05 #2

"Omega" <Omega@NO_SPAM. pl> wrote in message
news:Xn******** *************** ***********@213 .180.128.149...
int main() {
int socket_id = socket(PF_INET, SOCK_DGRAM,0);
struct sockaddr_in adres;
adres.sin_famil y = PF_INET;
adres.sin_port = htons(PORT_NR);
adres.sin_addr. s_addr = inet_addr("127. 0.0.1");
cli_msg msg;
msg.type = CMT_LOGIN;
sendto(socket_i d,&msg,sizeof(c li_msg),0,(stru ct sockaddr*)&adre s,sizeof
(struct sockaddr));
printf("wyslale m na 127.0.0.1\n");
ser_main_msg ser_msg;
unsigned int rozmiar;
Oops, your forgot to initialize 'rozmiar'.
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );
printf("otrzyma lem cos a adresu %s",inet_ntoa(a dres.sin_addr)) ;
printf("type = %d\n",ser_msg.t ype);
return 1; Server is rather long, but the inportant part is, the socket is datagram
socket:
-------------------------------------------------------------------
bzero(&klient1, sizeof(struct sockaddr_in));
int ret = recvfrom(sockid ,&msg_kli1,size of(cli_msg),0,( struct sockaddr*)
&klient1,&rozmi ar1);
Did you remember to initialize "rozmiar1"?
printf("receive d %s, %d, %d\n",inet_ntoa (klient1.sin_ad dr), ret,
errno);
if (msg_kli1.type == CMT_LOGIN) {
msg.type = SMMT_WAIT;
sendto(sockid,& msg,sizeof(ser_ main_msg),0,(st ruct sockaddr*)
&klient1,rozmia r1);
printf("send SMMT_WAIT to address %s\n",inet_ntoa
(klient1.sin_ad dr));
kli_nr++;
-------------------------------------------------------------------
serwer should print:
received 127.0.0.1, 16, 0
send SMMT_WAIT to addrress 127.0.0.1

but it prints:
reveived 0.0.0.0., 16, 0
send SMMT_WAIT to address 0.0.0.0

It happens only when I run the program on my computer, when I tried on
friends computer problem doesn't occur. Any ideas?


Passing an uninitalized value to 'recvfrom' results in unpredictable
behavior. Initialize it to the allocated length of the structure.

DS
Nov 21 '05 #3
Omega wrote:
I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.

The client's source code:

ser_main_msg ser_msg;
unsigned int rozmiar;
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );


You're not initializing 'rozmiar' to the length of 'adres'.

ssize_t recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, int *fromlen);

'fromlen' is value-result parameter meaning that recvfrom() uses it's
passed value and before returning modifies the contents of 'fromlen' to
indicate the actual size of the address stored there.

What this means to you is:

unsigned int rozmiar;
rozmiar = sizeof(adres); /* or rozmiar = sizeof(struct sockaddr_in); */
recvfrom(socket _id, &ser_msg, sizeof(ser_main _msg), 0, (struct
sockaddr*)&adre s, &rozmiar);

You have to initialize 'fromlen' (in your case rozmiar) to the proper
length before each call to recvfrom(). The reason it works sometimes is
because of any number of random possibilities as to what the contents
of 'rozmiar' are (since it's an auto variable) without you explicitly
initializing it.

Nov 23 '05 #4
Omega wrote:
I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.

The client's source code:

ser_main_msg ser_msg;
unsigned int rozmiar;
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );


You're not initializing 'rozmiar' to the length of 'adres'.

ssize_t recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, int *fromlen);

'fromlen' is value-result parameter meaning that recvfrom() uses it's
passed value and before returning modifies the contents of 'fromlen' to
indicate the actual size of the address stored there.

What this means to you is:

unsigned int rozmiar;
rozmiar = sizeof(adres); /* or rozmiar = sizeof(struct sockaddr_in); */
recvfrom(socket _id, &ser_msg, sizeof(ser_main _msg), 0, (struct
sockaddr*)&adre s, &rozmiar);

You have to initialize 'fromlen' (in your case rozmiar) to the proper
length before each call to recvfrom(). The reason it works sometimes is
because of any number of random possibilities as to what the contents
of 'rozmiar' are (since it's an auto variable) without you explicitly
initializing it.

Nov 23 '05 #5
Omega wrote:

Hi Alpha,
unsigned int rozmiar;
/* dont forget this */
rozmiar=sizeof( adres);
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );


Regards ... Rainer
Nov 23 '05 #6

Omega wrote:
I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.
[]
unsigned int rozmiar;
recvfrom(socket _id,&ser_msg,si zeof(ser_main_m sg),0,(struct sockaddr*)
&adres,&rozmiar );


rozmiar must be of type socklen_t and have to be initialized with the
length of your address structure prior the call to recvfrom

socklen_t rozmiar = sizeof adres;

see
http://www.opengroup.org/onlinepubs/.../recvfrom.html

Nov 23 '05 #7

Maxim Yegorushkin wrote:
rozmiar must be of type socklen_t and have to be initialized with the
length of your address structure prior the call to recvfrom

socklen_t rozmiar = sizeof adres;


To be 100% portable it should be socklen_t - but of course that's not
why it's failing here.

Nov 23 '05 #8
Omega wrote:

I have a problem with recvfrom(). Any ideas?


Yes.
This is off topic for comp.lang.c.
Followup set.

--
pete
Nov 23 '05 #9
Hi,
I have a problem with recvfrom(). I get a message and no error occurs,
but the struct sockaddr* from is not filled with sender's data.
ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
int flags, struct sockaddr *restrict address,
socklen_t *restrict address_len);
The 6th parameter /addrlen_len/ is an IN/OUT argument. It should be
initialized to the size of the structure pointed by the /address/
argument prior to calling recvfrom().

HTH,
Loic.


The client's source code:

#include "reversi.h"
-------------------------------------------------------------------
int main() {
int socket_id = socket(PF_INET, SOCK_DGRAM,0);
struct sockaddr_in adres; [snip] unsigned int rozmiar;
this line should be:
| unsigned int rozmiar = sizeof (adres);

Similarly in the server: -------------------------------------------------------------------
bzero(&klient1, sizeof(struct sockaddr_in));
the following line is missing:
| rozmiar1 = sizeof (klient1);
int ret = recvfrom(sockid ,&msg_kli1,size of(cli_msg),0,( struct sockaddr*)
&klient1,&rozmi ar1);

[...]

Nov 23 '05 #10

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

Similar topics

5
11683
by: Terry | last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a "recvFrom()" call and other thread sends a UDP packet to some address, that if the machine on the other end isn't up, that the "recvFrom()" call should just continue blocking. Right? What I'm seeing is that when I send a packet to a particular address that is not responding, my "recvFrom()" call throws an exception. I get "An existing connection was forcibly...
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 { ..............
1
5296
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is already written in file or not.. if it is not already entered into the file then a file is opened and that IP is entered. the problem is with this part.. if the filewrite() function is tested independently it is working but in this program...
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
0
2074
by: bndifek | last post by:
Hi I have a problem with raw socket. I want send SYN packet but I have a problem with checksum. When I send the packet to second komputer I see it in ethereal on the second computer( on the close port), but it shows that the packet has Incorect checksum. And the second komputer don't send packet to my kmoputer ( I have no reply ACK).I build RAW SOCKET like bellow: #include <netdb.h> #include <netinet/ip.h> #include <netinet/tcp.h>...
0
1227
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...
2
5607
by: kardon33 | last post by:
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. struct sockaddr_in a;
11
12461
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 2189 packets, which is not bad at all. But the server only handles 700 -- 870 packets, when it is...
7
4977
by: saee | last post by:
hello group, i'm trying to send a socket over udp. i'm doing it on same machine with loopback address. i wrote a server code n a client code. when i run them both, there should be a connection when i send text from client to server. client window shows sent complete n server window says waiting for connection that means recvfrom() is hanged or blocked. it does not go beyond recvfrom(). i tried connect() also from client. no luck. my pseudo...
0
8356
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
8783
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
8640
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...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
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
4198
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.