0

I am writing two programs (client and server) that are supposed to connect to one another. I have finally gotten all the individual bugs worked out, but when I got to test the client program (after starting and server), the connect function stops the progress of the code. I tested this with some simple text outputs.

Here is my server code (not really important)

#include "stdafx.h" using namespace std; int main() { WSAData WinSockData; WORD DLLVERSION; DLLVERSION = MAKEWORD(2, 1); int wsa_success = WSAStartup(DLLVERSION, &WinSockData); if (wsa_success != 0) { printf("Error %d while starting WSA\n", WSAGetLastError()); pause(); return 1; } SOCKET uc_socket; uc_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (uc_socket == INVALID_SOCKET) { printf("Error %d while creating socket\n", WSAGetLastError()); pause(); return 1; } SOCKADDR_IN ADDRESS; int AddressSize = sizeof(ADDRESS); ADDRESS.sin_addr.s_addr = INADDR_ANY; ADDRESS.sin_family = AF_INET; ADDRESS.sin_port = htons(444); int bind_success = bind(uc_socket, (SOCKADDR *)&ADDRESS, AddressSize); if (bind_success != 0) { printf("Error %d while binding socket\n", WSAGetLastError()); pause(); return 1; } int listen_success = listen(uc_socket, 12); if (listen_success != 0) { printf("Error %d while setting socket to listen\n", WSAGetLastError()); pause(); return 1; } SOCKET c_socket; SOCKADDR_IN client_sock; int client_sock_size = sizeof(client_sock); printf("Socket created; Set to listen for incoming connections\n"); c_socket = accept(uc_socket, (SOCKADDR *)&client_sock, &client_sock_size); if (c_socket == INVALID_SOCKET) { printf("Error %d while accepting connection request\n", WSAGetLastError()); pause(); return 1; } else { printf("Connection established!"); } pause(); return 0; } 

And my client code

#include "stdafx.h" using namespace std; int main() { int wsasuccessful = -1; WSAData WinSockData; WORD DLLVERSION; DLLVERSION = MAKEWORD(2, 1); wsasuccessful = WSAStartup(DLLVERSION, &WinSockData); if (wsasuccessful != 0) { printf("Error %d in client while starting WSA\n", WSAGetLastError()); pause(); return 1; } SOCKET client_socket; client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (WSAGetLastError() != 0) { printf("Error %d in client while instantiating socket\n", WSAGetLastError()); pause(); return 1; } SOCKADDR_IN client_connection; client_connection.sin_family = AF_INET; client_connection.sin_port = htons(444); client_connection.sin_addr.S_un.S_addr = INADDR_LOOPBACK; int connection_success = -1; printf("before connect()\n"); //for debugging connection_success = connect(client_socket, (SOCKADDR *)&client_connection, sizeof(client_connection)); printf("after conenct()\n"); //for debugging if (connection_success != 0) { printf("Error %d in client while instantiating socket\n", WSAGetLastError()); pause(); return 2; } printf("The client has found a server!"); printf("The last WSA error was: %d", WSAGetLastError()); pause(); return 0; } 

Both of the programs run without issue, but they don't connect to one another. I have configured my system to allow the listening and connecting.

The server output is - Socket created; Set to listen for incoming connections

The client output is - before connect()

The issue as I see it is that the client isn't recognizing the listening socket on port 444 and is holding up the program until the connection can be made.

I could bypass this with a nonblocking socket, but that wouldn't solve the underlying issue.

EDIT: forgot to include my stdafx.h

#pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <winsock2.h> #include <Ws2tcpip.h> #include <Inaddr.h> #include <string.h> #include <io.h> #include <Windef.h> #include <Ws2tcpip.h> #include <WinNT.h> #include <Windows.h> #include <tchar.h> void pause(void); //defined in stdafx.cpp 

Same for both server and client

EDIT 2: Finally received WSA error 10060 after changing SOCKADDR to SOCKADDR_IN in the accept() function in the server code.

The problem still remains, but now at least I know that it's the connection failing, and not the code.

5
  • No one has your stdafx.h Commented Feb 23, 2016 at 5:01
  • The server code is passing a sockaddr instead of a sockaddr_in to accept(). And your client code is misusing WSAGetLastError() on the socket() call. And some of your printf() calls on both sides are missing \n characters so the output might not be getting flushed to the console. Commented Feb 23, 2016 at 5:28
  • The params of the accept() function ask for a sockaddr according to the MSDN docs. If I remember correctly, they can be interchanged, but why are the docs asking for sockaddr rather than sockaddr_in? Commented Feb 23, 2016 at 23:35
  • accept() is an old function. At the time it was created, sockaddr was the only socket address struct available. Other the years, additional address types have been created, but the signature of accept() is locked in and cannot change. That is why it still takes a sockaddr* parameter, but the addrlen parameter specifies the actual size of the structure and it fills the structure data accordingly. The same goes for connect(), where the first field of the structure indicates the actual address type being passed in, and the namelen specifies the actual structure size. Commented Feb 24, 2016 at 0:16
  • Winsock error 10060 is WSAETIMEDOUT. You can't get that error from accept(). Please show your latest code, you are still doing something wrong. Commented Feb 24, 2016 at 0:19

1 Answer 1

1

connect is a blocking operation (by default). It is trying to connect and wont return until it works or fails. Sounds like it is going to time out , probably due to ports being blocked by windows firewall on the server machine. How long have you waited? Does it ever complete (with a failure)

Sign up to request clarification or add additional context in comments.

5 Comments

You really shouldn't be asking questions in an answer. This would have been better suited as a comment instead. Otherwise, it should be re-worded to state facts instead of solicit more input.
I have waited for a few minutes before and the system hasn't finished. It has never completed, nor has it even acknowledged that the other program was running.
did u disable windows firewall on the server machine
I am running both client and server on the same system, using INADDR_LOOPBACK (127.0.0.1) for the client connect address
If the firewall were blocking the connection, even a local connection, connect() would fail eventually when the socket times out internally, certainly within a couple of minutes at most, more likely within a few seconds. The only way to specify a custom timeout is to put the socket in non-blocking mode before calling connect() and then use select() to detect when the connection is complete or times out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.