LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-08-2014, 05:11 AM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 225

Rep: Reputation: 2
TCP Socket Programming: Sum Program


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.

Zulfi.
 
Old 08-08-2014, 05:30 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,706

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
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.

Last edited by jpollard; 08-08-2014 at 05:39 AM.
 
Old 08-11-2014, 12:53 AM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 225

Original Poster
Rep: Reputation: 2
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
 
Old 08-11-2014, 01:00 AM   #4
zak100
Member
 
Registered: Jul 2009
Posts: 225

Original Poster
Rep: Reputation: 2
Hi,
I used :
Code:
use strict;
use warnings;
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]$
Some body please guide me.

Zulfi.
 
Old 08-11-2014, 01:53 AM   #5
zak100
Member
 
Registered: Jul 2009
Posts: 225

Original Poster
Rep: Reputation: 2
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]$
Zulfi.
 
Old 08-11-2014, 02:42 AM   #6
zak100
Member
 
Registered: Jul 2009
Posts: 225

Original Poster
Rep: Reputation: 2
Hi,
I have run two clients:
Client1 output screen

Code:
   
[lab6@localhost socket]$ perl client1.pl
connected to the server
sent data of length 11
received response: ok
Enter the first number
and Client2 waits for Client1 to finish. It is not in accept loop
Code:
[lab6@localhost socket]$ !p
perl client1.pl
connected to the server
sent data of length 11
If i dont use TCP then if client1 sends 20 and client2 sends 30 then client1 would get output 50.

I think its working now.

Zulfi.
 
  


Reply


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
program to listen on TCP socket and start some program with I/O redirected to socket leniviy Linux - Software 4 08-12-2013 05:51 AM
Issue with tcp socket programming Eben1588 Linux - Networking 0 11-09-2011 05:54 AM
socket programming in KERNEL (TCP/IP) omkarlagu Programming 8 07-04-2011 01:42 AM
TCP/IP book for socket programming hubabuba Programming 3 10-17-2005 03:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:37 AM.

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