Hi

I have made a server and client code. I have some issues regarding this.

This is what I am trying to achieve :

1. Client to connect to server and send some string to server

2. After server reads the string and doing some series of actions send back a string to client.

3. Client replies back saying it got the message correctly.

A sample code is below

server.pl

#!/usr/bin/perl -w # server1.pl - a simple server use strict; use Socket; use IO::Socket; # use port 7890 as default use constant TEST_TCP_PORT => 7890; my $port = TEST_TCP_PORT; my $proto = getprotobyname('tcp'); sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "Test socket faile +d : $!\n"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "Test setsock +failed : $! \n"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "Test bind failed : $!"; listen(SERVER, SOMAXCONN) or die "Test listen failed : $!"; print "Server started on port $port \n"; # for each connection... my $client_addr; while ($client_addr = accept(CLIENT, SERVER)) { # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET) ; # tell who connected #print "Got a connection from: $client_host"," [$client_ipnum]\n"; logmsg "Got a connection from: $client_host"," [$client_ipnum]\n"; + #--------------------------------------------------------------------- +--------- # foreach (1 .. 10){ # print CLIENT "nO \n"; # CLIENT->flush(); # # } print CLIENT "Hello from the server \n\n "; CLIENT->flush(); my ($response,$temp); while ( defined ($response = <CLIENT>)){ #print length($response); print "Response from client :$response" ; $temp = $response; print "Temporary : $temp " ; print "Declined \n\n"; last ; } print "Temporary : $temp \n", length($temp) ; my @output = split (' ',$temp); print $output[0]; my $temp1 = $output[0]; print "Temporary : $temp1 " ; #print STDOUT "Sajan : ",length($response); print STDOUT "Sajan : ", length($temp1); if ( $temp1 eq "sajan" ){ print "yes"; print STDOUT "Authenticated : "; CLIENT->flush(); } #--------------------------------------------------------------------- +--------- close CLIENT; }
client.pl
#! /usr/bin/perl -w # client1.pl - a simple client use strict; use Socket; use IO::Socket; use constant TEST_SERVER => "temp.random.com"; use constant TEST_TCP_PORT => 7890; # initialize host and port my $host = TEST_SERVER ; my $port = TEST_TCP_PORT ; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!"; my $other_end = getpeername(SOCKET) or die "Couldn't identify other end: $!\n"; ($port, $iaddr) = unpack_sockaddr_in($other_end); my $actual_ip = inet_ntoa($iaddr); my $claimed_hostname = gethostbyaddr($iaddr, AF_INET); print "Connected to server : $actual_ip =>> $claimed_hostname \n"; + #-------------------------------------------------------- my $line; print SOCKET "sajan "; SOCKET->autoflush(); while ($line = <SOCKET>) { print SOCKET "Client says hellooooo toooo !!!"; SOCKET->autoflush(); sleep(11); print STDOUT "Sajan inside "; print $line; open my $tfh, " >> /tmp/sajan.tmp"; my $date = `date`; print $tfh "$date :: $line "; close $tfh }

The code here is not working properly to my purpose. Can someone guide me thru here ?


In reply to Socket deadlock doubt in code by sajanagr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.