Skip to main content
gamedev.net
🔒 Locked

LAN-Browser

Started by stryx 7 replies 1.1k views
Original Post
stryx
stryx
I know this question was asked before lots of times, but the answeres didn''t help me much. The problem is I have to code a ServerBrowser for LAN and I''ve no idea how to do this. I read in this forum, that Broadcasting is the way to do this. I have to create a Broadcasting Socket @ the server. Then the client sends to the ip 255.255.255.255 an requesting-servers message, and all the servers with the broadcast sockets should get this, and reply. Am I right with this ? It woulb be very nice if someone here can hack a small piece of code together (tutorial would be perfect for this page) and post it somewhere. If you got no space, mail me, I''ll host it. It would be nice if you can point me somewhere. thanks stryx stryx@gmx.at Thanks in advance.
Anything that requires finding convex hulls in realtime isstarting to sound like a bad idea. -- John Carmack
wrathgame
wrathgame
Elo,

You may get more help if you explain more what it is exactly that you want to do ??

You can use the UDP protocal to broadcast to all available IP addresses by sending it to 255.255.255.255 but that will only be forwarded by some network hubs/switchs like for example the internet doesnt forward 255.255.255.255 broadcasts

try giving an example of your problem
~ Tim
stryx
stryx
Ok here''s what i want to do.

This is only for LAN !

Server A creates a game.
Client A wants to join but doesn''t know the IP of the server.
So the client has to broadcast if there is a server on the network.
If a server recv''s this message he replys with his data.
So the client gets a list of the servers available on the network.
Like the server-browser in Quake 3

How to set up the client socket to broadcast, and how to set up the server socket to recv the broadcast ?
Anything that requires finding convex hulls in realtime isstarting to sound like a bad idea. -- John Carmack
Karg
Karg
Not that your idea won''t work, or that mine is even any better, but I had the servers broadcast every sec. or so and the client would just listen on the port when the program starts up and build up a list of servers it has heard from. Then it would drop a server it hadn''t heard from in a few secs. Just have the server send an "I''m here" packet every sec. and have the clients constantly recv. on the "server searching" port when it is at the splash screen/main menu/config/anything else not in game portion.

Karg
stryx
stryx
you''re right.
That''s a fine idea, too.
Better than mine, but I think it''s more bandwidth waste than mine.
Am I right ?

Well, the "MAIN-PROBLEM" is, that I don''t know how to send and recv this messages.
I know that I have to set a socket (server or client) to SO_BROADCAST or somethuing like that.

These lines for the setup and sending of the messages of both server and client are the problem.

Would be great if somebody can drop this lines here

thanks anyway, stryx
Anything that requires finding convex hulls in realtime isstarting to sound like a bad idea. -- John Carmack
Shannon Barber
Shannon Barber
Which method uses more bandwidth depend on the number of clients. If there are lots of clients, the second way may actually use less bandwidth.

But it doesn''t matter - one tiny, tiny packet sent once a second won''t make a big difference on a LAN.

I think I would only broad-cast it to the server''s subnet though.
e.g. server address:
ip: 192.168.0.1
mask: 255.255.255.0

broadcast to: 192.168.0.255
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
wrathgame
wrathgame
Ok.. I think your gagging for some code so here ya go

Client (broadcasts)

  
sockaddr_in sa;
int s;
int n = sizeof(sockaddr);
int on = 1;
int len = strlen(argv[1]);

if ( (s = socket(AF_INET,SOCK_DGRAM,0)) < 0 )
exit(-1);

// broadcast option

if ( setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *)&on, sizeof(on)) != 0)
exit(-2);

sa.sin_family = AF_INET;
// 255.255.255.255

sa.sin_addr.s_addr = htonl(INADDR_BROADCAST);
sa.sin_port = htons(1234);
sendto(s, argv[1], len, 0, (const struct sockaddr *)&sa, n);
}


Server (recieves)

  
struct sockaddr_in sa;
int s;
int i;
int n = sizeof(struct sockaddr);
char buf[256];
hostent *hp;

if ( (s = socket(AF_INET,SOCK_DGRAM,0)) < 0 )
exit(-1);

sa.sin_family = AF_INET;
// any address

sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(1234);

if ( bind(s, (struct sockaddr *) &sa, n ) != 0 )
exit(1);
while ( 1 )
{
if ( (i=recvfrom(s,buf,255,0,(struct sockaddr *)&sa,&n)) == -1 )
exit(1);
buf[i] = (char)0;
hp = gethostbyaddr((const char *)&sa.sin_addr, 4, AF_INET);
cout << hp->h_name << ": " << buf << endl;
}


That should get you started

Tim

[edited by - wrathgame on December 11, 2002 8:24:42 PM]
~ Tim
stryx
stryx
Yeah, that was it exactly what I wanted to know

Thank you for your great help !
Now I understand how to do this.
I thought it would be harder
You really don''t know how much you helped me with this.
Thanks for your patience and your time !

stryx
Anything that requires finding convex hulls in realtime isstarting to sound like a bad idea. -- John Carmack

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.