user2542813
user2542813

Reputation: 333

UDP client/server send data but can't read

I'm trying to send data from one application to another over the LAN, I can send the data (sendto returns the number of bytes expected) but I can't read the data. I know that because I'm using select to test the read operation and it returns false. Here is the code used to create the client and the server:

Creating the socket

sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)

Binding to a port:

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;

bind(sock_fd, (struct sockaddr*)&addr, sizeof(sockaddr));

to send the data to the other machine I use:

o_addr.sin_family = family;
o_addr.sin_port = port;
o_addr.sin_addr.s_addr = inet_addr("other_side_ip_address_here");
uint32 sent;    

sent = sendto(sock_fd, data, length, 0, (struct sockaddr*)&o_addr, sizeof(struct sockaddr))

to read the data from the other machine I use:

struct sockaddr_in o_addr;
uint32 read;
int l = sizeof(struct sockaddr);
read = recvfrom(sock_fd, data, length, 0, (struct sockaddr*)&o_addr, &l)

and to test if there is some data to read I use:

int r;
struct timeval tv = { 0, 0 };
fd_set fds;

FD_ZERO(&fds);
FD_SET(sock, &fds);

r = select(sock + 1, &fds, NULL, NULL, &tv);

if (FD_ISSET(sock, &fds))
    return true;
return false;

The sockets are on block mode, the tests are being performed in two machines. Thanks for attention

Upvotes: 0

Views: 563

Answers (1)

Oleg Andriyanov
Oleg Andriyanov

Reputation: 5289

My guess is that the bind call on a receiving socket fails for some reason. Try adding some error checks.

Upvotes: 1

Related Questions