p4R4d0x has asked for the wisdom of the Perl Monks concerning the following question:

Is this possible?

I now have a server, that opens and listens on the specified port, and a client that connects, and sends a message to the server, but the specifications for what I'm creating require a "handshake" between the client and server before the message is transferred, requiring data to be sent both ways across the connection, and I can't get anything to work, or find any support for such a task elsewhere, so I was wondering if such a connection is even possible in Perl using IO::Socket, and what I would need to do for this, or if I should research another way to set up the socket connection.

Thank you!

  • Comment on Is it possible to create a socket client and server that can send information back and forth using IO::Socket?

Replies are listed 'Best First'.
Re: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
by ikegami (Pope) on Oct 26, 2009 at 04:51 UTC

    requiring data to be sent both ways across the connection

    Sockets are bidirectional, so send data as you wish. What's the problem?

    You say you know how to establish a connection, so presumably the problem you are having is with the handshaking you've said nothing about.

      Sorry! To clarify the "handshaking," it's simply the server, once it receives a connection, tells the client "HELLO 1.0" and the client, if using version 1.0 will respond "HELLO 1.0" or "QUIT" if anything other than "HELLO 1.0" is received.

      I currently have a side that is able to send, and a side that is able to received, but I can't seem to create a socket object using IO::Socket that can both send and receive data. Any help is appreciated!

        I currently have a side that is able to send, and a side that is able to received

        You are mistaken. Sockets are bidirectional, which means both sides can send and receive.

        it's simply the server, once it receives a connection

        I wonder if you are trying to get things the wrong way around? Generally, assuming you are using SOCK_STREAM, the server waits on a connection and a first message from the client - it is usually the client who "speaks" first (it does not have to be anything deep and meaningful). The server then responds, and waits for a reply. Maybe you have both sides sending or recieving at the same time?
Re: Is it possible to create a socket client and server that can send information back and forth using IO::Socket?
by weismat (Friar) on Oct 26, 2009 at 10:19 UTC
    Client is just the one who initiates the session and the server is the one which responds to the client and handles multiple client at the same time.
    I think you should provide some code, so that we can look at it.

      Maybe I'm just going about sending data back in the wrong way? I'm unsure of how to get the handle to send from the 'server' to the 'client'. Here's the socket portion of what I have so far:

      'Server'

      my $clientSocket = new IO::Socket::INET( PeerAddr => $ipSend, PeerPort => $sendPort, Proto => 'tcp', ); $clientSocket or die "Could not create socket:$!\n"; print $clientSocket "HELLO 1.0"; sleep(3); close $clientSocket;

      'Client'

      my $servSocket = IO::Socket::INET->new( #LocalHost => 'localhost', LocalPort => $listenPort, #7890 Proto => 'tcp', Listen => 2, Reuse => 1 ); $servSocket or die "Could not create socket:$!\n"; my($tempSocket, $clientAddr, $buffer); while (($tempSocket, $clientAddr) = $servSocket->accept()) { my ($clientPort, $clientIP) = sockaddr_in($clientAddr); my $clientIPNum = inet_ntoa($clientIP); my $clientHost = gethostbyaddr($clientIP, AF_INET); while (defined ($buffer = <$tempSocket>)) { print $buffer; if ($buffer =~ m/^HELLO (\d+.\d+)/) { my $version = $1; if ($version == "1.0") { #This doesn't work #Want to send "HELLO 1.0" back through the socke +t #Is the $tempSocket handle just wrong, or #can I not use print to do this? print $tempSocket "HELLO 1.0"; } } last; } }

        Assuming you meant to send a newline after the HELLO,

        Side that initiates the handshake:

        print $sock "HELLO 1.0"; chomp( my $greet = <$sock> ); die if $greet ne 'HELLO 1.0'; ...continue...

        Side that responds to the handshake:

        chomp( my $greet = <$sock> ); die if $greet ne 'HELLO 1.0'; print $sock "HELLO 1.0"; ...continue...

        Without the newline, you'll need touse something other than <>. It won't return until a newline is received (by default).

        Two obvious problems:
        - Your client does not flush the buffer - thus nothing is sent, either call directly or check the autoflush property.
        - Your server can handle only client and needs to flush as well.
        Not sure if this is the cause of your problems, but there are some coding issues you should fix:
        if ($buffer =~ m/^HELLO (\d+.\d+)/) { my $version = $1; if ($version == "1.0") {
        should be:
        if (my ($version) = $buffer =~ m/^HELLO (\d+\.\d+)/) { ## Esca +pe the "." if ($version eq "1.0") { # Do a "text" compare, no +t numeric

             Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)