GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2009, 07:33
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 78
hira will become famous soon enough

Socket Programming - Client server chat application with threads


Hello,

I am totally new to socket programming. Some of its syntax passes over me totally, I've tried to understand a bit from the Beej's guide but still there are things I don't get at all. Our instructor provided the socket initialization code etc., so there weren't any problems with that. We were first supposed to make a simple client/server chat application, whose code I have. After that however, we were asked to do it with threads in such a way that the server and the client sides run simultaneously. I've no idea how its done. I know a bit of the basics, how the threads are created etc. But I do not understand where to create them, which part to make into a thread and so on. Could anyone please explain? I am posting the simple chat application code, without threads, below.

Server Side's Code:

CPP / C++ / C Code:
#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include <string>

using namespace std; 


void main()
{
	WSADATA wsadat;
	WORD rVersion;
	rVersion = MAKEWORD(2,0);
	
	if(WSAStartup(rVersion, &wsadat) != NO_ERROR)
	{
		cout<<"WSA initialization failed.\n";
		WSACleanup();
		return;
	}

	//Creating the welcome socket
	SOCKET welcome_socket;
	welcome_socket = socket(AF_INET, SOCK_STREAM, 0);
	
	if(welcome_socket == INVALID_SOCKET)
	{
		cout<<"Socket could not be created.\n";
		WSACleanup();
		return;
	}
	
	string ipaddress;
	int portno;
    cout<<"Enter your IP address: ";
	cin>>ipaddress;
	//ipaddress = "127.0.0.1";
	cout<<"Enter the port no.: ";
	cin>>portno;
	//portno=10000;

	SOCKADDR_IN sockaddress;
	sockaddress.sin_addr.s_addr = inet_addr(ipaddress.c_str());
	sockaddress.sin_port = htons(portno);
	sockaddress.sin_family = AF_INET;

	if(bind(welcome_socket, (SOCKADDR*)(&sockaddress), sizeof(sockaddress)) == SOCKET_ERROR)
	{
		cout<<"Attempt to bind failed.\n";
		WSACleanup();
		return;
	}
	
	//listen for incoming connection requests on the welcome socket


	listen(welcome_socket, 1);

	//create a socket for communication by accepting any requests on welcome socket


	SOCKET comm_sock;
	SOCKADDR_IN clientaddr;
	int addresslen = sizeof(clientaddr);
	char*Buffer = new char[13];

	int turn=0;
	char* buf;
	buf = new char[50];
	char temp;
	while(!kbhit())
	{
			comm_sock = accept(welcome_socket, (SOCKADDR*) &clientaddr, &addresslen);
			if(comm_sock != SOCKET_ERROR)
			{
				cout<<"Connection established with client at: "
					<<(int)clientaddr.sin_addr.S_un.S_un_b.s_b1<<"."
					<<(int)clientaddr.sin_addr.S_un.S_un_b.s_b2<<"."
					<<(int)clientaddr.sin_addr.S_un.S_un_b.s_b3<<"."
					<<(int)clientaddr.sin_addr.S_un.S_un_b.s_b4<<endl;	
			}
			while(buf !="exit")
			{
				if(turn==0)
				{
						cout<<endl;
						//send and receive data;		
						recv(comm_sock, buf, 50, 0);

						if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
							return;

						cout<<"Msg from client: "<<buf<<endl;
						cin.get(temp);
						turn=1;
				
				}

				
				if(turn==1)
				{
					cout<<"Enter Message: ";
					cin.get(buf,50);
					
				if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
					return;
			
					send(comm_sock, buf,strlen(buf)+1,0);
					turn=0;
				}
			}
	}		

	



	WSACleanup();
	return;
} 


Client Side:

CPP / C++ / C Code:
#include <iostream>
#include <conio.h>
#include <string>
#include <winsock2.h>
#include <fstream>	

using namespace std;


void main()
{

	char temp;
	string ip= "127.0.0.1";
	int port= 6789;

	//cout<<"Enter Server IP: ";
	//cin>>ip;
	//cout<<endl;
	//cin.get(temp);
	//cout<<"Enter Port Number: ";
	//cin>>port;
	//cout<<endl;
	//cin.get(temp);
	
	WSADATA wsadat;
	WORD rVersion;
	rVersion = MAKEWORD(2,0);
	
	if(WSAStartup(rVersion, &wsadat) != NO_ERROR)
								{
									cout<<"WSA initialization failed.\n";
									WSACleanup();
									return;
								}
	SOCKET client;
	client = socket(AF_INET, SOCK_STREAM, 0);

								if(client == INVALID_SOCKET)
								{
									cout<<"Error creating socket.\n";
									WSACleanup();
									return;
								}		
	SOCKADDR_IN server;
	server.sin_family		= AF_INET;
	server.sin_addr.s_addr	= inet_addr(ip.c_str());
	server.sin_port			= htons(port);

	if(connect(client, (SOCKADDR*) &server, sizeof(server)) == SOCKET_ERROR)
								{
									cout<<"Connection cannot be established. \n";
									WSACleanup();
									return;
								}

	char* buf;
	buf = new char[50];

	int turn=1;
	//send and receive data
	while(buf != "exit")
	{

		if(turn==0)
		{
			//	cout<<endl;
				recv(client, buf, 50, 0);
				
				if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
					return;
				cout<<"Msg from Server: "<<buf<<endl;
				cin.get(temp);
				turn=1;
			
		}	

		if(turn==1)
		{
		cout<<"Enter Message: ";
		cin.get(buf,50);

		if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
				return;
//		cin.get(temp);
		send(client, buf, strlen(buf)+1, 0);
		turn=0;
	//	buf='\0';
		}

	}
	
	shutdown(client, SD_BOTH);
	closesocket(client); 
	WSACleanup();
	return;
}

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnReceive notification failed from 2nd time onwards in Windows Socket Programming. mahendra MS Visual C++ / MFC Forum 0 09-Jun-2008 03:48
BarracudaDrive application server for Linksys NSLU2 BDG Computer Software Forum - Linux 0 24-Mar-2008 14:24
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE elcrazy C++ Forum 1 11-Mar-2007 21:42
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:26.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.