Sample for winsock client/server programming?

Started by
1 comment, last by yungivan 21 years, 7 months ago
Hi all, is there any good sample of winsock programming for beginner? And what is the different between winsock 1.1 and 2.0? Thanks a lot!
Advertisement
The following code connects to a web-server, submits an HTTP get request (ie read) and retrieves the first part of the web-pages contents.


  //###################################################################################//#																					#//#			Chapter 5 - Receive Functionality										#		//#																					#//#						Demonstrates the recv() function							#//#																					#//#						Todd Barron, 10/15/2000										#//#						Multiplayer Game Programming								#//#						http://www.lostlogic.com									#//#																					#//# Required Libraries: ws2_32.lib													#//#																					#//###################################################################################// Standard Includes#include <iostream.h>#include <winsock.h>#include <stdio.h>//// ----> Main Program Function (REQUIRED)//void main(void){	SOCKET		skSocket;	sockaddr_in saServerAddress;	int			iPort = 80,iStatus;	WSADATA		wsaData;	WORD		wVersionRequested;	LPHOSTENT	lpHost;	char		szHost[128];	char		szSendBuffer[256];	char		szRecvBuffer[32768];	int			iBytesSent;	int			iBytesReceived;	// Init the host value, change this IP to whatever valid IP you wish	sprintf(szHost,"www.yahoo.com");	// Tell WinSock we want version 2	wVersionRequested = MAKEWORD( 2, 0 );	// Initialize the socket handle	skSocket = INVALID_SOCKET;	// Startup WinSock	iStatus = WSAStartup( wVersionRequested, &wsaData );	// Create the socket	skSocket = socket( AF_INET, SOCK_STREAM, 0 );		// Check if there was an error	if( skSocket == INVALID_SOCKET ) {		cout << "**ERROR** Could Not Create Socket" << endl;		// Clean up WinSock		WSACleanup();		exit(1);	}	cout << "<-- SOCKET CREATED -->" << endl;	// Initialize the server address data structure	memset(&saServerAddress,0,sizeof(sockaddr_in));	// Set this by default	saServerAddress.sin_family = AF_INET;	// Load the IP Address	saServerAddress.sin_addr.s_addr = inet_addr(szHost);	// If the host specified is not an IP Address we must look up the value	if( saServerAddress.sin_addr.s_addr == INADDR_NONE )	{		cout << "<-- LOOKING UP HOST IP -->" << endl;		// Get the host name		lpHost = gethostbyname(szHost);		// Check if we got something back		if (lpHost != NULL) {			// Load the server address with the host information			saServerAddress.sin_addr.s_addr = ((LPIN_ADDR)lpHost->h_addr)->s_addr;		}		else {			cout << "**ERROR** Could Not Locate Host" << endl;			// Clean up WinSock			WSACleanup();			exit(1);		}	}	// Set the Server Port	saServerAddress.sin_port = htons(iPort);	// Attempt to connect to the server	iStatus = connect(skSocket, (struct sockaddr*)&saServerAddress,sizeof(sockaddr));		// Check if there was an error	if( iStatus == SOCKET_ERROR ) {		cout << "**ERROR** Could Not Connect To Server" << endl;		// Clean up WinSock		WSACleanup();		exit(1);	}	cout << "<-- CONNECTED TO SERVER -->" << endl;	// Load the data to send	sprintf(szSendBuffer,"GET / HTTP/1.0\n\n");	// Send the HTTP Request	iBytesSent = send(skSocket,szSendBuffer,256,0);	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;		// Wait for incoming data	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);	cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;	// Output the data received	cout << szRecvBuffer << endl;	// Close the socket	closesocket(skSocket);	// Clean up WinSock	WSACleanup();}  



LostLogic
www.lostlogic.com
Author, Multiplayer Game Programming
Author, Strategy Game Programming with Direct X 9 (Not yet released)

LostLogicwww.GamerOutfit.comXBox 360 Community Games Reviews and NewsExisled - 2D/3D Shooter for XBox 360

winsock 1.1 is basically just an implementation of Berkley Sockets. It has a couple of differences to berkley sockets which were made so that it would be compatible with windows (because windows treats some of it''s data differently than linux etc. do)

winsock 2.0 has winsock 1.1 as a subset, but also adds a whole new set of functions that are specific to winsock (and not portable to other sockets supporting platforms). winsock 2.0 may, or may not improve the performance of the berkley sockets subset. I only use the 1.1 subset, but i use the 2.0 library, i figure it won''t be too much bigure, and it''s probably a little more stable and possibly faster than 1.1

This topic is closed to new replies.

Advertisement