0

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?

1
  • Can you check the return value for the connect call and add the results? Commented May 3, 2016 at 0:26

1 Answer 1

3

You have an error in the client. This is bad:

serverinfo.sin_addr.s_addr = INADDR_ANY; 

Instead of INADDR_ANY you have to write the server IP address, maybe 127.0.0.1 if they run in the same server, in binary format. Use this function to convert from text IP address to binary IP address:

INT WSAAPI InetPton( _In_ INT Family, _In_ PCTSTR pszAddrString, _Out_ PVOID pAddrBuf ); 

https://msdn.microsoft.com/en-us/library/windows/desktop/cc805844(v=vs.85).aspx

The code would be:

if(InetPton(AF_INET, "192.168.0.1", (void*)&serverinfo.sin_addr.s_addr) <= 0) { //error } 
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, that makes sense. Does INADDR_ANY work with the server in the sense that a connection attempt on any local interface to port XXXX will be received by the process, or does INADDR_ANY mean just 0.0.0.0? EDIT: Ah, I remember this function now- I had a real struggle with this a few weeks ago trying to get the data types in the right form. Thanks for the example!
@ThomasWoods, your comment's first 2 lines are correct!
I'm having issues with your second section of code with the InetPton function- The docs say that the second parameter needs to be a pointer to a const TSTR. I've defined unicode, so I'm using the follow code to but the binary address into the buffer specified by the third parameter in InetPton()- I put it on pastebin for formatting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.