Client: Failed to connect [Archive] - C Board

PDA

View Full Version : Client: Failed to connect


Galvatron
04-23-2008, 09:11 PM
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.

#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;

}

BobS0327
05-04-2008, 06:42 AM
It may be a firewall issue. Is port 55555 being blocked by your firewall?

Niara
05-04-2008, 08:01 AM
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

laserlight
05-04-2008, 08:06 AM
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.

rags_to_riches
05-04-2008, 09:22 AM
Do you have a server listening on port 55555, and is it configured to accept connections from at least 127.0.0.1?

Niara
05-04-2008, 11:36 AM
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

Cactus_Hugger
05-04-2008, 05:56 PM
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.

Niara
05-12-2008, 03:40 PM
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