Solved

client failing to connect to server

Posted on 2000-06-03
4
314 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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