Question

int acceptSocket;
struct sockaddr_in addr, client, dest;
char buf[256];
long rc, sentbytes;
int port = 18017;

int CreateSocket()
{



    if(rc!=0)

       {

         printf("socket failure code: %ld\n",rc);

         return 1;

       }

       else

       {

         printf("socket started!\n");

       }

       // Socket creation for UDP

       acceptSocket=socket(AF_INET,SOCK_DGRAM,0);

       if(acceptSocket==-1)

       {

         printf("Failure: socket creation is failed, failure code\n");

         return 1;

       }

       else

       {

         printf("Socket started!\n");

       }

     memset(&addr, 0, sizeof(addr));

     addr.sin_family=AF_INET;

     addr.sin_port=htons(port);

     addr.sin_addr.s_addr=htonl(INADDR_ANY);

     rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));

     if(rc==-1)

     {

       printf("Failure: listen, failure code:\n");

       return 1;

     }

     else

     {

       printf("Socket an port %d \n",port);

     }




       while(rc!=-1)
       {
         rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, sizeof(client));
         if(rc==0)
         {
           printf("Server has no connection..\n");
           break;
         }
         if(rc==-1)
         {
           printf("failure: recv, failure code\n");
           break;
         }
         XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );


            makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
            makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
            makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms

     //    buf[rc]='\0';
     //    printf("Client sendet: %s\n",buf);
     //    sprintf(buf2,"Du mich auch %s",buf);
     //    rc=sendto(connectedSocket,buf2,strlen(buf2),0);

       }


       close(acceptSocket);



       return 0;

     }

I created a socket for udp layer and it is waiting for connection from the client and later sending the data to the client. My program is getting crashed in listen mode. i.e: after bind :I am getting error as

 printf("Failure: listen, failure code:\n");

Could anyone tell me why? @JoachimPileborg: it is not working!! again the same problem. I am not able to comment there.

Was it helpful?

Solution

First of all you need to find actual error.you can do as follows

As bind man page

On error, -1 is returned, and errno is set appropriately.

The standard procedure in every POSIX call, there is an integer named errno that contains the value of the actual error. You can either check its value manually, or use primitives as perror or strerror

if (rc  == -1) 
{
    perror("socket error");
}

must have #include<errno.h>

Now bind fails many reason one of it like

Probably some other application is listening on that port. You can find this by running

netstat -a | grep "18017"

So if you found port in use then switch to another port number or simply kill that process which uses that port.

Also please update you while...loop in proper format like no need to compare rc to both -1 and 0 please do as follows

   while(1)
   {
        //try to receive some data, this is a blocking call
        if ((rc = recvfrom(acceptSocket, buf, 256, 0, (struct sockaddr *) &client, &client)) == -1)   // read datagram from server socket
        {
            break;
        }
       //rest of your code
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top