I’m trying to implement a basic file transfer protocol, using UDP.
I’m using Beej’s Guide as a reference, and much of the code I will post is from there.
My program so far has the ‘talker’ send the name of the file it wants to the ‘receiver’.
From there, the reciever checks if the file exists, and if it does, it figures out the size of the file.
Now here is where I run into problems. I need the receiver to send the size of the file to the talker. You can see in my code (links below) how I implemented it. However, the talker just hangs, like it’s still waiting for something to be sent.
This makes me think the reciever needs some additional code to allow it to communicate back to the talker, and not just recieve data from it (I’m used to TCP, so excuse my lack of knowledge).
Could someone tell me what code I am missing, or if I am using the functions wrong? It’s difficult to follow Beej’s guide, and he does not provide an example of a two-way communication.
Thanks, and let me know if you need any more info.
Listener: http://pastebin.com/UL1xjDnP
Talker: http://pastebin.com/B2zrXPgZ
EDIT: Solved!
Thanks to cnicutar,
I was addressing the server in this code, when i should have addressed the client
if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
p->ai_addr, p->ai_addrlen)) == -1)
which should be changed to
if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
You’re not sending to the right peer. You are obtaining
pfromgetaddrinfoand then sending to it. So you are sending the message to yourself.You likely want to send it to “them”: