Advertisement

Retrieving IP - Listening Server

Started by December 22, 2005 07:05 PM
1 comment, last by blanky 19 years, 2 months ago
Hey guys, I'm making a listening server in windows (WinSock). So anyways, it's a listening server and so the IP address of the 'client' is set as INADDR_ANY, to allow anyone to connect. Anyways, my main goal is to, once the client connects, send them their IP address. I understand the send part, I could do that myself. However, it's getting their IP address which I dont seem to get. Here's my code:

#pragma comment(lib, "Ws2_32.lib")

#define NETWORK_ERROR -1
#define NETWORK_OK     0

#include <Winsock2.h>
#include <iostream>
using namespace std;

void ReportError(int errorCode, const char *whichFunc)
{
	char errorMsg[92];				// Declare a buffer to hold
									// the generated error message
	ZeroMemory(errorMsg, 92);		// Automatically NULL-terminate the string

	// The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
	sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
	MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
}

int main(int argc, char **argv)
{
	WORD sockversion;
	WSADATA wsadata;
	int nret;

	sockversion = MAKEWORD(1, 1);
	WSAStartup(sockversion, &wsadata);

	SOCKET listeningsocket;

	listeningsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (listeningsocket == INVALID_SOCKET)
	{
		nret = WSAGetLastError();
		ReportError(nret, "socket()");
		WSACleanup();
		return NETWORK_ERROR;
	}

	SOCKADDR_IN serverinfo;
	serverinfo.sin_family = AF_INET;
	serverinfo.sin_addr.s_addr = INADDR_ANY;
	serverinfo.sin_port = htons(711);

	nret = bind(listeningsocket, (LPSOCKADDR)&serverinfo, sizeof(struct sockaddr));

	if (nret == SOCKET_ERROR)
	{
		nret = WSAGetLastError();
		ReportError(nret, "bind()");
		WSACleanup();
		return NETWORK_ERROR;
	}

	nret = listen(listeningsocket, 10);

	if (nret == SOCKET_ERROR)
	{
		nret = WSAGetLastError();
		ReportError(nret, "listen()");
		WSACleanup();
		return NETWORK_ERROR;
	}

	SOCKET theclient;
	theclient = accept(listeningsocket, NULL, NULL);

	if (theclient == INVALID_SOCKET)
	{
		nret = WSAGetLastError();
		ReportError(nret, "accept()");
		WSACleanup();
		return NETWORK_ERROR;
	}

	cout << "Client connected" << endl;
	cout << "Client's IP address is: " << serverinfo.sin_addr.s_addr << endl;

	closesocket(theclient);
	closesocket(listeningsocket);
	WSACleanup();
	system("PAUSE");
	return NETWORK_OK;
}

At first, by logic I just sent serverinfo.sin_addr.s_addr, but it just returned 0 heh, any help or pointers would be nice, thanks guys.
The serverinfo you print is the address you're binding on on the server -- INADDR_ANY means 0, which means all available interfaces on the server.

The IP address of the client is returned from accept(), except you pass in NULL there, so you're throwing that data away. You can, alternately, get the address with getpeername().

Last: you should always start WinSock with version 2,2. The lower-numbered versions have some annoying bugs, and anything from Windows 98 and up support 2,2 anyway.
enum Bool { True, False, FileNotFound };
Advertisement
Yeah I was going to put 2.2 but forgot lol, and yeah I knew about the INADDR_ANY, I'm dumb I forgot, thanks bud!

This topic is closed to new replies.

Advertisement