I am working on a peer-peer chat system (which later should involve some interesting encryption). However, now I'm only doing the basics. I've set up one side to be a server and am using the Telnet in Windows to connect to the script on my unix box. My server is the following:
#!/usr/bin/perl
use IO::Socket;
$SIG{CHLD} = sub {wait()};
$main_sock = new IO::Socket::INET( LocalHost => 'b5',
LocalPort => 6666,
Listen => 5,
Proto => 'tcp',
Reuse => 1) or die $!;
$line = "";
while ($new_sock = $main_sock->accept()) {
$pid = fork();
die "Cannot fork: $!" unless defined($pid);
if ($pid == 0) {
# Child process
while (defined ($buf = <$new_sock>)) {
print "Client said: $buf\n";
}
exit(0);
} else {
$line = <STDIN>;
print $new_sock "$line";
}
}
My question is with regards to non-blocking. When I first connect, I can write from my telnet session to my server no problem but I can only send one thing I type from my server. Somehow it is not getting to the print $new_sock "$line"; statement. I'm doing forking because it seemed less hairier than using IO::Select. This code has been borrowed and modified from Advanced Perl Programming.
In reply to Sockets
by radagast
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.