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

Im working on a simple im client/server. Right now its just a client that connects to a server, and the server sends the client some version info.

Here is the client:
#!/usr/bin/perl ################## #name: slut #by: jasper ################## use Class::Struct; use Time::localtime; use IO::Socket; use strict; ##################### #client configuration my $server = "localhost"; my $port = 7809; my $nick = shift || "slut"; struct( slut => [ server => '$', port => '$', nick => '$', usr_crypto => '%', #stores remote nick (key) and session encry +ption key (value) bytes_o => '$', bytes_i => '$', sock => '$' ]); print "+ - -\n* Slut v0.1a\n* By: Jasper\n+ - -\n\n"; my $sock = IO::Socket::INET->new (PeerAddr => $server, PeerPort => $port, Proto => "tcp", Reuse => 1)or die "Can't Connect to $s +erver!\n"; my $slut = slut->new(server => $server, port => $port, nick => $nick, sock => $sock); my $buff; print $sock "hey!"; while ($sock->recv($buff,256)) { chomp($buff); print "$buff\n"; $slut->bytes_i =+ length($buff); #$slut->bytes_o =+ length($data); }


Here is the server:
#!/usr/bin/perl ################## # SlutServ v1.0 # by: jasper ################## use Class::Struct; use Time::localtime; use IO::Socket; use IO::select; use strict; use Constant DEBUG => 1; ##################### #server configuration my $name = "SlutServ"; #name of server my $port = shift || 7809; #listening port my $max_conn = 15; #maximum number of connections allowed struct( slut => [ name => '$', port => '$', users => '%', #stores remote nicks and their ip addresses usr_crypto => '%', #stores remote nick (key) and session encry +ption key (value) bytes_o => '$', bytes_i => '$', sock => '$' ]); print "+ - -\n* SlutServ v0.1a\n* By: Jasper\n+ - -\n\n"; my $listen = IO::Socket::INET->new ( LocalPort => $port, listen => $max_conn, Proto => "tcp", Reuse => 1) or die "Can't Start Server + on Port $port!\n"; print "Socket Initialized!\n" if DEBUG; my $slut = slut->new(name => $name, port => $port, sock => $listen); my $reader = IO::Select->new($listen) or die "Can't create i/o select +read object!"; print "Before while loop\n" if DEBUG; while (1) { print "Start While Loop\n" if DEBUG; if (my @ready = $reader->can_read(1)) { print "Just got @ready. moving to foreach loop\n"; foreach my $handle (@ready) { print "In socket foreach loop\n"; if($handle == $listen) { print "handle is \$listen\n" if DEBUG; my $connect = $listen->accept(); print "Accepted Connection!\n" if DEBUG; syswrite($connect,"200 SlutServ v0.1a"); } else { print "Isnt \$listen\n"; } } } else { print "Not Ready!\n"; } }


The slut structure is going to be used later on. This is my first client/server pair, so I dont know if im doing everything right.

I want the server to listen for connections, accept the connections, receive data sent from the various clients, process the data sent from the various clients, and send data back out accordingly.
Any help will be appreciated

-jasper-

Replies are listed 'Best First'.
Re: Client/Server
by Ryszard (Priest) on Sep 22, 2002 at 09:00 UTC
    There are a couple of things you can do here:
    1. Get rid of your "slut" name space.
    2. Use Net::Server, you dont have to worry about building the server at such a low level, and the doco provides a good working example.
      I started to write all this in C, and surprisingly it was much easier, and i got help quicker. But i appreciate your feedback :). "Slut" is going to be a console based IM Client/Server.
      I use Net::Server to creat udpserver, I want print udpdata but I don't know how to do Who can help me! I am a newbe. Thanks!