I'm attempting to teach myself some networking programming in C++, but I'm running into some core issues that I can't seem to solve.
I have two programs that are in the same VS project- a client and a server. They don't exchange information, they simply connect and tell me when the connection is established. Here is the client file-
SERVER.CPP
#include "stdafx.h" using namespace std; int main() { //setting up WSA WSADATA wsaData; int WSAcheck = WSAStartup(MAKEWORD(2, 2), &wsaData); //checking WSA if (WSAcheck != 0) { printf("WSA couldn't start correctly\n"); pause(); exit; } //setting up the socket SOCKET serversock = socket(AF_INET, SOCK_STREAM, 0); //checking socket's validity if (serversock == INVALID_SOCKET) { printf("Socket isn't valid: %d\n", WSAGetLastError()); pause(); exit; } //setting up the sockaddr_in for bind() sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; server_sockaddr.sin_addr.s_addr = INADDR_ANY; server_sockaddr.sin_port = htons(25565); //binding the socket. int bindcheck = bind(serversock, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)); //checking to see if the bind worked, and calling the error if it doesn't if (bindcheck != 0) { printf("Bind failed: %d\n", WSAGetLastError()); pause(); exit; } //setting the socket to listen int listencheck = listen(serversock, 10); //checking to make sure the listen command was successful if (listencheck != 0) { printf("Listen failed: %d\n", WSAGetLastError()); pause(); exit; } //telling that the socket is being set to listen printf("Socket is ready to accept connections\n"); //accepting any incoming connections SOCKET clientsocket = accept(serversock, NULL, NULL); //checking the socket if (clientsocket == INVALID_SOCKET) { printf("Connecting socket isn't valid or has timed out: %d\n", WSAGetLastError()); pause(); exit; } //ending the program printf("fin\n"); pause(); } CLIENT.CPP
#include "stdafx.h" using namespace std; int main() { //setting up WSA WSADATA wsaData; int WSAcheck = WSAStartup(MAKEWORD(2, 2), &wsaData); //checking WSA if (WSAcheck != 0) { printf("WSA couldn't start correctly\n"); pause(); exit; } //setting up the socket SOCKET clientsock = socket(AF_INET, SOCK_STREAM, 0); //checking socket's validity if (clientsock == INVALID_SOCKET) { printf("Socket isn't valid: %d\n", WSAGetLastError()); pause(); exit; } //setting up the struct with the connection info sockaddr_in serverinfo; serverinfo.sin_family = AF_INET; serverinfo.sin_addr.s_addr = INADDR_ANY; serverinfo.sin_port = htons(25565); //connecting printf("Trying to connect\n"); connect(clientsock, (SOCKADDR *)&serverinfo, sizeof(serverinfo)); printf("Passed the connect function\n"); pause(); } STDAFX.H
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently #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 pause is defined as getchar(), to break the code to see what is going on.
The output for the client is -
Trying to connect
Passed the connect function
regardless of whether the server process is running.
The output for the server is
The socket is ready to accept connections
regardless of what I test, I cannot get it to pass that.
What am I doing wrong?
connectcall and add the results?