Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

multi-client server

by Rudolf (Monk)
on Jul 20, 2012 at 23:37 UTC ( #982930=perlquestion: print w/ replies, xml ) Need Help??
Rudolf has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I've been fooling around making a chat program for my friends and I and it works alright except for timeouts and the fact that - my main problem - I can not figure out how to get a client over the internet to connect to the server. The program works correctly over a private network using private network addresses like.. 192.168. or 10... etc. However I am not sure what I need to do for a client to connect to my computer from their computer at home. at the moment in my code I use INADDR_ANY, which Im not entirely sure the meaning of. I have also tried using my external address ( the one you find if you ask for your ip on google ), There is probably some knowlege I am lacking so any help would be greatly appreciated. Thanks!!

use v5.14; use Socket; use IO::Socket; require('server_lib.pl'); flush_system(); socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp')); setsockopt(SERVER,SOL_SOCKET,SO_REUSEADDR,1); my $my_addr = sockaddr_in(5555,INADDR_ANY); bind(SERVER,$my_addr) or die "cant bind"; listen(SERVER,SOMAXCONN); REQUEST: while(accept(CLIENT,SERVER)){ if (my $pid = fork){ next REQUEST; } else{ #child close SERVER; my $ip = get_ip(*CLIENT); my $user_name; if(recv(CLIENT,$user_name,1024,0)){ if( user_exists($user_name) || ip_exists($ip) ){ send_to_ip($ip,"User name exists (please restart);\n"); say "Attempt to duplicate name '$user_name' from IP: $ip"; next REQUEST; } else{ cache($user_name,$ip); } my $conn_msg = "{$user_name Connected}"; say "$conn_msg: $ip"; global_send($conn_msg); while(recv(CLIENT,my $data,1024,0)){ last if !ip_exists($ip); my $msg = "[$user_name] $data"; say $msg; unless( special_command($msg) ){ global_send($msg); } } } my $disconn_msg = "{$user_name Disconnected}"; say $disconn_msg; global_send($disconn_msg); remove_user($user_name); exit; } } continue{ close(CLIENT); }

Comment on multi-client server
Download Code
Re: multi-client server
by tobyink (Abbot) on Jul 21, 2012 at 01:36 UTC

    Firstly, you may have a firewall, so make sure the port in question is not blocked by it.

    Secondly, most home Internet connections these days use NAT. This is typically a feature of your router which allows multiple machines on your local network to hide behind a single public IP address. Most routers have a settings panel somewhere allowing you to map external port numbers to internal machines.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      right, thats great information thanks. I opened up the port I just need to test it out now. But there is not a specific ip addr I should be listening on with my server? Should my current code work?
        Binding to INADDR_ANY just means to listen on all available interfaces no matter what their address. As you're likely to have just one (real, apart from the loopback) network interface that's just what you need. The port forwarding on the router takes care of the rest.
Re: multi-client server
by linuxkid (Sexton) on Jul 21, 2012 at 17:54 UTC

    make sure you have that port open on your firewall and all incomning requests go to the server's ip

    --linuxkid


    imrunningoutofideas.co.cc

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982930]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-10-01 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (7 votes), past polls