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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



May 15, 2009, 5:35 PM

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


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

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

my $sock = new IO::Socket::INET(
LocalHost => '',
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;

$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.


May 26, 2009, 5:59 AM

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



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

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