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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.