Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a udp client to receive a simple text file,the code builds fine but gives me a blank console when run,after some observation I found out that the problem is occuring at the time of recetion,so I am pasting that part of my code,could someone please guide me...

C++
size_t data=0;
if(data=recvfrom(sd, file_buffer, sizeof(file_buffer), 0, (struct sockaddr *) &server, &server_length)<0)
{
    printf("Error receiving file.");
    exit(1);
}

if(data==sizeof(file_buffer))
{
    printf("Received Data:[%s]",file_buffer);
}


the point where I am receiving data:
data=recvfrom(sd, receivedData, sizeof(receivedData)-1, 0, (struct sockaddr *) &server,  &server_length);
receivedData[data]='\0';


the point where I am writing data to the file:
if(fwrite(receivedData, 1, data, fp)!=data);
       {
           printf("Error writing file! \n");
           exit(1);
       }
Posted
Updated 5-Apr-13 22:59pm
v4
Comments
Richard MacCutchan 6-Apr-13 3:45am    
You should not be using UDP to receive data such as this. UDP datagrams do not have guaranteed delivery or order; switch to TCP.

1 solution

At first I thought the whole problem was in the first if condition, because it is somewhat busy with calling recvfrom(), assigning the return value to data and then doing a less than comparison.
But after looking at it for a while, I realized you have size_t data = 0;, but size_t is an unsigned integer type, so it will never have a value less than zero. My guess is that you actually do get a negative return value from recvfrom(), but it never gets caught because of this.

It looks like you should change it to int data = 0; and then see if you land on exit(1);.

I wish I could do more, but that is tough without seeing more of the code, so try that change out and do some troubleshooting in the code. Outputting the return value of recvfrom() when it fails would be helpful.

Soren Madsen
 
Share this answer
 
v2
Comments
Aayman Khalid 6-Apr-13 2:52am    
as you said I've changed the data type of data to "int" and well it appears that the result is less than zero...
SoMad 6-Apr-13 2:56am    
Well, this is progress. Now you have to find out why it fails. What is the return value?

Soren Madsen
Aayman Khalid 6-Apr-13 3:16am    
the return value is "-1" so basically the client is not receiving the file,when I send some string from the server side the client receives it,so I thought that I will send the contents of the text file to client and will first display it on the console but here rises the issue!
SoMad 6-Apr-13 4:12am    
Yes, that is the value of SOCKET_ERROR. You need to call WSAGetLastError() to get the actual error. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120(v=vs.85).aspx[^].

Soren Madsen
Aayman Khalid 6-Apr-13 4:27am    
finally received the data:) but there is still one problem:although the file gets written, still I get on the console:"Error writing file" and I also get a little bit of useless data although I set the receiving buffer to zero before receiving the text file...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900