Hi!
I am receiving data from host, and transmitting data back to the same destination(same ip and port). For that I am using 'from' struct(to know who sent data to me).
But for transmit, I am using dedicated sockadr_in struct.
After recvfrom, 'from' struct is filled with source host information.
Before transmit, sockadr_in struct is correct(family, port, sin_addr).
For receive socket, I set timeout of 100 ms.
My code is:
//after connect and dhcp
sockaddr_in host;
unsigned char rx_buf[1500];
rx_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
unsigned long timeout = 100;
if(setsockopt( rx_socket, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT, &timeout, sizeof( timeout ) ) != 0)
return -1;
memset(&host, 0, sizeof(sockaddr_in));
host.sin_family = AF_INET;
host.sin_port = htons(1025);
host.sin_addr.s_addr = 0;
if (bind(rx_socket, (sockaddr*)&host, sizeof(sockaddr)) != 0)
{
closesocket(rx_socket);
return -1;
}
long length;
int sendlen;
socklen_t flen;
sockaddr_in from;
while(1)
{
int rcvlen = recvfrom(rx_socket, (char*)&rx_buf, sizeof(rx_buf), 0, (sockaddr*)&from, &flen);
if(rcvlen>0)
{
in_packet_counter++;
length = rcvlen;
host.sin_family = AF_INET;
host.sin_addr.s_addr = from.sin_addr.s_addr;
host.sin_port = from.sin_port;
sendlen = sendto(rx_socket, (char*)&rx_buf, length, 0, (sockaddr*)&host, sizeof(host));
if(sendlen != length)
{
return -1;
}
}
}//while
So, I am receiving packet, trying to answer, and first time I am calling sendto it returns with normal result(I don't know if packet is really sended). But next call to recvfrom returns with 0xFFFFFFC7;
And all of this working if I am using separate sockets for rx and tx..