For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > May 2004 > IO::Sockets









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IO::Sockets
Peter Wilson

2004-05-21, 6:30 am

Hi

I am trying to use sockets to have a server that takes a message then
returns down the socket that it was ok and give back an id.

I can get the server ok but i can't understand how to get the client to read
in from the socket after send its message.

My code is:

Server:

use IO::Socket;

$SIG{CHLD} = sub {wait ()};

$main_socket = new IO::Socket::INET (Localhost => 'localhost',
LocalPort => 1200,
Proto => 'tcp',
Listen => 5,
Reuse => 1,
);

die "Socket could not be created. Reason: $!" unless $main_socket;

while($new_sock = $main_socket->accept())
{
$pid = fork();
die "Can not fork : $!" unless defined ($pid);
if ($pid == 0)
{
while (defined ($buf = <$new_sock> ))
{
print $buf."\n";
print $new_sock "You said: $buf\n";
}
exit(0);
}
}
close ($main_socket);

Client is:

use IO::Socket;

my $sock;
my $buf;
$sock = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => 1200,
Proto => 'tcp',
);

die "Socket could not be created. Reason: $!\n" unless $sock;

print $sock '<MSG SENDER="Peter"></MSG>';
$sock->flush();
while($new_sock = $sock->accept())
{
while (defined($buf = <$new_sock> ))
{
print $buf;
}
exit(0);
}

close($sock);

But this of course does not work.

Can anyone give me a pointer?

Thanks

Peter


Jim Gibson

2004-05-27, 4:31 pm

In article <v0krc.5$XG2.0@newsfe6-win>, Peter Wilson
<peter.wilson270@ntlworld.com> wrote:

> Hi
>
> I am trying to use sockets to have a server that takes a message then
> returns down the socket that it was ok and give back an id.
>
> I can get the server ok but i can't understand how to get the client to read
> in from the socket after send its message.
>
> My code is:
>


[OK looking server snipped]

>
> Client is:
>
> use IO::Socket;
>
> my $sock;
> my $buf;
> $sock = new IO::Socket::INET (PeerAddr => 'localhost',
> PeerPort => 1200,
> Proto => 'tcp',
> );
>
> die "Socket could not be created. Reason: $!\n" unless $sock;
>
> print $sock '<MSG SENDER="Peter"></MSG>';


You should add a newline to the end of this line, since you are reading
in line mode in the server.

print $sock "\n";

> $sock->flush();
> while($new_sock = $sock->accept())


Do not call accept(). That call is only for servers and only at the
beginning of the connection to establish it. Simply read from the
socket as you are doing below, but not from $sock, not $new_sock.

> {
> while (defined($buf = <$new_sock> ))


while( defined($buf = <$sock> ))

> {
> print $buf;
> }
> exit(0);
> }
>
> close($sock);


If you can't get <$sock> to work, try $sock->print() and $sock->$read()
or $sock->sysread() and $sock->syswrite().
Jim Gibson

2004-05-27, 5:31 pm

In article <270520041212341402%jgibson@mail.arc.nasa.gov>, Jim Gibson
<jgibson@mail.arc.nasa.gov> wrote:

[snip]


>
> Do not call accept(). That call is only for servers and only at the
> beginning of the connection to establish it. Simply read from the
> socket as you are doing below, but not from $sock, not $new_sock.


Sorry, that should have read " ... but from $sock, not $new_sock."
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com