Eamorr
Eamorr

Reputation: 10012

Perl client/server socket

--|proxy|--|mux|--|demux|--|proxy|--
                --
                --
                --
machineA   satellite link    machineB

172.16.1.224 172.16.1.218

Greetings,

I have setup as above. I'm trying to create 'mux'. Basically, it reads traffic from a proxy and splits it up for transmission over 4 wires. The 'demux' reads off 4 wires and forwards traffic on to the proxy.

I've got a basic client/server setup in Perl. But I don't know how to get traffic from the proxy into 'mux'?

Here is my code:

server.pl -- runs on 172.16.1.218

use IO::Socket;
$| = 1;
$socket = new IO::Socket::INET (
    LocalHost => '172.16.1.218',
    LocalPort => '5000',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "Coudn't open socket" unless $socket;
print "\nTCPServer Waiting for client on port 5000";

while(1)
{
        $client_socket = "";
        $client_socket = $socket->accept();
        $peer_address = $client_socket->peerhost();
        $peer_port = $client_socket->peerport();

        #print "\n I got a connection from ( $peer_address , $peer_port ) ";
        while (1){
                $send_data = <STDIN>;
                $client_socket->send($send_data);
                $client_socket->recv($recieved_data,10);
                print $recieved_data;#."\n";
                #$client_socket->autoflush(); 
        }
}

and:

client.pl

use IO::Socket;

$socket = new IO::Socket::INET (
    PeerAddr  => '172.16.1.224',
    PeerPort  =>  5000,
    Proto => 'tcp',
)
or die "Couldn't connect to Server\n";

while (1) {
        $socket->recv($recv_data,10);
        print $recv_data."\n";
        $send_data = <STDIN>;
        $socket->send($send_data);
}

I'm just a bit stuck and would appreciate any comments.

Many thanks in advance,

Upvotes: 1

Views: 15574

Answers (1)

Francisco R
Francisco R

Reputation: 4048

  • Your server is handling just one connection. You should use an array of connections (@socket).
  • You have two infinite loops nested. Since the inner one is never going to finish, you are going to attend only the first connection.

This seems a typical chat server, so i recommend you to search Google for "perl chat server". Here you have some source code that can be of help:

http://sourceforge.net/projects/perlchat/

Upvotes: 2

Related Questions