Thread: Client: Failed to connect

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    Client: Failed to connect

    Hi all, i want to know what happen to my program.That program failed to connect at 127.0.0.1 address.i want to know how to fix it and what caused it.

    Code:
    #include <stdio.h>
    #include <stdafx.h>
    #include <winsock2.h>
    
     
    
    int main()
    
    {
    
      // Initialize Winsock
    
      WSADATA wsaData;
    
      int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    
      if (iResult != NO_ERROR)
    
        printf("Client: Error at WSAStartup().\n");
    
      else
    
        printf("Client: WSAStartup() is OK.\n");
    
     
    
      // Create a SOCKET for connecting to server
    
      SOCKET ConnectSocket;
    
      ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
      if (ConnectSocket == INVALID_SOCKET)
    
      {
    
        printf("Client: Error at socket(): %ld.\n", WSAGetLastError());
    
        WSACleanup();
    
        return 0;
    
      }
    
      else
    
        printf("Client: socket() is OK.\n");
    
     
    
      // The sockaddr_in structure specifies the address family,
    
      // IP address, and port of the server to be connected to.
    
      sockaddr_in clientService;
    
      clientService.sin_family = AF_INET;
    
      clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
    
      clientService.sin_port = htons(55555);
    
     
    
      // Connect to server.
    
      if (connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
    
      {
    
        printf("Client: Failed to connect.\n");
    
        WSACleanup();
    
        return 0;
    
      }
    
      else
    
        printf("Client: connect() is OK.\n");
    
     
    
      // Declare and initialize variables.
    
      int bytesSent;
    
      int bytesRecv = SOCKET_ERROR;
    
      char sendbuf[100] = "Client: Sending some data.";
    
      char recvbuf[100] = "";
    
     
    
      while(bytesRecv == SOCKET_ERROR )
    
      {
    
        bytesRecv = recv(ConnectSocket, recvbuf, 100, 0);
    
        if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
    
        {
    
          printf("Client: Connection Closed.\n");
    
          break;
    
        }
    
        else
    
        {
    
           printf("Client: recv() is OK.\n");
    
           printf("Client: Bytes received: %ld\n", bytesRecv);
    
        }
    
      }
    
     
    
      // Send and receive data.
    
      bytesSent = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
    
      printf("Client: Bytes sent: %ld\n", bytesSent);
    
      WSACleanup();
      getchar();
      return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    It may be a firewall issue. Is port 55555 being blocked by your firewall?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Or are you trying to connect to your router? Maybe it doesn't listen on that port.
    172.0.0.1 looks like a router adress, if it uses telnet you will have to connect through port 23

    Niara

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    172.0.0.1 looks like a router adress
    I think you misread: the address in question is 127.0.0.1, i.e., localhost.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Do you have a server listening on port 55555, and is it configured to accept connections from at least 127.0.0.1?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    laserlight: I confused it with the gateway.
    In that case Galvatron should have a local server listening on port 55555

    Another thing, I think usually the client sends a request and then gets a response; the code first recvs and after sends. If the server doesn't send data immediatelly after the accept, the client will rest waiting infinte time.

    Niara

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Another thing, I think usually the client sends a request and then gets a response; the code first recvs and after sends. If the server doesn't send data immediatelly after the accept, the client will rest waiting infinte time.
    Do you mean in general? For TCP, either side (server/client) can send first. For example, in HTTP, the sends the request, and the server answers. Other protocols, the server may open with a welcome message of sorts.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Cactus_Hugger: yes, I've seen it now; the problem is 'That program failed to connect at 127.0.0.1 address', so my last post is totally wrong; I was assuming that is trying to connect to an http server.

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  2. Getting info on a client when they connect to the server
    By Finchie_88 in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-01-2005, 07:12 AM
  3. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. Problem with my client and connect()
    By Thantos in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-02-2003, 05:38 PM