[C] [Network] [UDP] recvfrom() failed while Binding localhost

This is a discussion on [C] [Network] [UDP] recvfrom() failed while Binding localhost within the Networking/Device Communication forums, part of the General Programming Boards category; Hi there, I might need a little help from some C expert in network programming ! I am binding a ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    [C] [Network] [UDP] recvfrom() failed while Binding localhost

    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
    Last edited by Sismon; 04-06-2010 at 07:20 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > number_of_sockets
    Do we assume this contains 5 ?

    > sockets[4]
    Do we assume this is an array with at least 5 elements?

    Do we assume your UDPSock() didn't print an error message, and didn't return -1?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Hi Salem,

    Oh yes, sorry if I miss some details, but as my program begins to have numbers of line I did not want to flood the post...

    So to give more details :


    >> number_of_sockets
    >Do we assume this contains 5 ?
    Code:
    int number_of_sockets=4;
    >> sockets[4]
    >Do we assume this is an array with at least 5 elements?
    Code:
    char sockets[4];
    And for the last one I am not sure of what you mean ?!? As it would return -1 if cannot "getaddr" or cannot "bind".

    But I have no such error at the moment.

    Please let me know if you would need more details, as I am not really sure what would help you ?

    Thanks for your comment.

    NB: I edit my first post

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    sockets[4]=UDPSock(Unicast,port);
    There is no member with index 4 in your array

    UDPSock returns int - char is too small to store this value
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Oh yes !!

    Such bad mistakes, and I did not even noticed it....

    What surprise me is that the compiler was not complaining at all...

    One socket assigned out of the range

    And a socket file descriptor as a char....

    Thank you very much to both of you !

    Now many other happy error to correct, but that's my part....MALLOC error and so on...

    Thanks !

    Sismon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  2. Blocking a server with hosts.
    By adrianxw in forum Tech Board
    Replies: 4
    Last Post: 03-14-2003, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21