What you need to do appears a little sketchy. If the issue is that you need lots of clients to talk to a server that will only accept a single connection then the answer is you need a proxy.
Client --> P Client --> R Client --> O ------> Single Conn Server Client --> X Client --> Y
Here is some simple code for a server that will handle multiple connections from clients.
Simple proxy code....updated in line with comments by thospel
use IO::Select; use IO::Socket; my $listen_port = 8080; # we proxy for many clients on this port my $scs_port = 8181; # and talk to a single connection on this por +t my $DEBUG = 1; my $lsn = new IO::Socket::INET( Listen => 1, LocalPort => $listen_port ) or die $!; # make a single connection to some server my $sock = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => $scs_port, Proto => 'tcp' ) or die $!; my $client = new IO::Select( $lsn ); my $server = new IO::Select( $sock ); my ($count, $data, $buffer); while( my @ready = $client->can_read) { for my $fh (@ready) { if($fh == $lsn) { # Create a new socket to handle more conns $count++; $DEBUG && warn "Accepted new socket $count\n"; my $new = $lsn->accept; $client->add($new); # register it with IO::Select } else { # Process socket $DEBUG && warn "Proxy getting data\n"; $data = ''; $data .= $buffer while sysread( $fh, $buffer, 1 ) and $buf +fer ne "\n"; $DEBUG && warn "Proxy got: $data\n"; if ( ! $data or $data =~ m/exit/i ) { remove_handle( $client, $fh ); } else { # act as a proxy for this client to the SCS $DEBUG && warn "Writing to server\n"; die "Can't write to server!\n" unless $server->can_wri +te; syswrite( $sock, "$data\n" ); $DEBUG && warn "Reading from server\n"; die "Can't read from server!\n" unless $server->can_re +ad; $data = ''; $data .= $buffer while sysread( $sock, $buffer, 1 ) an +d $buffer ne "\n"; $DEBUG && warn "Writing back to client\n"; if ( $client->can_write ) { syswrite( $fh, "$data\n" ); } else { $DEBUG && warn "Can't write data back to client\n" +; remove_handle( $client, $fh ); } #syswrite( $fh, "Hello $data\n" ); # is we want a sim +ple server } } } } sub remove_handle { my ( $client, $fh ) = @_; $DEBUG && warn "Removing client!\n"; syswrite( $fh, "Sayonara!\n" ) if $client->can_write; $client->remove($fh); $fh->close; $count--; }
cheers
tachyon
In reply to Re: Single user to multi user socket server
by tachyon
in thread Single user to multi user socket server
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |