Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Io Socket Example

by mlh2 (Initiate)
on Dec 10, 2004 at 18:41 UTC ( #413936=perlquestion: print w/ replies, xml ) Need Help??
mlh2 has asked for the wisdom of the Perl Monks concerning the following question:

Can someone show me a working example of IO socket, I need a server and a client, the server opens up a port and listens for a client to talk then does something (say write a file) with what is said, when the client is done, the server goes backt to listening again. http://www.perlfect.com/articles/sockets.shtml I looked at that and it does not seam to work. I am running perl 5.8

Comment on Io Socket Example
Re: Io Socket Example
by zejames (Hermit) on Dec 10, 2004 at 18:52 UTC

    At first glance, the article you provide is correct. Be sure however to change the hostname ('LocalHost' and 'PeerAddr' in the article) to IP addresses or hostnames that exist in your current network.

    If you just want to test, use '127.0.0.1' for both

    .

    --
    zejames
Re: Io Socket Example
by dba (Monk) on Dec 10, 2004 at 18:56 UTC
    Network Programming with Perl examples will help you
Re: Io Socket Example
by jobi (Scribe) on Dec 10, 2004 at 18:59 UTC
Re: Io Socket Example
by JoeJaz (Monk) on Dec 10, 2004 at 19:26 UTC
    Hi, I had to make a proxy server for a CS class. It uses perl and IO::Sockets. Here is my code. Hopefully it will be useful since it is implements both server and client code (the nature of a proxy server). If you are doing this for a class assignment yourself, please only use the code for reference. Otherwise have fun with it. I'm not that good of a perl programmer so all of the experts I'm sure will find flaws with it :-)
    #!/usr/bin/perl -w ################################# # Jaz Server Proxy ################################# # Joe Jaisnski # Networking # Due: Oct 5 2004 ################################# $|=1; # flush buffer use strict; # enable strict mode to require var declara +tion use IO::Socket; # use the IO::Sockets library my $server_port = 7788; # define the port that the proxy will run o +n my ($server); # declare the server variable # establish the server socket my $socket_server = IO::Socket::INET->new( Listen => 5, LocalPort => $ +server_port, Reuse => 1, Proto => 'tcp' +) or die "cannot connect to port $server_port: $!"; while ($server = $socket_server->accept()) # loop while the file hand +le is true (infinate loop) { my ($response_all, $content_length) = ("", 1000); # declare some +vars my (@request_headers, @response_all); # declare some +vars local $/ = "\015\012"; # set the loop +break string while (<$server>) # while the soc +ket is being accessed { chomp; # remove the newl +ine at the end of each line last if $_ eq ''; # break loop if i +nput is empty push(@request_headers, $_ . "\n"); # push the input +onto an array } my ($host, $port) = $request_headers[1] =~ m/^Host:\s*([\w\d\.\-]+ +):?(\d*)/mi; # parse host and port my ($path) = $request_headers[0] =~ m#^GET (.+) HTTP/1.[01]# ; + # parse out path # replace micorsoft.com with sourceforge.com if the host contains +MS or is empty if (($host =~ "microsoft.com") or ($host eq "")) { $host = "www.so +urceforge.net"; $path = "/";} if (!defined($port) or ($port eq '')) { $port = "80"; } # ch +eck to see if port is defined $request_headers[0] = "GET $path HTTP/1.1\n"; # se +tup replacement GET header $request_headers[1] = "Host: $host:$port\n"; # se +tup replacement Host: header # establish the client socket with newly determined host my $socket_client = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Prot +o => 'tcp') or die "cannot connect to port $port at $host: $!"; print $socket_client join('', @request_headers) . "\n"; # pr +int headers to the socket my $client; # define scalar while($client = <$socket_client>) # read in retrieved content fr +om the client socket { print $server $client; } # print the content to the bro +wser $socket_client->close; # close the connection $server->close; # close the connection ($host, $port, $path) = (undef, undef, undef); # undefine the ho +st port and path vars }
Re: Io Socket Example
by Your Mother (Canon) on Dec 10, 2004 at 22:50 UTC

    When you think server:client it's worth checking on POE. Really nice framework that takes the grunt work out of it. And if you get stuck, just ask here and Rocko might rescue you.

Re: Io Socket Example
by zentara (Archbishop) on Dec 11, 2004 at 12:33 UTC
    If you don't want to mess with the low level sockets stuff, try Net::EasyTCP. It make socket connections easy. It allows you to pass hashes across the socket, encrypt the connection, and ask for port passwords. It greatly simplifies things.

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-12-19 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls