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: Beginner:
TCP socket not receiving

 



zak100
User

Aug 2, 2014, 12:19 AM

Post #1 of 4 (914 views)
TCP socket not receiving Can't Post

Hi,
I have download a client server code using TCP sockets from the internet. I have modified this code to add two numbers but at the server end the TCP socket is not receiving. Somebody please help me.
Server Code

Code
use IO::Socket::INET; 

# auto-flush on socket
$| = 1;

# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";

while(1)
{
# waiting for a new client connection
my $client_socket = $socket->accept();

# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";

# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "received data: $data\n";

# write response data to the connected client
$data = "ok";
$client_socket->send($data);

# notify client that response has been sent
my $data1;
my $data2;
my $sum;
$client_socket->recv($data1, 1024);
$client_socket->recv($data2, 1024);
print "data1 = $data1 and data2= $data2\n";

$sum = $data1 + $data2;
$client_socket->send($sum);
shutdown($client_socket, 1);
}

$socket->close();

Connection is established but $data1 and $data2 are emplty. It is not waiting at:

Code
$client_socket->recv($data1, 1024); 
$client_socket->recv($data2, 1024);



Client Code


Code
use IO::Socket::INET; 

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => '7777',
Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);

print "sent data of length $size\n";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";

print "Enter the first number\n";
my $num1 = <STDIN>;
$socket->send($num1);

print "Enter the second number\n";
my $num1 = <STDIN>;
$socket->send($num1);

$response = "";
$socket->recv($response, 1024);
print "Sum = $response\n";


$socket->close();


Please guide me. I am working in windows environment.

Zulfi.


(This post was edited by zak100 on Aug 8, 2014, 3:02 AM)


zak100
User

Aug 8, 2014, 2:59 AM

Post #2 of 4 (856 views)
Re: [zak100] TCP socket not receiving [In reply to] Can't Post

Hi,
I have changed the code but still i am not getting data at server.


Code
use IO::Socket::INET;  

# auto-flush on socket
$| = 1;

# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";


# waiting for a new client connection
my $client_socket = $socket->accept();

# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";

while (1){
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "received data: $data\n";

# write response data to the connected client
$data = "ok";
$client_socket->send($data);

# notify client that response has been sent
# my $data1;
# my $data2;
# my $sum;
$client_socket->recv($data1, 1024);
$client_socket->recv($data2, 1024);
print "data1 = $data1 and data2= $data2\n";

$sum = $data1 + $data2;
$client_socket->send($sum);
#shutdown($client_socket, 1);
}
shutdown ($client_socket,1);
$socket->close();


And the client code is:


Code
use IO::Socket::INET;  

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => '7777',
Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);

print "sent data of length $size\n";

# notify server that request has been sent
#shutdown($socket, 1);

# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";

print "Enter the first number\n";
my $num1 = <STDIN>;
$socket->send($num1);

print "Enter the second number\n";
my $num1 = <STDIN>;
$socket->send($num1);

$response = "";
$socket->recv($response, 1024);
print "Sum = $response\n";


$socket->close();


I am getting following output:

Server Side output

Server waiting for client connection port 7777
connection from 127.0.0.1:34644
received data: hello World (Note: this is not copy paste)

Client output

Connected to Server
send data to the server
sent data length 11
received response :ok
Enter the first number
10
enter the second number
20
-------------
The numbers 10 & 20 are not received at Server. Somebody please guide me. My PC is not connected directly to internet.I am working on Linux and posting through Windows PC.

Zulfi.


(This post was edited by zak100 on Aug 8, 2014, 3:05 AM)


FishMonger
Veteran / Moderator

Aug 8, 2014, 8:09 AM

Post #3 of 4 (842 views)
Re: [zak100] TCP socket not receiving [In reply to] Can't Post

There are several problems.

1) The main reason for the failure to communicate is the IP address. Don't use the loopback address. Use the IP address of the server in both scripts.

2) You're missing 2 very important modules/pragmas which should be in every script.

Code
use strict; 
use warnings;

These 2 statements should be the first ones in the script.

They will point out a number of problems that need to be fixed, such as;

Code
Global symbol "$data1" requires explicit package name at ./server.pl line 44. 
Global symbol "$data2" requires explicit package name at ./server.pl line 45.
Global symbol "$data1" requires explicit package name at ./server.pl line 46.
Global symbol "$data2" requires explicit package name at ./server.pl line 46.
Global symbol "$sum" requires explicit package name at ./server.pl line 48.
Global symbol "$data1" requires explicit package name at ./server.pl line 48.
Global symbol "$data2" requires explicit package name at ./server.pl line 48.
Global symbol "$sum" requires explicit package name at ./server.pl line 49.
Execution of ./server.pl aborted due to compilation errors.



zak100
User

Aug 11, 2014, 12:48 AM

Post #4 of 4 (805 views)
Re: [FishMonger] TCP socket not receiving [In reply to] Can't Post

Hi,
Thanks. I have solved this problem:
The server code is:

Code
 
[lab6@localhost socket]$ cat serv1.pl
use IO::Socket::INET;
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);

$|=1;
#TCP socket using accept

my $socket = new IO::Socket::INET( LocalHost => '127.0.0.1',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1);

die "cannot create socket $! \n" unless $socket;
print "server waiting for client connection on port 7777\n";

my $client_socket;
my $client_address;
my $client_port;
my $data;
my $sum;
my $data1;
my $data2;

while(1) {
$client_socket = $socket->accept ( );
$client_address = $client_socket->peerhost();
$client_port = $client_socket->peerport();
print "connection from $client_address: $client_port\n";

$data = "";
$client_socket->recv($data, 1024);
print "received data : $data\n";


$data = "ok";
$client_socket->send($data);

$client_socket->recv ($data1, 1024);
$client_socket->recv($data2, 1024);
print "data1 = $data1 and data2 = $data2\n";

$sum = $data1 + $data2;
$client_socket->send($sum);
#shutdown ($client_socket, 1);
}
shutdown ($client_socket,1);
$socket->close();

[lab6@localhost socket]$


and client code is:


Code
[lab6@localhost socket]$ cat client1.pl 
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);

use IO::Socket::INET;

my $socket = new IO::Socket::INET(
PeerHost => '127.0.0.1',
PeerPort => '7777',
Proto => 'tcp',
);

die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

my $req = 'hello world';
my $size = $socket->send($req);
my $num1;

print "sent data of length $size\n";

#shutdown ($socket, 1);

my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";

print "Enter the first number\n";
$num1 = <STDIN>;
$socket->send($num1);

print "enter the second number\n";
$num1 = <STDIN>;
$socket->send($num1);

$response = "";
$socket->recv($response, 1024);
print "sum = $response\n";

$socket->close();


[lab6@localhost socket]$



Thanks for help.

Zulfi.

 
 


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

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