views:

283

answers:

7

I need to move some data from one machine to another. Is it a good idea to write a client server app using sockets in Perl to do the transfer? Will I have problems if one side is written in Java?

I mean, should I be aware of any issues I might face when I try to attempt the above?

+3  A: 

You don't say whether you need to implement your application to support any particular protocol or whether you need to implement a home grown protocol. The networking support in Perl is flexible enough to support either (or most places in between).

At the low level socket end, your code is going to be fairly similar whatever language your are using - BSD socket APIs are pretty well the same everywhere they are supported. The support you need for this is built into Perl but low level socket programming can be frustrating - it's very low level.

However, Perl's standard library contains the Socket module which is rather easier to use (and well documented).

If you need to implement an existing protocol you may well find that it has already been implemented. For example Net::Telnet implements command/response protocols (like Telnet) making a client app trivial.

Searching CPAN may save you a lot of pain. Look at modules in the Net::* hierarchy

Nic Gibson
why would i want to write a app to support a particular protocol..i just want to move some data from machine A to machine B using sockets in perl.Now is this a good idea or not? was my question..
not-exactly-a-unixhater
You didn't say what the client is for. It could have been a web user agent, so you could have supported HTTP. Actually, if you want to move some data, you might just use one of the simple HTTP server modules on CPAN and the LWP client tools. You could probably write the code in perhaps 200 lines max.
Nic Gibson
A: 

You are basically asking two questions:

  1. Is Perl a proper language for socket communication?
  2. Is Perl a proper language for UI?

Referring to e5's answer, Perl is indeed a string-centric language with a focus on readable strings, less well equipped to handle binary data. Thus the answer probably lies in the questions: Is your communication string based? Is your UI string based?

If doing binary interaction through a socket, well, you probably could be doing better than Perl (not talking about C, but maybe C-ish languages). If you want to do graphical user-interaction you probably reach faster results by choosing one of the higher languages that focus more on gui interaction. (Java-ish might be the thing here.)

Don Johe
Perl is well equipped to deal with binary data, see the pack and unpack functions. And I would much rather use Perl and Gtk2 than Java and Swing or AWT to write GUI apps.
Chas. Owens
OK, then I have to claim ignorance and beg forgiveness to have misjudged the perl. May I add in defence, that one should use the language he is most proficient in. (Up to some usability issues, I refer to assembler and chiseling a GUI out of processor registers.)
Don Johe
Who mentioned any UI?
innaM
Why -1 for this answer..there is nothing wrong with the answer..
not-exactly-a-unixhater
I infer "UI" from "client app". But I'd like to stress that CLI or GUI both comprise UI, and there are many shades inbetween (CLI-completion, curses, screen). [reputation values are fluctuating today]
Don Johe
I imagine the -1 is for the answer 'less well-equipped to handle binary data'. Perl doesn't really differentiate between the two.
Nic Gibson
A short list of non-gui network clients you have probably used: ftp, telnet, ssh, wget, curl, and so on.
Chas. Owens
+1 @not-exactly-a-unixhater The reason someone voted down this answer is because some people in this thread are voting downing all answers that make negative statements about perl. I'm hoping to hit -25 on my answer by days end.
e5
@e5 : Those 25 people are no programmers for sure..becoz thy think perl is the best language in the world and will not tolerate other who say otherwise..indeed like every other programmer..thy will come to their senses..when perl dies out..
not-exactly-a-unixhater
+4  A: 

I can only think of one gotcha off the top of my head: most text based network protocols use CRLF for line endings, but Perl on UNIX type machines assumes LF endings by default, this means you will need to change the input and output record separators if you want to use readline (aka <>) and print (also beware of printf, since it doesn't use the output record separator). Of course, if you are going to use a pre-existing protocol, there is probably already a Net::<PROTOCOL NAME> module on CPAN, so you won't need to worry about that. If you are designing your own protocol, I would keep the CRLF convention because it makes it easy to debug the server with telnet (which is really the last valid use for that program).

Chas. Owens
So if i write the client in java and the server in perl, will i have issues with the messages that i pass around?
not-exactly-a-unixhater
So long as you develop a protocol and follow it on both ends you should be safe.
Chas. Owens
Socket.pm gives you constants $CR, $LF and $CRLF.
Sinan Ünür
A: 

Write socket communication in Perl is relatively easy. Do it right and reliable is big pain even CPAN modules are examples of error prone code. It depends of your expectations.

Hynek -Pichi- Vychodil
+1  A: 

I don't think you're gonna have any major issues that you won't have by not using Perl. Even performance will be comparable to other solutions due to network latencies. You might want to look at POE framework. It makes writing such components a breeze.

zakovyrya
+5  A: 

Short answer: Using a Perl program as the client or server is just fine. Your only problem might be your personal skill and experience level, but after you do it you know how to do it. :) Most of the problem is choosing how you need to do it, not the technology involved. Perl isn't going to be the problem, but it doesn't have an advantage over other languages either.


As some have already noted, the socket portion of the problem is going to be the same in most languages since almost everything uses the BSD stuff. Perl doesn't have any roadblocks or special gotchas for that. To move data around you create one side to listen on a socket and the other to open a connection and send the data. Easy peasy. You might want to check out Lincoln Stein's Network Programming with Perl for that bit. That can get you the low-level bits.

For higher-level networking, POE is very useful and easy to work with once you get started. It's a framework for dealing with event-driven programming and has many plugins to easily communicate between processes. You might spend a little time learning it, but it gives a lot back too.

If you aren't inventing your own protocol, there's most likely already a Perl module that can format and parse the messages.

If you just want to transfer data, there are several things you can do. The easiest in concept might be just to write lines to the socket and read them as lines from the other end. A bit more complicated than that is using something like Data::Dumper, YAML, or JSON to serialize data to text and send that. For more complex things, such as sharing Perl objects, you might want to use Storable. You freeze your objects, send them as data over the network, then thaw them on the other side.

If you want to implement your client and server in different languages you have a bit more work to figure out how they'll talk to each other. The socket stuff is mostly the same, but a Java server won't understand the output of Perl's Storable (it's possible, but you'll have to parse it yourself and that's no good :). If you do everything right, neither side should care what you used on the other side.

brian d foy
Good answer...i was expecting something like this..thanx..
not-exactly-a-unixhater
+1  A: 

It probably depend on a few factors. Does speed or responsiveness matter? Are you moving data between they same type of machines (Unix to Unix, Windows to Windows)? What type of data are you trying to move (Text or Binary)? What is knowledge about sockets and what languages do you have experience?

I have sent and received binary data over PERL sockets from differing applications, but I don't have much experience with the text processing over sockets from differing machines. If you are moving data between machine you need to keep in mind the way the data is marshalled and if it is packed or aligned on some byte boundry. I have not exchanged data with a Java programs, but is should be similiar.

It probably would help to have some experience with PERL, and I would recommend looking at the examples in the "camel" book. I have used the ones in the book as a starting point and made modification for what I needed to achieve. You may have to consult some other areas of the book if you are dealing with binary data, or to help in doing translations for sending data.

Glenn