Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl Socket programming problem

by sarf13 (Beadle)
on Nov 30, 2013 at 20:20 UTC ( [id://1065082]=perlquestion: print w/replies, xml ) Need Help??

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

Help me out to understand the socket programming.For passing data from one machine to another machine over TCP/IP protocol. What all basic things i have to take care for creating socket script?? Do I have to always run below server script on server end?? Is there any way to run server script remotely?? Is there any way to pass data across the machine running both server and client script at one machine only??

server script

use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7890', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);

client script

use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '7890', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; my @val = qw(Hello there! this is socket); print $sock @val; close($sock);

sorry I was not very much cleared my problem above

actually my requirement is simple to pass data from one machine let say M1 (client) to machine M2 (server) in the form of defined binary data set (Binary structure data which define for system to accept).

here my problem is I cannot execute my server script (which I have given above) on machine M2 (server) end. Is there any other way to pass data from M1 (client) to M2 (server) without executing server script

Replies are listed 'Best First'.
Re: Perl Socket programming problem
by NetWallah (Canon) on Nov 30, 2013 at 23:57 UTC
    Most of your questions can be answered by reading "perldoc perlipc" (or perlIPC TCP sockets) and trying things out.

    If you have an actual problem or issue understanding a concept, or error, please post back here, and we will be happy to assist.

    Specifically:

    • Do I have to always run below server script on server end? : YES, if yo want it to be available to connect to.
    • Is there any way to run server script remotely? : ssh on Linux, psexec on WIndows
    • Is there any way to pass data across the machine running both server and client script at one machine only?: YES - use "localhost". Your code already has that.

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

      Is there any way to pass data across the machine running both server and client script at one machine only?: YES - use "localhost". Your code already has that.

      On above mentioned point how i can pass data to server let say machine M2 from machine M1 (client) without running the server script since i don’t have control on server side and i cannot execute any script over server

      based one given parameter like IP, port, protocol(TCP) i need to pass massage packet(data) I have to login to server and send data and capture return server value. Could you please help me out on this

        If you cannot run code on the server, you will need to use an existing service provided by the server. (It is not a server unless it provides services).

        One example of a service is a web server. If your server accepts http or https connections, you can send it data (packets) using that protocol.

        You need to have an existing, usually permanent listening endpoint, in order to be able to send on-demand data.
        This is what the server is supposed to provide.

        When you have information on the port, protocol, and authentication required by that server endpoint, you can plan on how to connect to it.

                     When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: Perl Socket programming problem
by kcott (Archbishop) on Dec 01, 2013 at 00:38 UTC

    G'day sarf13,

    This seems to be a very general request. I've provided some links below. Please read through them then post more specific questions indicating exactly what you don't understand or what code you're having difficulties with: the guidelines in "How do I post a question effectively?" should help you achieve this.

    "Help me out to understand the socket programming.For passing data from one machine to another machine over TCP/IP protocol. What all basic things i have to take care for creating socket script??"

    Take a look at "perlipc - Perl interprocess communication". In particular:

    "Do I have to always run below server script on server end??"

    I don't understand what you're asking with this one. Is it about the specific code you've posted or where you'd run that code? Perhaps after reading the above links you can rephrase this indicating what you're actually attempting to do and why.

    "Is there any way to run server script remotely??"

    You can run Perl scripts remotely. A good place to start might be Net::OpenSSH. This also provides information about other modules, with similar functionality, that may be more suited to your needs. Again, a description of what you're specifically trying to achieve and why would be helpful in providing a better answer.

    "Is there any way to pass data across the machine running both server and client script at one machine only??"

    You can run a server and one or more clients on the same machine and pass data between them. As before, more details about what you're having problems with understanding or coding would be useful.

    -- Ken

Re: Perl Socket programming problem
by Laurent_R (Canon) on Nov 30, 2013 at 23:59 UTC
    I think you should explain what you need to do. Socket programming is quite low level, there are a number of higher level network protocols which are implemented in Perl (usually through Perl modules) and might give you what you need. Personally, the last time I had to do socket programming was in 1995, or about 18 years ago (it was not in Perl at the time, but in C). In brief, unless there is any specific reason for needing low-level network programming, try to think about higher level solutions.

      Yes you are very correct. Actually this is first time i am working on low level network programming. Actually system is accepted only on socket level data pass.

      can you tell me some high level protocols??If i am not wrong are you talking about web services(HTTP, HTTPS)??

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1065082]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found