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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.