Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

client failing to connect to server

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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