#include 
#include 
#include 
#include 
#include 

#include 

#define SPORT 3490 //Server local port
#define CPORT 3480 //client local port
#define MaxClts 1 //Maximum number of accepted clients
#define MaxMsg 101 //Message maximum length

void main(){

	//for comptability with windows platform
	WSADATA wsaData;
	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){
		fprintf(stderr,"WSAStartup Failed.\n");
		exit(1);
	}
	//.............

	//Mode of opertion
	int mode = 1;
	do
	{
		cout<<"\nPress : 1 - for Server";
		cout<<"\n        2 - for Client";
		cout<<"\n        3 - to Quit\n";
		cin>>mode;
    }
	while ( mode !=1 && mode !=2 && mode !=3 );
	//.............

	//Initialization
	
	int sockfd; //socket handlers 
	struct sockaddr_in my_addr; //My Local Addy
	struct sockaddr_in their_addr; //Others Addy
	char message[MaxMsg-1]; //message being sent or received
	char IP[16];		//server ip .. will be entered by user later on


	//---------------SERVER------------------------------------------

	if(mode == 1)
	{

		printf("\n\nServer");
		printf("  \n--------\n\n");

		//socket()
		if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
		{
			printf("\n\nCan not initialize a socket !\n");
			getch();
			exit(1);
		}
		
		printf("\n\nSocket Declared...");
		
		//address initializing
		
		my_addr.sin_family = AF_INET;
		my_addr.sin_port = htons(SPORT);           //Server Port = 3490
		my_addr.sin_addr.s_addr = INADDR_ANY;      //My IP
		memset(&(my_addr.sin_zero),'\0',8);        //Zero padding

		//bind()
		if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1)
		{
			printf("\n\nCan not bind !\t");
			getch();
			exit(1);
		}
		printf("\nSocket Binded\t");

		//listen()
		if(listen(sockfd,MaxClts)==-1)
		{
			printf("\n\nCan not listen !\n");
			getch();
			exit(1);
		}
		printf("\nListening ...");

		//accept()
		int sin_size;
		sin_size = sizeof(struct sockaddr_in);
		sockfd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);

	
		
		//getting data from the other side
		printf("\n\nListening to %s\t",inet_ntoa(their_addr.sin_addr));


		do
		{
				//receive()
				int nbytes;
				if((nbytes=recv(sockfd,message,MaxMsg-1,0))==-1)
				{
					printf("\n\nNot able to receive !\t");
					getch();
					exit(1);
				}
				if(nbytes==0)
				{
					printf("\n\nOther side closed connection\t");
					getch();
				}
				else
				{
					message[nbytes]='\0';
					printf("\nClient : %s",message);
					printf("\nNumber of bytes : %d \n",nbytes);
				}
		
				//send()
				printf("\n\nServer :   ");
				gets(message);
				int messagelen=strlen(message);
				if (messagelen>(MaxMsg-1)) 
				{
					printf("\n\nMessage is too long to be sent !");
					getch();
				}
				else
				{
					if(send(sockfd,message,messagelen,0)==-1)
					{
						printf("\n\nNot able to send !");
						getch();
						exit(1);
					}
				}
						
		}while(strcmp(message,"/quit"));

		
		
		

		//close the sockets
		closesocket(sockfd);
		WSACleanup();
		printf("\n\nSocket closed");
		getch();
	}
	
	
	
	
	//---------------Client------------------------------------------
	else if(mode == 2)
	{

		printf("\n\nClient");
		printf("  \n______\n\n");

		//socket()
		if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
		{
			printf("\n\nCan not initialize a socket !\t");
			getch();
			exit(1);
		}
		printf("\n\nSocket declared\t");

		//get server ip
		printf("\n\nPlease enter the ip of the destination :");
		gets(IP);
		if ( !strlen(IP) )
		{
			strcpy(IP ,"127.0.0.1");
			printf("\nDefault IP address 127.0.0.1 chosen ...");
		}
		

		//Address initialization
		their_addr.sin_family = AF_INET;
		their_addr.sin_port = htons(SPORT);    //Server Port = 3490
		their_addr.sin_addr.s_addr = inet_addr(IP); //server's IP (entered by user)
		memset(&(their_addr.sin_zero),'\0',8); //Zero

		//bind()
		//no need to bind

		//connect()
		if(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr))==-1)
		{
			printf("\n\nCan not connect !\t");
			getch();
			exit(1);
		}
		printf("\n\nConnected\t");

		do
		{
						
				printf("\n Client :   ");
				gets(message);
				int messagelen=strlen(message);
				if (messagelen>(MaxMsg-1)) 
				{
					printf("\n\nMessage is too long to be sent !");
					getch();
				}
				else
				{
					if(send(sockfd,message,messagelen,0)==-1)
					{
						printf("\n\nNot able to send !");
						getch();
						exit(1);
					}
				}
			
			
				//receive()
				int nbytes;
				if((nbytes=recv(sockfd,message,MaxMsg-1,0))==-1)
				{
					printf("\n\nNot able to receive !\t");
					getch();
					exit(1);
				}
				if(nbytes==0)
				{
					printf("\n\nOther side closed connection\t");
					getch();
				}
				else
				{
					message[nbytes]='\0';
					printf("\nServer : %s",message);
					printf("\nNumber of bytes : %d \n",nbytes);

				}

			
		}
		while(strcmp(message,"/quit"));

		//close the sockets
		closesocket(sockfd);
		WSACleanup();
		printf("\n\nSocket closed\t");
		getch();

	}
	//---------------QUIT------------------------------------------
	else
	{
		exit(1);
		cout<<"\n\nProgram terminated\t";
		getch();
	}
	cout<<"\n\nProgram terminated\t";
	getch();
}

    Source: geocities.com/tarekamr20/code

               ( geocities.com/tarekamr20)