CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
reading from a socket, then writing to it

 



b34st1y
Novice

May 15, 2009, 5:35 PM

Post #1 of 2 (458 views)
reading from a socket, then writing to it Can't Post

Hi,

I've been tinkering with sockets for a while now. I've been wondering how I can get the server that is listening on the socket to write something back to the socket, such as to speak back to the client. Any help would be greatly appreciated...I seem to be stuck. This is my server script

Code
#!/usr/bin/perl -w 
# serIO.pl
# server using IO::Socket
#---------------------
#use strict;
use IO::Socket;

my $sock = new IO::Socket::INET(
LocalHost => '192.168.3.111',
LocalPort => 7890,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);

$sock or die "no socket :$!";


my($new_sock, $c_addr, $buf);

while (($new_sock, $c_addr) = $sock->accept())
{
$pid = fork();
#die "Cannot fork: $!" unless defined($pid);


if ($pid == 0)
{
my ($client_port, $c_ip) =sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host =gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host"," [$client_ipnum] ";

# ;
while (defined ($buf = <$new_sock>))
{
print $buf;
}
}

else
{
$stuff1 = <STDIN>;

print $sock $stuff1;

}


it reads just fine from the socket, and can listen for multiple clients, thanks to the fork() call. I just need it to be able to write back to the socket and the client to see it on the other side.


b34st1y
Novice

May 26, 2009, 5:59 AM

Post #2 of 2 (399 views)
Re: [b34st1y] reading from a socket, then writing to it [In reply to] Can't Post

*bump*

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives