Click here to Skip to main content
15,064,485 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...

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

    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);

the point where I am writing data to the file:
if(fwrite(receivedData, 1, data, fp)!=data);
           printf("Error writing file! \n");
Updated 5-Apr-13 22:59pm
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
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[^].

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...
SoMad 6-Apr-13 4:44am
Unfortunately it is quite difficult for us to speculate what might be your problem without seeing the code.

Soren Madsen
Aayman Khalid 6-Apr-13 5:00am
I've uploaded the relevant code.
SoMad 6-Apr-13 5:25am
Alright. Well, as Richard pointed out, UDP does not have guaranteed delivery, so you could be missing packets. However, I believe I see the problem.

You have a semicolon at the end of the if statement that writes to the file. Remove that semicolon. You should also make sure data has a valid value.

if (data > 0)
if(fwrite(receivedData, 1, data, fp)!=data)
printf("Error writing file! \n");

Soren Madsen
Aayman Khalid 10-Apr-13 1:19am
I removed the semicolon but the result is still the same...
SoMad 13-Apr-13 5:31am
Sorry about the delay. I was pretty sure that semicolon was the problem. Tell me something, are you sure the file is open when you write to it?

Soren Madsen
Aayman Khalid 15-Apr-13 1:02am
the code is fine now,Thank you so much:)
SoMad 15-Apr-13 1:09am
Great. Glad I could help.

Soren Madsen
H.Brydon 23-Apr-13 23:19pm
+5 for the answer and the extra effort.
SoMad 23-Apr-13 23:50pm
Thanks (x3) Harvey.

Soren Madsen

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