Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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.");
    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 5-Apr-13 20:23pm
Edited 5-Apr-13 23:59pm
v4
Comments
Richard MacCutchan at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Aayman Khalid at 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 at 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 at 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 at 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 at 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 at 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 at 6-Apr-13 5:00am
   
I've uploaded the relevant code.
SoMad at 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");
exit(1);
}
}
 
Soren Madsen
Aayman Khalid at 10-Apr-13 1:19am
   
I removed the semicolon but the result is still the same...
SoMad at 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 at 15-Apr-13 1:02am
   
the code is fine now,Thank you so much:)
SoMad at 15-Apr-13 1:09am
   
Great. Glad I could help.
 
Soren Madsen
H.Brydon at 23-Apr-13 23:19pm
   
+5 for the answer and the extra effort.
SoMad at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 6 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100