Hi there,
I might need a little help from some C expert in network programming !
I am binding a UDP socket to localhost ("127.0.0.1"), this is recieving all the messages sent as it should, but my problem is that if I press ENTER, it makes the socket failed :
recvfrom() failed: Socket operation on non-socket
I have other sockets bind to multicast addresses which work fine, and if I comment out the Unicast socket, I can press ENTER when the program is running.
Here is how I am binding my socket :
Code:
int UDPSock(const char* ip_addr,const char* port)
{
int sockfd;
struct sockaddr_storage Addr;
memset(&Addr, 0, sizeof(Addr));
if (get_addr(ip_addr, port, PF_UNSPEC,SOCK_DGRAM, &Addr) <0)
{
fprintf(stderr, "get_addr error:: could not find multicast address=[%s] port=[%s]\n", multicast_ip, port);
return -1;
}
//Create and bind the socket
sockfd = socket(Addr.ss_family, SOCK_DGRAM, 0);
if (bind(sockfd, (struct sockaddr *)&Addr, sizeof(Addr)) < 0)
{
perror("bind error:: ");
close(sockfd);
return -1;
}
return sockfd;
}
Code:
int main()
{
int number_of_sockets=4;
char sockets[4];
sockets[0]=UDPMulicastSock(ipv4,port);
sockets[1]=UDPMulicastSock(ipv4_2,port);
sockets[2]=UDPMulicastSock(ipv4_3,port);
sockets[3]=UDPMulicastSock(ipv6,port);
sockets[4]=UDPSock(Unicast,port);
while(1)
{
int largest = 0;
FD_ZERO(&readReadySet);
for (i = 0; i < number_of_sockets; ++i)
{
FD_SET(sockets[i], &readReadySet);
if (largest < sockets[i])
{
largest = sockets[i];
}
}
rc = select(largest + 1, &readReadySet, NULL, NULL, NULL);
if (rc == -1)
{
/* select( ) error */
}
else if (rc == 0)
{
/* No sockets ready */
}
else
{
for (i = 0; i < number_of_sockets; ++i)
{
if (FD_ISSET(sockets[i], &readReadySet))
{
/* Clear the receive buffers & structs */
memset(recv_str, 0, sizeof(recv_str));
from_len = sizeof(from_addr);
memset(&from_addr, 0, from_len);
/* Block waiting to receive a packet */
recv_len = recvfrom(sockets[i], recv_str, MAX_LEN, 0,(struct sockaddr*)&from_addr, &from_len);
if(recv_len<0)
{
printf("\nrecv_len==%i",recv_len);
perror("recvfrom() failed");
exit(1);
...}
I might have a bug somewhere which I do not find, or some behaviour of socket that I do not know/understand because I was not suspecting that pressing ENTER would be seen as a socket entry.
Thanks by advance for your help, or even to have read me.
Regards,
Sismon