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

Hi guys:

I want to build a tcp session between my computer and a remote server using specific port, and read data from the remote server. I see many available packages but a little confused about these.

Could you give a suggestion which package is best to use in Windows ?

  • Comment on How to build a tcp session with a remote server?

Replies are listed 'Best First'.
Re: How to build a tcp session with a remote server?
by BrowserUk (Patriarch) on Aug 22, 2011 at 09:24 UTC

    At the most basic level, IO::Socket::INET.

    If the remote server is of a particular known type of server -- http; ftp; DNS; etc. -- then look in the NET::* namespace to find higher level abstractions that may greatly reduce your programming effort.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to build a tcp session with a remote server?
by Marshall (Canon) on Aug 22, 2011 at 09:47 UTC
    It would help if you could explain exactly what you are going to be talking to?

    Perl can do an amazing job at tcp/ip communication and it will work great!
    But your client software needs to know the basic messaging protocol with the server.

    The general idea is that you connect to a server who is listening on some port. Then as the client, you send information and the server responds. And that is normally how it goes...request/reply...request/reply.

    A Perl client can do anything that a client written in C can do. At the end of the day, you need to explain what information you need to send to the server as the request and what you expect for it to send back as the response.
    Tell us that information and it is easy to write the client.

    There is no generic "hey, I can talk to any server" protocol.
    The protocol is server specific. There will be a way for the server to understand that all of the information for a request has been received. Then the server does its magic. There will be a way for the client to understand that all of the information for a response to its request has been be received.

      There is no generic "hey, I can talk to any server" protocol.
      Well, there is in the (still broad) scope of his question. In fact, the name of the protocol is even mentioned in the title of his question: tcp.
      The protocol is server specific.
      Only in the sense there in theory there exists "servers" that don't have a TCP/IP stack. I seriously doubt that connecting to such servers even crossed the mind of the OP.
        Tcip/ip is a transport layer. It is a way for the client to shovel data bits into a pipe and for the server to read those bits. This lower level protocol ensures that the bits that I send is what you get.

        The next level higher is like: how many bits from you constitutes "one thing"? Maybe a fixed 256 bytes constitutes one "message". Maybe some string of characters which end in "\n" constitutes one "message". Maybe some number of lines that end in "\n" and a null line "\n" after all of the relevant lines constitutes collectively one "message". This is part of how the client "talks" to the server.

        This is client<->server message protocol specific. Perl is great at "\n" terminated messages! I print one line to you and then I receive one line back. Perl can also deal with fixed byte length message packets.

        Anyway all of this is related to the application I/F between client and server.

Re: How to build a tcp session with a remote server?
by zentara (Cardinal) on Aug 22, 2011 at 11:52 UTC
    Try Net::EasyTCP For example code, see Indirect file transfer and Sockets-File-Upload with Net::EasyTCP and for a rudimentary messaging system, here is a client and server example, (lightly tested recently, but works with Perl 5.14.1)

    A server, must be started first

    #!/usr/bin/perl use Net::EasyTCP; #my @nocrypt = qw(Crypt::RSA); #too slow, but more secure #my @nocompress = qw(Compress::LZF); #just testing this feature $server = new Net::EasyTCP( host => "localhost", mode => "server", port => 2345, password => "ztest", donotcheckversion => 1, # donotencryptwith => \@nocrypt, # donotencrypt => 1, # donotcompresswith => \@nocompress, # donotcompress => 1, welcome => "Hi! Wecome to the test server!\n", ) || die "ERROR CREATING SERVER: $@\n"; $server->setcallback( data => \&gotdata, connect => \&connected, disconnect => \&disconnected, ) || die "ERROR SETTING CALLBACKS: $@\n"; $server->start() || die "ERROR STARTING SERVER: $@\n"; #################################################### sub gotdata() { my $client = shift; my $serial = $client->serial(); my $data = $client->data(); print "Client $serial sent me some data, sending it right back to them + again\n"; $client->send($data) || die "ERROR SENDING TO CLIENT: $@\n"; if ($data eq "QUIT") { $client->close() || die "ERROR CLOSING CLIENT: $@\n"; } elsif ($data eq "DIE") { $server->stop() || die "ERROR STOPPING SERVER: $@\n"; } } ##################################################### sub connected() { my $client = shift; my $serial = $client->serial(); print "Client $serial just connected\n"; } ################################################### sub disconnected() { my $client = shift; my $serial = $client->serial(); print "Client $serial just disconnected\n"; }
    and a client
    #!/usr/bin/perl use Net::EasyTCP; # a client $client = new Net::EasyTCP( mode => "client", host => 'localhost', port => 2345, password => ztest ) || die "ERROR CREATING CLIENT: $@\n"; my $encrypt = $client->encryption(); my $compress = $client->compression(); print "encryption method ->$encrypt\ncompression method ->$compress\n" +; my $encryption = $client->encryption() || "NO"; my $compression = $client->compression() || "NO"; print "Using $encryption encryption and $compression compression\n\n"; #Send and receive a simple string $client->send("HELLO THERE") || die "ERROR SENDING: $@\n"; $reply = $client->receive() || die "ERROR RECEIVING: $@\n"; print "$reply\n"; #Send and receive complex objects/strings/arrays/hashes by reference #%hash = ("to be or" => "not to be" , "just another" => "perl hacker") +; #$client->send(\%hash) || die "ERROR SENDING: $@\n"; #$reply = $client->receive() || die "ERROR RECEIVING: $@\n"; #foreach (keys %{$reply}) { #print "Received key: $_ = $reply->{$_}\n"; #} #Send and receive large binary data #for (1..4096) { #for (0..255) { #$largedata .= chr($_); #} #} #$client->send($largedata) || die "ERROR SENDING: $@\n"; #$reply = $client->receive() || die "ERROR RECEIVING: $@\n"; #$client->close(); $client->send('Hello from Joe') || die "ERROR SENDING: $@\n"; $reply = $client->receive() || die "ERROR RECEIVING: $@\n"; print "$reply\n"; while(1){ $input = <STDIN>; $client->send($input) || die "ERROR SENDING: $@\n"; $reply = $client->receive() || die "ERROR RECEIVING: $@\n"; print "$reply\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: How to build a tcp session with a remote server?
by JavaFan (Canon) on Aug 22, 2011 at 11:28 UTC
    Could you give a suggestion which package is best to use in Windows ?
    You're giving way to litte information to tell you what the best for you is, but I can give you the name of the package that allows you to write the simplest Perl program. Install the nmap utility set (it's not a CPAN or even a Perl package; it's set of C programs; Windows binaries are available). It comes with a utility ncat. If all you need to do is connect to a remote port and read its data:
    my $HOSTNAME = "..."; # Name of host to connect to. my $PORTNUMBER = ...; # Port to connect to. my $data = `ncat -C $HOSTNAME $PORTNUMBER`;
    Don't forget, code reuse doesn't stop at the CPAN boundaries!
Re: How to build a tcp session with a remote server?
by Marshall (Canon) on Aug 24, 2011 at 11:21 UTC
    Things have gotten a long way from the original question...
    Whew!
    I do not think that we need to get into a technical terminology discussion. That is pretty much irrelevant to the task at hand.

    I want to build a tcp session between my computer and a remote server using specific port, and read data from the remote server.
    You need to look for a spec on how this communication works. You should be looking for a description of the sequence of stuff that you need to send to the server and the sequence of stuff that the server will send back to you.

    If you are lucky, there is already some Perl module written that will do all of this. But, without knowing what specifically what that server application is and what data you expect back there is no way that I advise further. Just saying that I want a TCP/IP connection is just not nearly enough!