LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question about Sockets Programming (https://www.linuxquestions.org/questions/programming-9/question-about-sockets-programming-234673/)

sibtay 09-24-2004 06:11 AM

Question about Sockets Programming
 
Hi

I am using recvfrom function to get data from the destination

The underlying protocol for communication is UDP...

Some times this function works properly while most of the times it causes
segmentation faults. Following is my code

char *rec_data=new char [800];

j=recvfrom(sourceDesc,rec_data,800,0,(struct sockaddr*)&Source,(socklen_t*)sizeof(Source));

I have been using this code since about 2 months now..and it never caused any problems...but now suddenly its malfunctioning

deiussum 09-24-2004 08:29 AM

The problem is probably your last parameter. recvfrom attempts to fill in that value with the size of your Source variable, but you are passing a pointer to a temporary variable... Try to change it like so:

Code:

char *rec_data=new char [800];
socklen_t iSize = sizeof(Source);

j=recvfrom(sourceDesc,rec_data,800,0,(struct sockaddr*)&Source,&iSize);


Mara 09-24-2004 05:04 PM

From recvfrom man page (about 6th parameter):
Quote:

The argument fromlen is a value-result parameter, initialized to the size of the buffer associated with from, and modified on return to indicate the actual size of the address stored there.
So it must be as deiussum shows.


All times are GMT -5. The time now is 02:20 AM.