Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-28-2008, 03:29 AM   #1
LQ Newbie
Registered: Jul 2008
Posts: 20

Rep: Reputation: 0
How to send and receive data from a single socket in perl??

Hello everyone,
What my server want to do is:
1. Connect to one client only and one server only
2. Receive data from the client
3. If something is received then create a child process and in the child process send the data to another server and send the response of that server to the client.
4. In the meantime my server want to receive more data from the same client simultaniously.

So you can say that it is a middleware

This is my code:
whats wrong with it? please help.


use IO::Socket;

$SIG{CHLD} = sub {wait ()};
my $main_sock = new IO::Socket::INET(
LocalHost => '',
LocalPort => 4000,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
die "Socket could not be created. Reason: $!\n" unless ($main_sock);

my $cbsSocket = new IO::Socket::INET(PeerPort=>5000, PeerHost=>'', Proto=>'tcp');
die "Socket could not be created. Reason: $!\n" unless ($cbsSocket);

while ($new_sock = $main_sock->accept())
print "SOCKET READY \n";
print "%%".$buf."%%\n";
if($buf eq '')
$pid = fork();
die "Cannot fork: $!" unless defined($pid);

if($pid == 0)
print "** INSIDE CHILD PROCESS ** \n";
#do some manipulation wiht the data $buf

#sending $buf to another server that will return me buf2
$cbsSocket->recv($buf2, 10000);

# Now sending $buf2 to the client from which I received $buf
if($buf2 ne '')
print "inside if \n";
print "*** Sending SUCCESSFUL ***\n";
print "***** cannot send ****** \n";
print "*** EXCEPTION NO RESPONSE *** \n";
print "*** SENDING EMPTY *** \n\n";
$a = "NO REPLY";

print "** EXIT FROM CHILD PROCESS ** \n";
#$new_sock = $main_sock->accept();
exit 0;
print "RETURN TO PARENT \n";
#$new_sock = $main_sock->accept();
#close ($main_sock);


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simultaneous Socket Send/Recv in Perl? juanbobo Programming 10 07-28-2008 03:30 AM
Modem Data Send/Receive Problem mofargel Linux - Hardware 1 10-16-2007 08:42 PM
Unable to receive data in an udp socket stephenwalter Programming 8 11-24-2005 11:20 PM
Ethernet card (Ether Express Pro) can send but NOT receive data Pipo Linux - Networking 2 09-28-2005 05:33 AM
can i send or receive data in compress form to internet alvi2 Linux - Networking 1 03-05-2005 07:48 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration