client failing to connect to server

/*This program is about client/server application.Here a server
 is listening on port 1000h. When a client connects to the server,
 the server returns the system time to the client.*/

#include<winsock2.h>
#include<time.h>
#include<stdio.h>

void main()
{
    SOCKET serversocket,ser1;
      char buf[100];
      int s3;
    WORD w;
      WSADATA wsd;
      
      struct tm *newtime;
      char am_pm[] = "AM";
      time_t long_time;

    struct sockaddr_in s1;
   
   int d1,d2;

  /* creating a socket that is bound to the service provider */
   
   serversocket = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
   
   w = MAKEWORD(2,0);

   if ((WSAStartup(w,&wsd))!=0)
   {
         printf("failure");
   }
   
   s1.sin_family = AF_INET;

   /* port to which the server is connected */
   s1.sin_port = htons (1000);
   
   s1.sin_addr.s_addr = inet_addr("192.168.100.11");
   
   /*  associate server to the local address   */
   d1= bind(serversocket,(struct sockaddr*)&s1,sizeof(s1));
     
   //printf("socket =%ld\n",serversocket);
   //printf("BIND = %1d\n",d1);

         time( &long_time );                /* Get time as long integer. */
    newtime = localtime( &long_time ); /* Convert to local time. */

    if( newtime->tm_hour >= 12 )        /* Set up extension. */
            strcpy( am_pm, "PM" );
    if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
            newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
    if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight */
            newtime->tm_hour = 12;
      strcpy(buf,asctime( newtime ));
      printf( "%.19s %s\n", asctime( newtime ), am_pm );
      printf("The system time is %s",buf);
 
  while(1)
   {
        d2=listen(serversocket,SOMAXCONN);
            s3 = sizeof(s1);
            ser1 = accept(serversocket,(struct sockaddr*)&s1,&s3);
            recv(serversocket ,buf,sizeof(buf),0);
          send(serversocket ,buf,sizeof(buf),0);
            
  }
}

the bind function returns the value -1 hence the server is not getting bound to the socket... please help with all possible answers!!
sanmathAsked:
Who is Participating?
 
rbuchnikCommented:
#include<winsock2.h>
#include<time.h>
#include<stdio.h>

void main()
{
    SOCKET serversocket,ser1;
char buf[100];
int s3;
    WORD w;
WSADATA wsd;

struct tm *newtime;
char am_pm[] = "AM";
time_t long_time;

    struct sockaddr_in s1;
   
   int d1,d2;

  /* creating a socket that is bound to the service provider */
   
   serversocket = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
   
   w = MAKEWORD(2,0);

   if ((WSAStartup(w,&wsd))!=0)
   {
   printf("failure");
   }
   
   s1.sin_family = AF_INET;

   /* port to which the server is connected */
   s1.sin_port = htons (1000);
   
   s1.sin_addr.s_addr = inet_addr("192.168.100.11"); ->here try 127.0.0.1
   
   /*  associate server to the local address   */
   d1= bind(serversocket,(struct sockaddr*)&s1,sizeof(s1));
       
   //printf("socket =%ld\n",serversocket);
   //printf("BIND = %1d\n",d1);

    time( &long_time );                /* Get time as long integer. */
    newtime = localtime( &long_time ); /* Convert to local time. */

    if( newtime->tm_hour >= 12 )        /* Set up extension. */
            strcpy( am_pm, "PM" );
    if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
            newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
    if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight */
            newtime->tm_hour = 12;
strcpy(buf,asctime( newtime ));
printf( "%.19s %s\n", asctime( newtime ), am_pm );
printf("The system time is %s",buf);
   
  while(1)
   {
        d2=listen(serversocket,SOMAXCONN);
s3 = sizeof(s1);
ser1 = accept(serversocket,(struct sockaddr*)&s1,&s3);
recv(serversocket ,buf,sizeof(buf),0);
     send(serversocket ,buf,sizeof(buf),0);

  }
}
0
 
saurabh_dasguptaCommented:
Can you try these lines of code just before doing bind()

//code begins here


    int opt_val=TRUE;
    if (setsockopt(
                serversocket,
                SOL_SOCKET,
                SO_REUSEADDR,
                (char*)&opt_val,
                sizeof(int))!=0)
        {
        //an error has occurred, use WSAGetLastError
        }
    else
        {
        //setsockopt worked fine , go ahead with bind()
        }        

//code ends here
0
 
jason987Commented:
Here's the begining of a simple tcp listen server...  Hope it helps...  


void main (int argc, char **argv)
{
      SOCKET s;
      SOCKET cs;
      SOCKADDR_IN sa;
      SOCKADDR_IN csa;
      WSADATA wd;
      WORD wdVersion;
      PROTOENT *pe;
      char inBuffer[BUFSIZE];
      char outBuffer[BUFSIZE];
      char manBuffer[BUFSIZE];
      int rt;
      int port;
      
      if (argc != 3)
      {
            printf("Usage::::\n %s [this mach's ip] [port]\n...This is the server side\n", argv[0]);
            return;
      }
      //Reset the buffers
      memset(inBuffer, 0, sizeof(inBuffer));
      memset(outBuffer, 0, sizeof (inBuffer));
      //Set up version
      wdVersion = MAKEWORD( 2, 1.1);
      WSAStartup(wdVersion, &wd);

      pe = getprotobyname("tcp");
      s = socket (AF_INET, SOCK_STREAM, pe->p_proto);

      if(s == INVALID_SOCKET)
      {
            printf("Error calling socket()\n");
            printf("Error code:%d\n", WSAGetLastError());
            WSACleanup();
            return;
      }
      port = atoi(argv[2]);
      sa.sin_family = AF_INET;
      sa.sin_port = htons(port);
      sa.sin_addr.s_addr = inet_addr(argv[1]);

      rt = bind (s, (struct sockaddr *)&sa, sizeof ( struct sockaddr ));
      if (rt == SOCKET_ERROR)
      {
            printf("Error binding socketed\n");
            printf("Error code:%d\n", WSAGetLastError());
            WSACleanup();
            return;
      }

      printf("Socket availble and bound...\n");

      rt = listen(s, SOMAXCONN);
.......

}
0
 
IlangoCommented:
Hi Sanmath,

          You mayn't sure whether there is any other application is using that Port(1000) or not. Like Gupta said you can check whether you can reuse that port. Otherwise try using some other number. Best thing you have to do is make use of WSAGetLastError() fucntion to identify the error. This will help you to fix the error easily.

Best Of Luck.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.