Create a TCP/IP Connection

Hello,

I am trying to write a script in Perl which will send some data from a UNIX Box to a windows box. I am planning to create a TCP/IP communication port for the same. How do I go about this? Kindly help.

Regards,
Garric

this module would be of help to you: IO::Socket::INET

The perlipc manual page basically has the stuff you need, although you might need to read up on the background info if you don't understand it on the first read; it really assumes you are familiar with basic socket programming. It all seems quite mysterious until you learn to ignore most of the lingo (-: But try the very simplest examples there; they should suffice for your needs.

Can you please one such simple example. The ones I tried dont seem to work.For eg

#!/usr/intel/bin/perl -w
use IO::Socket;
$remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "daytime(13)",
)
or die "cannot connect to daytime port at localhost";
while ( <$remote> ) { print }

That one works here, do you get an error message? Maybe your server doesn't run a daytime service on port 13. Mine doesn't, so I get "cannot connect to daytime port", but adapting it to use a port number for which there is a server allows it to work just fine; there is certainly nothing wrong with the code. Changing it to use port 22, for example, it will print the SSH banner from the SSH server (and then nothing, because it obviously doesn't speak that protocol -- that's why they chose daytime, because it uses such a mind-bogglingly stupid, er, I mean, easy-to-understand demo protocol.)

If you don't have previous experience with network protocols, play around with netcat for a while. Maybe it's actually all you will ever need.

A:\WINDOWS> NETCAT.EXE -l -p 4343

vnix$ nc windows 4343
Stuff I type here will be seen by my Windoze box!
Oh my^C

Hello guys,

I can't get this to work. Can you please show me an example (or lead me to one) of how I can setup a TCP Port between 2 unix machines. The simplest of examples will help me build on it. Please help.

Regards,
garric

It doesn't get much simpler than that. If you can't get the traffic to flow, you have a firewall or something in between the endpoints.

Try get it to work just from localhost to localhost on the Unix box for a start. Open two xterms, run nc -l -p 4343 in window 1 and connect to it with nc localhost 4343 from window 2. Now whatever you type into window 2 should appear at the other end in window 1. Press ctrl-C in window 2 to terminate both.

netcat seems to be disabled for me. I actually want to run something on many machines simultaneously and then create a tcp port into which all machines will write the results into .. into one common location. How can I do this?

Regards,
garric

The Perl examples should work as well; just replace whatever port number they use (daytime in the example we discussed before) with a random port number which is larger than 1023, such as port 4343, as in the Netcat examples above. (Opening ports below 1024 for listening usually requires root privileges.)

I got the simple netcat example to work on the same machine. But when i try running it through a script ( Perl ) with a port higher than that 1024 ,
it just 'dies' with the error.

(Eg :
$remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "locahost",
PeerPort => "4343"
) or die "cannot connect to port 4343 at localhost";

Add $! to the error message to find out why it's dying. Also check your spelling of "localhost".

Connection refused it is. Though I am running it as root

--- cannot connect to port 4343 at localhost : Connection refused at /tmp/test.pl line 3---

Hmm, looks like you just get "Invalid argument" in $! if you use a host name which doesn't exist, so that doesn't help much. Still, a good habit to always include $! in suicide notes.

Connection refused means you forgot to start the listener on port 4343. You can't connect if nobody has opened the port for listening. Just run nc -l -p 4343 on there for now. Once you have the client working, you can work on replacing the server with something more to your liking.

Thanks .. I got this to work, finally. But this requires me to be there for the input. How do i re-direct the output of a script into a port?

Also, how does the port handle multiple requests coming in at the same time?

Thanks for your help.
Regards,
garric

Go back to the perlipc man page and look at the code for some simple servers.

A common model is to fork a new copy of the server for each incoming request. For really high performance, some architectures -- Apache being the prime example -- create forks in advance, so there's a pool of server instances waiting for incoming connections. But unless you envision hundreds or thousands of concurrent connections (in which case you are in too deep anyway), the much simpler demo daemons in the manual page should be sufficient.

Also to avoid reinventing the wheel, look at some of the demo scripts which are included with Netcat. I recall there was a simple server demo there as well.

What did u mean when u said " I am in too deep anyways" because I am actually looking for huge concurrency.

Sounds like you shouldn't be coding up something by yourself from scratch then. See if you can co-opt the Apache code base to run a server on. Probably make sense to design a protocol which resembles HTTP, or simply just use HTTP. Maybe your clients could do a simple HTTP POST to upload their reports.

era,

How do you think I can start off doing this? Can you lead me to a few pointers, please? I do not have a strong bckground this? Please help.

Regards,
garric

A basic CGI script running under Apache sounds like it should work at least for prototyping. Try something like mod_perl and/or fastcgi if it's not fast enough by itself.

The clients don't really need to run a proper browser, just a telnet to port 80 of the server to dump a POST request with the data you want from them.