Thread: Is there a way...

  1. #1
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39

    Is there a way...

    Is there a way to create a mixture of a game/chatting interactive program using c++? I have seen it done before, if there is can somebody give me a link to a site that i can learn basics for this. Thanx in advance.

  2. #2

    Join Date
    May 2005
    Posts
    1,042
    You want to look into network programming. Winsock is the typical API for this.

    http://compnetworking.about.com/gi/d...2Fwinsock.html
    http://www.madwizard.org/view.php?pa...pter4&lang=cpp


    Here's some code I wrote for initializing winsock and established a socket. Once you have init'ed winsock and created a socket you can start sending packets. My code *just* initializes winsock and a socket however.

    Code:
    #include	"SocketHelper.h"
    
    #define	SOCKET_DEBUG	0
    
    #if	SOCKET_DEBUG
    #include	<fstream>
    #endif
    
    
    	/*
    	http://compnetworking.about.com/gi/dynamic/offsite.htm?site=http%3A%2F%2Fworld.std.com%2F%7Ejimf%2Fpapers%2Fsockets%2Fwinsock.html
    	http://www.madwizard.org/view.php?page=tutorials.networking.chapter4&lang=cpp	
    	*/
    	
    	/*
    	gethostbyname("hostname")		type in "www.yahoo.com" and it returns information about it
    
    	Note  The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. 
    	Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.
    	*/
    
    	/*
    	socket()	--->>>  AF_INET = internetformat
    	Creates a socket, first argument is the address family (use internet format), second is the type of socket (use SOCK_STREAM or SOCK_DGRAM,
    		the latter stands for datagram, the former means the data will come in as a stream of characters)
    	*/
    
    	/*
    	bind()
    	Binds a socket to an address
    	*/	
    
    /*
    	Starts up winsock, returns true if successful, false otherwise
    */
    BOOL	Initialize_Winsock()
    {
    	WSAData	info;
    	
    	if(WSAStartup(MAKEWORD(1,1),&info) !=	0)
    	{
    	//	MessageBox(NULL,"Could Not Initiate Winsock 1.1","ERROR",MB_OK|MB_ICONEXCLAMATION);
    		return	FALSE;
    	}
    	else
    	{
    	//	MessageBox(NULL,"Winsock 1.1 Initiated properly","SUCCESS",MB_OK|MB_ICONINFORMATION);
    		return	TRUE;
    	}	
    }
    
    /*
    	Establishes a socket on the computer calling the function on the given port number
    */
    int Establish_Socket(unsigned	int	portnumber,SOCKET	*output)
    {
    	hostent	*myself;
    	sockaddr_in	socket_address;	//socket address for internet format
    	SOCKET	MySocket;
    	
    	char	MyName[256];	//Host name of this computer
    	memset(MyName,0,sizeof(char)*256);	
    	
    	gethostname(MyName,sizeof(char)*256);	//Get the host name of this computer
    	myself	=	gethostbyname(MyName);		//Then use the host name to get the hostent information 
    
    	if(myself	==	NULL)
    	{
    	//	MessageBox(NULL,"Establish_Socket::Invalid host information","ERROR",MB_OK);
    		return	-1;
    	}
    
    	socket_address.sin_family	=	myself->h_addrtype;	
    	socket_address.sin_port		=	htons(portnumber);
    
    	//INADDR_ANY means 'use the internet IP of this computer' which makes sense because we are trying to establish the socket on THIS computer
    	//The inet_addr function in the example above can convert an IP address in dotted string format to the appropriate 32-bit value in network byte order. 
    	//There is also a function called inet_ntoa, which does exactly the opposite.
    	socket_address.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//inet_addr("127.0.0.1");
    
    	//Create a socket on this computer using internet format (AF_INET) and a type of SOCK_STREAM (data transmitted
    	//as a stream of characters)
    
    	MySocket	=	socket(AF_INET,SOCK_STREAM,0);
    	if(MySocket == INVALID_SOCKET)
    	{
    	//	MessageBox(NULL,"Establish_Socket::Failed after call to socket()","ERROR",MB_OK);
    		return	-2;
    	}
    	
    	if (bind(MySocket, (struct sockaddr *)&socket_address, sizeof(sockaddr_in)) == SOCKET_ERROR) 
    	{
    	//	MessageBox(NULL,"Establish_Socket::Failed after call to bind()","ERROR",MB_OK);
    		closesocket(MySocket);
    		return	-3;
    	}
    	
    	
    	listen(MySocket,3);		//Listen to 3 connections maximum
    	*output	=	MySocket;
    	return	1;
    }
    
    
    /*
    #if	SOCKET_DEBUG
    		int	error = WSAGetLastError();
    		std::ofstream	fout("WINSOCKERROR.txt");
    		switch(error)
    		{
    			
    		case	WSANOTINITIALISED :
    			{
    				fout	<<	"WSANOTINITIALISED" << "\n";
    				break;
    			}
    
    		case	WSAENETDOWN:
    			{
    				fout	<<	"WSAENETDOWN: The network subsystem has failed" << "\n";
    					break;
    			}
    		case	WSAEACCES :
    			{
    				fout << "WSAEACCES Attempt to connect datagram socket to broadcast address failed because setsockopt option SO_BROADCAST is not enabled" << "\n";
    				break;
    			}
    			
    		case	WSAEADDRINUSE:
    			{
    				fout	<<	"WSAEADDRINUSE A process on the computer is already bound to the same fully-qualified address and the socket has not been marked to allow address reuse with SO_REUSEADDR. For example, the IP address and port are bound in the af_inet case). (See the SO_REUSEADDR socket option under setsockopt" << "\n";
    				break;
    			}
    
    		case	WSAEADDRNOTAVAIL:
    			{ 	
    				fout << "WSAEADDRNOTAVAIL The specified address is not a valid address for this computer" << "\n";
    				break;
    			}
    		case	WSAEFAULT:
    			{
    				fout	<<	"WSAEFAULT The name or namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains an incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor s" << "\n";
    				break;
    			}
    		case	WSAEINPROGRESS:
    			{
    				fout	<<	"WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function" << "\n";
    				break;
    			}
    
    		case	WSAEINVAL:
    			{
    				fout << "WSAEINVAL The socket is already bound to an address" << "\n";
    				break;
    			}
    
    		case	WSAENOBUFS:
    			{
    				fout << "WSAENOBUFS Not enough buffers available, too many connections" << "\n";
    				break;
    			}
    
    		case	WSAENOTSOCK:
    			{
    				fout	<<	"WSAENOTSOCK The descriptor is not a socket" << "\n";
    				break;
    			}
    
    			fout.close();
    		}
    #endif
    		*/
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed