Sponsored Content
Full Discussion: perl and communications
Top Forums Shell Programming and Scripting perl and communications Post 302524085 by Chirel on Saturday 21st of May 2011 11:14:14 AM
Old 05-21-2011
On my understanding of a client / server communication, it's the server side that is listening to a port.

Here are some very simple example easy to find with google.

The server side script
Code:

#!/usr/bin/perl -w
# server.pl
#--------------------

use strict;
use Socket;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) 
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) 
   or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, pack( 'Sn4x8', AF_INET, $port, "\0\0\0\0" ))
       or die "Can't bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(NET_SOCKET, SOCKET)) {
    # send them a message, close connection
    print NEW_SOCKET "Smile from the server";
    close NEW_SOCKET;
}

Client side script
Code:
#!/usr/bin/perl -w
# client.pl
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;
my $server = "10.12.12.168";

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
   or die "Can't create a socket $!\n";
connect( SOCKET, pack( 'Sn4x8', AF_INET, $port, $server ))
       or die "Can't connect to port $port! \n";

my $line;
while ($line = <SOCKET>) {
    print "$line\n";
}
close SOCKET or die "close: $!";

 

4 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

linux communications modes

what r the ways in which a linux application communicate with an external system like linux or windows? (1 Reply)
Discussion started by: shil
1 Replies

2. Shell Programming and Scripting

Logging/Reading Interprocess Communications

Greetings, I'm posting this in the shell scripting forum because I'm hoping this can be done in BASH or PERL. If not, I'm still open to suggestions of other ways to do it: I've got an iPhone app that's sending some encrypted (SSL) traffic to a server and I'd like to be able to read the... (0 Replies)
Discussion started by: FiZiX
0 Replies

3. AIX

VIO and LPAR disk/fcs communications

:wall::wall::wall: 1. I have created an LPAR in the HMC. 2. I have allocated the storage from an Hitachi AMS2500 and assigned it to the host group. 3. I have zoned the LPAR and Storage on a Brocade 5100. (The zone sees the AMS) Next I activated the LPAR in the HMC, SMS mode for the mksysb... (3 Replies)
Discussion started by: Dallasguy7
3 Replies

4. UNIX for Dummies Questions & Answers

In UNIX which following type of file is used for network communications

a) FIFO B) SOCKET C) DIRECTORY D) BLOCK PLEASE SUGGEST ME THE ANSWER Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules. Please review the rules, which you agreed to when you... (0 Replies)
Discussion started by: raghugowda
0 Replies
SOCKET(1)                                                     General Commands Manual                                                    SOCKET(1)

NAME
socket - create a TCP or a UNIX domain socket and connect to stdin/out SYNOPSIS
socket [ -bcfqrvw ] [ -p command ] [ -B local address ] host port socket [ -bcfqrvw ] [ -p command ] /path socket [ -bcfqrvw ] [ -p command ] [ -B local address ] -s [ -l ] port socket [ -bcfqrvw ] [ -p command ] -s [ -l ] /path DESCRIPTION
Socket creates an Internet domain TCP or a UNIX domain stream socket and connects it to stdin and stdout. The host argument can be an Internet number in dot-notation (like ``130.149.28.10'') or a domain name. In this case it must be possible to resolve the name to a valid Internet address with gethostbyname(3). The port argument can be a port number or a service name which can be mapped to a port number by getservbyname(3). If an UNIX domain socket is wanted to be created instead of an Internet socket, specify the path instead of an internet (canonical domain named or dot-notated) host. The hostname is treated as a pathname if contains at least a single slash. I.e. if one wants to create or connect to a socket in the current directory, use ./filename to specify the connection point. OPTIONS
-b (background) The program forks itself into the background, detaches from its controlling tty, closes the file descriptors associated with the tty, and changes its current directory to the root directory. -B (local address) This option specifies which local address to binded to when making a connection. -c (crlf) Linefeed characters (LF) are converted to a Carriage Return Linefeed sequence (CRLF) when written to the socket. CRLF sequences read from the socket are converted to a single LF. -f (fork) When a server connection has been accepted, a separate process is forked to handle the connection in background. -l (loop) (only valid with -s) After a connection has been closed, another connection is accepted. -p (program) The specified command is executed for each connection. Its standard input, standard output, and standard error channels are con- nected to the socket. Command can be any shell command since it is passed to /bin/sh. -q (quit) The connection is closed when an end-of-file condition occurs on standard input. -r (read only) No data is read from standard input and written to the socket. -s (server) A server socket is created. A hostname argument is not required of Internet sockets, only the port number but a pathname is required for UNIX domain sockets. -v (verbose) Messages about connections etc. are issued to stderr. -w (write only) No data is read from the socket and written to the standard output. -version Socket prints its version ID and terminates. This must be the first argument to have an effect. EXAMPLES
The command socket -v coma.cs.tu-berlin.de nntp connects to the nntp port (port 119) of coma.cs.tu-berlin.de (130.149.28.10). The command socket -sl 3425 creates a server socket on port 3425 on the local host and waits for a connection. After a connection has been closed, a new connection is accepted. The command socket -wslqvp "echo Socket! " 1938 creates a server socket on port 1938 on the local host and waits for a connection. When a connection is accepted, the string "Socket!" is written to the socket. No data is read from the socket and written to the finger program. The connection is closed when an end-of-file condition at the standard output of the program occurs. Then a new connection is accepted. DIAGNOSTICS
Lots of diagnostics for failed system calls. unknown host host host's address could not be resolved. Signal signal caught, exiting Socket exits on any signal other than SIGTSTP, SIGCONT, SIGCLD, SIGQUIT. A non-zero exit code is returned if socket terminates due to an error condition or a signal. SEE ALSO
ip(7), tcp(7), unix(7), accept(2), bind(2), listen(2), connect(2), socket(2), gethostbyname(3), getservbyname(3) BUGS
socket -p terminates due to a SIGPIPE signal when there is more data from the socket available than the executed program wants to read. Please report any other bugs to the author. VERSION
This manual page describes Socket-1.1. AUTHOR
Juergen Nickelsen <nickel@cs.tu-berlin.de> Aug 6, 1992 SOCKET(1)
All times are GMT -4. The time now is 11:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy