Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LAN/WAN

Status
Not open for further replies.

XerxesTheMighty

Programmer
Jun 15, 2003
72
US
I'm creating a mutiplayer game and I'm just wondering how to connect to a user across the network (LAN/WAN,internet, etc.), and continually exchange data back n' forth (constantly updating).

--------------
"Getting to the top is an extra reward.Its the climb that makes the man."
Xerxes Dynatos
 
You will probably want to learn how to use sockets. There are a lot of resources on the internet about their use.
 
Like I want to make a chat in one part of the game. I also have a checkbox. K, so when I click on the checkbox on my computer, it'll be clicked on their's. I really don't understand sockets to well, but I'm gettin there--perhaps some suggestions would help me.

--------------
"Getting to the top is an extra reward.Its the climb that makes the man."
Xerxes Dynatos
 
what especialy you do not understand on using sockets?

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Like connecting and sending 'data' back and forth.

In one part I have a chat thing.

I also have some checkboxes-so when I click on one on the server, the client will see it checked also.

Basically everthing.



--------------
"Chinese proverb:'Man who sit with legs crossed and mouth open, waiting for roast duck to fly in, have long hunger!'"
Xerxes Dynatos
 
this is the code of simpliest chat program, in console mode:
Code:
////////////client////////////////
#include<io.h>
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
#define PORTNUM 888
#include<windows.h>
bool run = true;
DWORD WINAPI ThreadProcToSvr(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	while(1)
	{
		int nbytes = recv(ns, buf, sizeof(buf), 0);
		printf(&quot;%s\n&quot;, buf);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
DWORD WINAPI ThreadProcCln(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	HANDLE hThreadCln = 0;
	ULONG th_id;
	while(1)
	{
		gets(buf);
		send(ns, buf, sizeof(buf), 0);
		if(!hThreadCln)hThreadCln = CreateThread(0, 0, ThreadProcToSvr, (void*)&ns, 0, &th_id);
		if(!hThreadCln){printf(&quot;error on creating thread\n&quot;);exit(1);}
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}

int main(HINSTANCE, HINSTANCE, LPSTR, int)
{
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSADATA wsd;
	WSAStartup(wVersionRequested, &wsd);

	int s;//SOCKET

	sockaddr_in svr_addr;//sei-filipski
	hostent *hp;
	char buf[80] = &quot;salut la toti&quot;;
	if((hp = gethostbyname(&quot;hostname&quot;)) == 0)
	{
		printf(&quot;error on calling gethostbyname()\n&quot;);
		exit(1);
	}
	strncpy((char*)&svr_addr.sin_addr.S_un.S_un_b, hp->h_addr_list[0], hp->h_length);


	svr_addr.sin_family = hp->h_addrtype;
	svr_addr.sin_port = htons((u_short)PORTNUM);
	if((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		printf(&quot;error on creating socket\n&quot;);
		exit(1);
	}
	if(connect(s, (const sockaddr*)&svr_addr, sizeof(svr_addr)) == -1)
	{
		printf(&quot;error on calling connect()\n&quot;);
		exit(1);
	}
	ULONG th_id;
	HANDLE hThreadSvr = 0;
	hThreadSvr = CreateThread(0, 0, ThreadProcCln, (void*)&s, 0, &th_id);
	if(!hThreadSvr){printf(&quot;error on creating thread&quot;);exit(1);}
	while(run);
	close(s);
	return 0;
}
////////////client////////////////
////////////server////////////////
#include<conio.h>
#include<io.h>
#include<stdlib.h>
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
//WS2_32.Lib required
#define PORTNUM 888;
//server
bool run = true;
DWORD WINAPI ThreadProcCln(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	while(1)
	{
		gets(buf);
		send(ns, buf, sizeof(buf), 0);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
DWORD WINAPI ThreadProcSvr(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	HANDLE hThreadCln = 0;
	ULONG th_id;
	while(1)
	{
		int nbytes = recv(ns, buf, sizeof(buf), 0);
		if(!hThreadCln)hThreadCln = CreateThread(0, 0, ThreadProcCln, (void*)&ns, 0, &th_id);
		if(!hThreadCln){printf(&quot;error on creating thread\n&quot;);exit(1);}
		printf(&quot;%s\n&quot;, buf);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
int main()
{
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSADATA wsd;
	WSAStartup(wVersionRequested, &wsd);
	int s, ns;
	int nport;
	nport = PORTNUM;
	nport = htons((u_short)nport);
	sockaddr_in svr_addr, cln_addr;
	s = socket(AF_INET, SOCK_STREAM, 0);
	if((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
	{
		printf(&quot;error on calling socket()\n&quot;);
		exit(1);
	}
	svr_addr.sin_family = AF_INET;
	svr_addr.sin_addr.S_un.S_addr = (u_long)INADDR_ANY;
	svr_addr.sin_port = nport;
	if(bind(s, (const sockaddr*)&svr_addr, sizeof(svr_addr)) == -1)
	{
		printf(&quot;error on calling bind()&quot;);
		exit(1);
	}
	printf(&quot;server ready: %s\n&quot;, inet_ntoa(svr_addr.sin_addr));
	if(listen(s, 5) == -1)
	{
		printf(&quot;error on calling listen()\n&quot;);
		exit(1);
	}

	while(1)
	{
		int addrlen;
		addrlen = sizeof(cln_addr);
		if((ns = accept(s, (sockaddr*)&cln_addr, &addrlen)) == -1)
		{
			printf(&quot;error on calling accept\n&quot;);
			exit(1);
		}
		printf(&quot;client: %s\n&quot;, inet_ntoa(cln_addr.sin_addr));
		int nbytes;
		char buf[80];
		ULONG th_id;
		HANDLE hThreadSvr = 0;
		hThreadSvr = CreateThread(0, 0, ThreadProcSvr, (void*)&ns, 0, &th_id);
		if(!hThreadSvr){printf(&quot;error on creating thread&quot;);exit(1);}
		while(run);
		close(ns);
		close(s);
		exit(0);
	}
	return 0;
}
////////////server////////////////

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
I have done a lot of research into the winsock and network talking. Well atleast enough to write an application that transmits account information into an access database. And updates it on a minute to minute basis. I started with Sam's Teach yourself C++, they have a network chat program in there.

I then posted some questions here on the board:

thread116-635627 possibly a few others I might have missed, and the people here give very good answers (sometimes with a little prying) as to what to do.

The other place to look at, which was a little help is MSDN and there network chat program.


Here is the section on CAsycnSocket which I used to do what I need to do.



and:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top