Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl Chat server client hangs up
    text
    copied!<p>I'm trying to make a chat server program in perl where the client enters some text and the server responds back with the sum of the ASCII value of the input. However, there is one problem. The server is processing the data right but the client is not receiving it. Here is the code:</p> <pre><code>#!usr/bin/perl #client.pl use strict; use warnings; use IO::Socket; $| = 1; print "Client Program\n"; my $lp = 12000; my $client_socket = new IO::Socket::INET ( PeerAddr =&gt; '127.0.0.1', PeerPort =&gt; $lp, Proto =&gt; 'tcp' ) or die "Cannot create the socket: $!\n"; print "Server connected at port $lp \n"; print "Enter the text to sent to the server: \n"; my $user_input = &lt;&gt;; chomp $user_input; print $client_socket; $client_socket-&gt;send($user_input); my $server_output; $client_socket-&gt;recv($server_output, 1024); print "ASCII Sum received: scalar(&lt;$client_socket&gt;)"; $client_socket-&gt;close(); </code></pre> <p>The client just hangs up after sending the data (I can say that because I see the text received in the server program. However, the server processing doesn't show up until I terminate the client). I am not getting what is going wrong here. I can post the server program too but nothing seems to be wrong with it.</p> <p>Here is the server program:</p> <pre><code>#!/usr/bin/perl #server.pl use strict; use warnings; use IO::Socket; $| = 1; print "Server Program\n"; my $lp = 12000; my $server_socket = new IO::Socket::INET ( LocalHost =&gt; '127.0.0.1', LocalPort =&gt; $lp, Proto =&gt; 'tcp', Listen =&gt; SOMAXCONN, Reuse =&gt; 1) or die "Cannot create the socket: $!\n"; my $sum_ASCII = 0; print "Server started at port $lp \n"; print "Press Ctrl+C to stop the server\n"; print "Waiting for client to connect.. \n"; # or die sprintf "ERROR:(%d)(%s)(%d)(+%s)", $!,$!,$^E,$^E while (my $new_client = $server_socket-&gt;accept()) { my $addr = $new_client-&gt;peerhost(); my $port = $new_client-&gt;peerport(); print "Connected to client at $addr at port $port "; while(&lt;$new_client&gt;) { print "Following is the text entered by client: \n"; print "$_ \n"; my $len = length($_); print "The length of the string is: $len\n"; my @char_array = split(//); #print "@char_array"; print "Initially sum is $sum_ASCII\n"; for (my $i = 0; $i&lt; $len; $i++) { print "Adding ASCII of '$char_array[$i]'.. "; $sum_ASCII = $sum_ASCII + ord($char_array[$i]); print "\t Sum = $sum_ASCII\n"; } print "\n\nTotal sum of ASCII values of the all the sent characters is: $sum_ASCII\n" } print "Sending the sum to the client.. \n"; my $np = 98765; my $client_socket = new IO::Socket::INET ( PeerAddr =&gt; '127.0.0.1', PeerPort =&gt; $np, Proto =&gt; 'tcp' ) or die "Cannot create the socket: $!\n"; print "Client connected at port $np \n"; $client_socket-&gt;send($sum_ASCII); $client_socket-&gt;close(); print "\nClient now disconnecting..\n"; close $new_client; print "\nWaiting for new client to connect.. \n"; </code></pre> <p>}</p> <pre><code>#my $np = 12345 #my $client_socket = new IO::Socket::INET ( #LocalAddr =&gt; '127.0.0.1', #PeerPort =&gt; $np, #Proto =&gt; 'tcp' #) or die "Cannot create the socket: $!\n"; #print "Client connected at port $np \n"; #$client_socket-&gt;send($sum_ASCII); #$client_socket-&gt;close(); #print "Enter the text to sent to the server: \n"; #my $user_input = &lt;&gt;; #chomp $user_input; #print $client_socket; #$new_client-&gt;send($sum_ASCII); $server_socket-&gt;close(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload