Solved

client failing to connect to server

Posted on 2000-06-03
4
317 Views
Last Modified: 2011-09-20
/*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!!
0
Comment
Question by:sanmath
4 Comments
 
LVL 4

Expert Comment

by:saurabh_dasgupta
ID: 2884865
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
 
LVL 5

Expert Comment

by:jason987
ID: 2885043
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
 
LVL 1

Expert Comment

by:Ilango
ID: 2886871
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
 

Accepted Solution

by:
rbuchnik earned 50 total points
ID: 3172673
#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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now