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.
sockaddrinstead of asockaddr_intoaccept(). And your client code is misusingWSAGetLastError()on thesocket()call. And some of yourprintf()calls on both sides are missing\ncharacters so the output might not be getting flushed to the console.accept()is an old function. At the time it was created,sockaddrwas the only socket address struct available. Other the years, additional address types have been created, but the signature ofaccept()is locked in and cannot change. That is why it still takes asockaddr*parameter, but theaddrlenparameter specifies the actual size of the structure and it fills the structure data accordingly. The same goes forconnect(), where the first field of the structure indicates the actual address type being passed in, and thenamelenspecifies the actual structure size.WSAETIMEDOUT. You can't get that error fromaccept(). Please show your latest code, you are still doing something wrong.