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

here is the thing, first of all i don't speak very well english, so sorry about the errors.. I need to create something in perl, Create one server and one or two clients, using sockets, to trade messages betwin the clients, in one LAN for example, I already done something using sockets and there is the code, is a calculator but has no interaction with the user, and i need that to the chat.. Client:
#! /usr/bin/perl -w # client1.pl - a simple client use strict; use Socket; my $host = shift || 'localhost'; my $port = shift || 7890; my $proto = getprotobyname('tcp'); my $line; my $end = 0; my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; my $old_fh = select(SOCKET); $| = 1; select($old_fh); connect(SOCKET, $paddr) or die "connect: $!"; print "Client connected \n"; while ($end == 0) { $line = <SOCKET>; print "Data received from server: \n"; chomp ($line); print $line,"\n"; if ($line eq "OK") { print "->OK received. Will send data\n"; print SOCKET "30/2\n"; print "Data sent\n"; } elsif ($line =~ /[0-9]+/) { print "Resultado: $line"; $end = 1; } else { print "Data unrecognized...\n"; #sleep(1); $end = 1; } } close SOCKET or die "close: $!"; print "\n --Client has disconnected-- \n";
Server:
#! /usr/bin/perl -w # server0.pl use strict; use Socket; my $port = shift || 7890; my $proto = getprotobyname('tcp'); socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; my $paddr = sockaddr_in($port, INADDR_ANY); bind(SERVER, $paddr) or die "bind: $!"; listen(SERVER, SOMAXCONN) or die "listen: $!"; print "SERVER started on port $port \n"; my $client_addr; my $calc=0; my @data; while ($client_addr = accept(CLIENT, SERVER)) { my $old_fh = select(CLIENT); $| = 1; select($old_fh); print "New connection established.\n"; print "Requesting data...\n"; print CLIENT "OK\n"; print "OK sent!\n"; while ($calc == 0) { print "reading...\n"; $calc = <CLIENT>; } print "Data: \n"; print $calc; @data = split(/(\+|\*|\/|\-)/,$calc); if ($data[1] eq "+") { print CLIENT $data[0]+$data[2]; } if ($data[1] eq "-") { print CLIENT $data[0]-$data[2]; } if ($data[1] eq "*") { print CLIENT $data[0]*$data[2]; } if ($data[1] eq "/") { print CLIENT $data[0]/$data[2]; } close CLIENT; print "--END--"; exit; }
based on that code some hellp me to create, or create the code to use one server and one or more clients, to trade messages betwin them, pls i really need your help. Urgent

Replies are listed 'Best First'.
Re: Perl Chat or privat messenger
by Your Mother (Archbishop) on Apr 23, 2008 at 23:56 UTC

    Don't have time to read through the code above right now. I do recommend taking a look through the POE Cookbook. You'll probably get something easier to extend, debug, and adapt.

      tks but i'm using perl express in windows..

        Hi, I want connect remote server and local machine .I have written a simple code but its working on same machine not worked on different machine.what i do? please reply the below mail ggopal1986@gmail.com

      Hi, I want connect remote server and local machine .I have written a simple code but its working on same machine not worked on different machine.what i do?

        You're also replying to a five year old thread. You'll have a much better chance of getting by help posting a new question with details about what you've tried, including a minimal code example, and the exact results you had versus what you expected.

        daniel mesquita:

        First, find out why it's not working with the remote machine. Check firewall rules, DNS, and all the usual suspects.

        Chances are reasonably good that if both the client and server are operating on your local machine then you may have a networking issue rather than a code problem.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Perl Chat or privat messenger
by doctor_moron (Scribe) on Apr 24, 2008 at 04:06 UTC
    There's a book, Network Programming With Perl by Lincoln D. Stein, you can see in Chapter 19 about chat client and server
Re: Perl Chat or privat messenger
by zentara (Cardinal) on Apr 24, 2008 at 14:41 UTC
      tks for the hellp...