Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

client failing to connect to server

Posted on 2000-06-03
4
Medium Priority
?
327 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
[X]
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
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 100 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.
Suggested Courses

610 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