One half of a Windows Sockets test

This is the client side of a Windows Sockets test in C++.

#ifndef _WINSOCK_TEST_H
#define _WINSOCK_TEST_H

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

WSADATA wsaData;

int initWinsock();

#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include "winsock_test.h"

int initWinsock()
{
	int iResult;

	puts("initWinsock()");

	if (iResult = WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
		return 0;

	printf("WSAStartup(MAKRWORD(2, 2), 0x%x) returned %d\n", &wsaData, iResult);
	return 1
}
#include "winsock_test.h"

#define TCP_SOCKET_PORT 27015
#define TCP_SOCKET_ADDR "127.0.0.1"
#define BUFLEN		512

int client()
{
	int    iResult;
	int    recvbuflen       = DEFAULT_BUFLEN;
	struct addrinfo *result = NULL;
	struct addrinfo *ptr    = NULL,
	struct addrinfo hints;
	char   port[5];
	char   *sendbuf         = "this is a test";
	char   recvbuf[DEFAULT_BUFLEN];

	puts("client()");

	printf("ZeroMemory(0x%x, 0x%x)\n", &hints, sizeof (hints));
	ZeroMemory(&hints, sizeof (hints));
	hints.ai_family   = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	itoa(TCP_SOCKET_PORT, port, 10);
	printf("getaddrinfo(%s, %s, 0x%x, 0x%x)\n", TCP_SOCKET_ADDR, port, &hints, &result);
	if (iResult = getaddrinfo(TCP_SOCKET_ADDR, port, &hints, &result) != 0)
	{
		printf("getaddrinfo(%s, %s, 0x%x, 0x%x) returned %d\n", TCP_SOCKET_ADDR, port, &hints, &result, iResult);
		WSACleanup();
		return 1;
	}

	SOCKET ConnectSocket = INVALID_SOCKET;
	ptr = result;
	puts("socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP)\n");
	if (ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol) == INVALID_SOCKET)
	{
		printf("Error on socket(): %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	puts("connect(ConnectSocket, ptr->ai_addr, (int) ptr->ai_addrlen)\n");
	if (iResult = connect(ConnectSocket, ptr->ai_addr, (int) ptr->ai_addrlen) == SOCKET_ERROR)
	{
		printf("connect(ConnectSocket, ptr->ai_addr, (int) ptr->ai_addrlen) returned %d\n", iResult);
		closesocket(ConnectSocket);
		ConnectSocket = INVALID_SOCKET;
	}

	freeaddrinfo(result);

	if (ConnectSocket == INVALID_SOCKET)
	{
		puts("Socket connection failed.\n");
		WSACleanup();
		return 1;
	}

	printf("send(ConnectSocket, %s, (int) %d, 0)\n", sendbuf, (int) strlen(sendbuf));
	if (iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0) == SOCKET_ERROR)
	{
		printf("send failed: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	{

	printf("Bytes sent: %ld\n", iResult);

	puts("shutdown(ConnectSocket, SD_SEND)\n");
	if (iResult = shutdown(ConnectSocket, SD_SEND) == SOCKET_ERROR)
	{
		printf("shutdown failed: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}

	// receive data
	do
	{
		puts("recv(ConnectSocket, recvbuf, recvbuflen, 0)\n");
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0)
			printf("Bytes received: %d\n", iResult);
		else if (iResult == 0)
			puts("Connection closed"\n);
		else
			printf("recv failed: %d\n", WSAGetLastError());
	}
	while (iresult > 0);

	// no more data, shut down
	puts("shutdown(ConnectSocket, SD_SEND)\n");
	if (iResult = shutdown(ConnectSocket, SD_SEND) == SOCKET_ERROR)
	{
		printf("shutdown failed: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}

	closesocket(ConnectSocket);
	WSACleanup();
	return 0;
}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: