Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am working on Fedora 20. I am getting Client Server Communication. But when i am sending integer values, server is not doing sum. My Server code is:
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 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 k
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.
You might have received a single byte (perhaps 4... 3 nulls and byte value 30) in the client. The sum of 10+20 is 30, which is a record separator in ASCII.
You can check the length of $response in the client to find out. If you got 1 (or 4), then you need to convert the sum into a string before sending it to the client.
--- Sorry. I was wrong in my inspection...
Actually, when I directly tested your code, it worked just fine, other than not detecting that the client closed the connection (I believe the "while (1)" needs to go before the "$socket->accept()" line.
Hi,
I tried but still i am not getting correct response. Server is not able to receive client's data (two integer values for sum).
My server coder is:
Code:
[lab6@localhost socket]$ cat serv1.pl
use IO::Socket::INET;
$|=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";
while(1) {
my $client_socket = $socket->accept ( );
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address: $client_port\n";
my $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($data1, 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 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);
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";
my $num1 = <STDIN>;
$socket->send($num);
print "enter the second number\n";
my $num1 = <stdIN>;
$socket->send($num);
$response = "";
$socket->recv($response, 1024);
print "sum = $response\n";
$socket->close();
[lab6@localhost socket]$
I am getting following output at server side:
Code:
lab6@localhost socket]$ !p
perl serv1.pl
server waiting for client connection on port 7777
connection from 127.0.0.1: 37405
received data : hello world
data1 = and data2 =
^C
[lab6@localhost socket]$ !
and at client :
Code:
perl client1.pl
connected to the server
sent data of length 11
received response: ok
Enter the first number
20
enter the second number
30
^C
I got this information from one another forum. I am now getting following errors:
Code:
[lab6@localhost socket]$ perl serv1.pl
Global symbol "$data1" requires explicit package name at serv1.pl line 33.
Global symbol "$data1" requires explicit package name at serv1.pl line 34.
Global symbol "$data1" requires explicit package name at serv1.pl line 35.
Global symbol "$data2" requires explicit package name at serv1.pl line 35.
Global symbol "$sum" requires explicit package name at serv1.pl line 37.
Global symbol "$data1" requires explicit package name at serv1.pl line 37.
Global symbol "$data2" requires explicit package name at serv1.pl line 37.
Global symbol "$sum" requires explicit package name at serv1.pl line 38.
Global symbol "$client_socket" requires explicit package name at serv1.pl line 41.
Execution of serv1.pl aborted due to compilation errors.
[lab6@localhost socket]$
Hi,
Its now running. I am now checking multiprocessing.
The running code is: server.pl
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.pl 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]$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.