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); }
In reply to multi-client server by Rudolf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |