donkeylabor has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to open two sockets on different ports in the same program? I mean, this certainly seems right, but I can't seem to make it work. I have read up on Socket, IO::Socket, forking, select(), etc. and can't get it to work. What I would like to accomplish is to have one server.pl server respond to requests from (a) clients on port, say, 7070 and (b) other type of clients on, say, 7071. When I build and listen to both sockets, though, they seem to get in each other's way. Or am I wrong, can you not create two sockets on different ports with the same script?

Replies are listed 'Best First'.
Re: Two Sockets One Script
by friedo (Prior) on Mar 22, 2005 at 02:18 UTC
    What have you tried so far? Show some code.

    Your problem might be that you're trying to talk on both sockets at the same time. In that case you will need to look into a multitasking environment like threads or POE.

      Here's the sockets code: SERVER
      socket(PEERS, AF_INET, SOCK_STREAM, $protocol) or die "socket() failed +: $!"; setsockopt(PEERS,SOL_SOCKET,SO_REUSEADDR,1) or die "Can't set SO_RE +USADDR: $!" ; my $my_peer_addr = sockaddr_in($peerport,INADDR_ANY); bind(PEERS,$my_peer_addr) or die "bind() failed: $!"; listen(PEERS,SOMAXCONN) or die "listen() failed: $!"; warn "Waiting for Peer Servers on $peerport\n"; ######################################### # SET UP FILE TRANSFER SOCKET ######################################### socket(SERVER, AF_INET, SOCK_STREAM, $protocol) or die "socket() faile +d: $!"; setsockopt(SERVER,SOL_SOCKET,SO_REUSEADDR,1) or die "Can't set SO_R +EUSADDR: $!" ; my $my_addr = sockaddr_in($port,INADDR_ANY); bind(SERVER,$my_addr) or die "bind() failed: $!"; listen(SERVER,SOMAXCONN) or die "listen() failed: $!"; warn "Waiting for Files on $port\n"; } while (1) { while(my $remote_peer_addr = accept(PEERREG,PEERS)){ my ($peerport,$peeraddr) = sockaddr_in($remote_peer_addr); PEERREG->autoflush(1); my $newpeer = inet_ntoa($peeraddr); push (@servers, $newpeer); $servers = @servers; warn "We have $servers servers\n"; shutdown (PEERREG,2); } while (my $remote_addr = accept(FILESESSION,SERVER)){ my ($port,$hisaddr) = sockaddr_in($remote_addr); my $newclient = inet_ntoa($hisaddr); warn "Connection from $newclient\n"; FILESESSION->autoflush(1); my $length_of_randomstring=8; my @chars=('a'..'z','A'..'Z','0'..'9','_'); my $random_string = ""; foreach (1..$length_of_randomstring) {$random_string.=$chars[rand @chars];} $response = $random_string."\n"; print FILESESSION $response; open NEWFILE, ">splits/$random_string"; binmode(NEWFILE); print NEWFILE <FILESESSION>; close(NEWFILE); shutdown(FILESESSION,2); } }
        I just gotta wonder now, where are you reading that you end up using Socket instead of IO::Socket, or even higher-level things like POE? And that truly is a serious question... we need to update those doc locations so that people don't stray down the low-level junk.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.