develooper Front page | perl.beginners | Postings from February 2002

RE: socket help, last time i promise ;)

Thread Previous | Thread Next
Bob Showalter
February 6, 2002 08:08
RE: socket help, last time i promise ;)
Message ID:
> -----Original Message-----
> From: Scott L Ryan []
> Sent: Wednesday, February 06, 2002 7:29 AM
> To:
> Subject: socket help, last time i promise ;)
> I want to display a message when a client connects to the server.
> msg>
> right now, I cannot seem to display that message until I have received
> something from the client and I want to display it before the client
> sends anything. puzzling..
> #!/usr/local/bin/perl
> use Socket;
> if (!@ARGV) {
> print "Script cannot be called with no Port Number.... doh!\n";
> } else {
> $server_port = $ARGV[0];
> socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
> $my_addr = sockaddr_in($server_port, INADDR_ANY);
> bind(SERVER, $my_addr) or die "couldnt bind";
> listen(SERVER, SOMAXCONN) or die "cant listen";

I recommend you use IO::Socket instead of this. Much cleaner

> while (accept(CLIENT, SERVER)) {

OK, here's where we get an incoming connection. Why aren't you
displaying your message right here? Or maybe I don't understand
what you mean by "display a message". Do you mean on the server
or on the client?

> print "msg>";

This prompt goes to the client. You need to set autoflush on,
because the prompt will not get to the client. If you use
IO::Socket, it will take care of that for you.

> $input = <CLIENT>;

Here's where we block, waiting for the client to send a line.
I can't see where you are "displaying a message".

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About