Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / MFC

Hello world application for socket programming in windows

Rate me:
Please Sign up or sign in to vote.
1.42/5 (12 votes)
20 Oct 20071 min read 58.1K   2.8K   13  
Application for newbies in the world of
// client.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include "winsock2.h"
#include "conio.h"

#define SERVER_PORT 12345
#define BUF_SIZE 4096 // block transfer size  
#define QUEUE_SIZE 10
#define IPAddress "127.0.0.1" // Local to the system - Loop back address

int _tmain(int argc, _TCHAR* argv[])
{
	WORD		wVersionRequested;
	WSADATA		wsaData;
	SOCKADDR_IN target; //Socket address information
	SOCKET		s;
	int			err;
	int			bytesSent;
	char		buf[10] = "hi hello";
	
	
	while(1) {
	//--- INITIALIZATION -----------------------------------
	wVersionRequested = MAKEWORD( 1, 1 );
	err = WSAStartup( wVersionRequested, &wsaData );

	if ( err != 0 ) {
		printf("WSAStartup error %ld", WSAGetLastError() );
		WSACleanup();
		return false;
	}
	//------------------------------------------------------
	
	//---- Build address structure to bind to socket.--------  
	target.sin_family = AF_INET; // address family Internet
	target.sin_port = htons (SERVER_PORT); //Port to connect on
	target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
	//--------------------------------------------------------

	
	// ---- create SOCKET--------------------------------------
	s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
	if (s == INVALID_SOCKET)
	{
		printf("socket error %ld" , WSAGetLastError() );
		WSACleanup();
		return false; //Couldn't create the socket
	}  
	//---------------------------------------------------------

	
	//---- try CONNECT -----------------------------------------
	if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
	{
		printf("connect error %ld", WSAGetLastError() );
		WSACleanup();
		return false; //Couldn't connect
	}
	//-----------------------------------------------------------
	
	//---- SEND bytes -------------------------------------------
	
	gets(buf);
	bytesSent = send( s, buf, 10, 0 ); 
	printf( "Bytes Sent: %ld \n", bytesSent );

	//------------------------------------------------------------
	closesocket( s );
	WSACleanup();
	}

	getche();
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions