When I run your code, I get a bunch of errors when the client tries to connect. I'm on Windows 7 x64 with Strawberry 5.22.1 64-bit.
It may be just adding the prompt statements in the "if($so == $sock)" block of your server code before accept() call.
This doesn't directly answer your question, but maybe some useful advice? Your code looks a lot like it was ported from C using Socket calls directly. Perl has IO::Socket in core and IO::Socket::IP in newer versions which even allows IPv6 and IPv4 - if you're interested in that.
Following is a simple example TCP client / server with IO::Socket instead. It doesn't use IO::Select so it's blocking, but it seems you have that in your code so you can probably figure out how to incorporate if you need it:
SERVER:
#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $PORT = $ARGV[0] || 7070; $| = 1; my $socket = IO::Socket::INET->new( Proto => "tcp", LocalPort => $PORT, Listen => SOMAXCONN ) || die "Cannot create server\n"; print "TCP Server waiting for client on port $PORT\n"; while (my $client = $socket->accept) { my $peer_addr = $client->peerhost; my $peer_port = $client->peerport; print $client "Welcome to SERVER\n"; print "CONNECTION: ($peer_addr:$peer_port)\n"; while (<$client>) { chomp $_; print "($peer_addr:$peer_port): $_\n"; print $client "$.: $_\n" } close($client); print "DISCONNECT: ($peer_addr:$peer_port)\n"; } die "Cannot accept socket ($!)\n";
CLIENT:
#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $PORT = 7070; if (!$ARGV[0]) { print "Usage: $0 Peer_IPAddress [Port]\n"; exit } elsif ($ARGV[2]) { print "Usage: $0 Peer_IPAddress [Port]\n"; exit } elsif ($ARGV[1]) { $PORT = $ARGV[1] } my $PEER = $ARGV[0]; my $socket = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $PEER, PeerPort => $PORT ) || die "Cannot create client\n"; #print scalar <$socket>; while (1) { print scalar <$socket>; print "PROMPT> "; my $message = <STDIN>; chomp $message; if (($message !~ /^q(?:uit)?$/i) && ($message !~ /^e(?:xit)?$/i)) +{ print $socket $message . "\n" } else { last } } close($socket);
In reply to Re: Chat room with socket programming question
by VinsWorldcom
in thread Chat room with socket programming question
by Perl_New_Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |