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_family = 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_id,&msg,sizeof(cli_msg),0,(struct sockaddr*)&adres,sizeof
(struct sockaddr));
printf("wyslalem na 127.0.0.1\n");
ser_main_msg ser_msg;
unsigned int rozmiar;
recvfrom(socket_id,&ser_msg,sizeof(ser_main_msg),0 ,(struct sockaddr*)
&adres,&rozmiar);
printf("otrzymalem cos a adresu %s",inet_ntoa(adres.sin_addr));
printf("type = %d\n",ser_msg.type);
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,sizeof(cli_msg),0,(struc t sockaddr*)
&klient1,&rozmiar1);
printf("received %s, %d, %d\n",inet_ntoa(klient1.sin_addr), ret,
errno);
if (msg_kli1.type == CMT_LOGIN) {
msg.type = SMMT_WAIT;
sendto(sockid,&msg,sizeof(ser_main_msg),0,(struct sockaddr*)
&klient1,rozmiar1);
printf("send SMMT_WAIT to address %s\n",inet_ntoa
(klient1.sin_addr));
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 9 8435
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_Keith) 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.
"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_family = 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_id,&msg,sizeof(cli_msg),0,(struct sockaddr*)&adres,sizeof (struct sockaddr)); printf("wyslalem 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,sizeof(ser_main_msg),0 ,(struct sockaddr*) &adres,&rozmiar); printf("otrzymalem cos a adresu %s",inet_ntoa(adres.sin_addr)); printf("type = %d\n",ser_msg.type); 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,sizeof(cli_msg),0,(struc t sockaddr*) &klient1,&rozmiar1);
Did you remember to initialize "rozmiar1"?
printf("received %s, %d, %d\n",inet_ntoa(klient1.sin_addr), ret, errno); if (msg_kli1.type == CMT_LOGIN) { msg.type = SMMT_WAIT; sendto(sockid,&msg,sizeof(ser_main_msg),0,(struct sockaddr*) &klient1,rozmiar1); printf("send SMMT_WAIT to address %s\n",inet_ntoa (klient1.sin_addr)); 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
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,sizeof(ser_main_msg),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*)&adres, &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.
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,sizeof(ser_main_msg),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*)&adres, &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.
Omega wrote:
Hi Alpha, unsigned int rozmiar;
/* dont forget this */
rozmiar=sizeof(adres);
recvfrom(socket_id,&ser_msg,sizeof(ser_main_msg),0 ,(struct sockaddr*) &adres,&rozmiar);
Regards ... Rainer
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,sizeof(ser_main_msg),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
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.
Omega wrote: I have a problem with recvfrom().
Any ideas?
Yes.
This is off topic for comp.lang.c.
Followup set.
--
pete
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,sizeof(cli_msg),0,(struc t sockaddr*) &klient1,&rozmiar1);
[...] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |