Port hopping on IO::SOCKET::INET

Joe Platano

i am try to get a better understanding on Perl and was stumble about sockets. i am try to understand a "simple" example i found on xmodulo. I was expecting that like in the example the port will be 7777. But it seems like the port used for this socket communication is somewhere above 35000 and on every call of the client script the port is incremented by +1. Why ist the port different to the 7777 and why it is incrementing on every call?

the server example looks like this:

use IO::Socket::INET;     
# auto-flush on socket
$| = 1;     
# creating a listening socket
my $socket = new IO::Socket::INET (
    LocalHost => '0.0.0.0',
    LocalPort => '7777',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";

while(1)
{
    # waiting for a new client connection
    my $client_socket = $socket->accept();

    # get information about a newly connected client
    my $client_address = $client_socket->peerhost();
    my $client_port = $client_socket->peerport();
    print "connection from $client_address:$client_port\n";

    # read up to 1024 characters from the connected client
    my $data = "";
    $client_socket->recv($data, 1024);
    print "received data: $data\n";

    # write response data to the connected client
    $data = "ok";
    $client_socket->send($data);

    # notify client that response has been sent
    shutdown($client_socket, 1);
}

$socket->close();

the client example is:

use IO::Socket::INET;     
# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => '192.168.1.10',
    PeerPort => '7777',
    Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);
print "sent data of length $size\n";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";

$socket->close();   
ikegami

A connection isn't defined by

  • local address
  • peer address
  • port

It's defined by

  • local address
  • local port
  • peer address
  • peer port

For example,

>netstat /a

Active Connections

  Proto  Local Address          Foreign Address        State
  ...
  TCP    10.0.0.2:34208         stackoverflow:http     ESTABLISHED
  TCP    10.0.0.2:34212         stackoverflow:http     ESTABLISHED
  TCP    10.0.0.2:34213         stackoverflow:http     ESTABLISHED
  TCP    10.0.0.2:34224         stackoverflow:http     ESTABLISHED
  TCP    10.0.0.2:34226         stackoverflow:http     ESTABLISHED
  TCP    10.0.0.2:34227         stackoverflow:http     ESTABLISHED
  ...

You didn't specify a local port for the client, so the system picked an available one. That's the right thing to do. There's no reason to limit the client to one port. It can even cause problems.

For example, let's say your web browser tried to bind its sockets to port 80 (the port on which web servers listen). Your web browser would only be able to have one request pending at a time. That would be bad. You want to be able to create multiple connections to the same service. This allows you to request two different images at the same time, this allows you to load two pages in two different tabs at the same time, and so on. In the example above, my machine had six connections to stackoverflow.com's port 80, but that wouldn't have been possible if the web browser has bound the socket to port 80.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Wide character in IO::Socket::INET send

How to deal with exceptions in IO::Socket::INET?

perl6 io::socket::inet how to set timeout for socket?

SOCKET.io not listening on a port

Proper use of IO::Socket::INET for TCP client in perl

Socket.io port configuration with Heroku

perl6 IO::Socket::INET Could not receive data from socket: Connection reset by peer

Perl IO::Socket::INET6::sockaddr_in6 redefined error

perl6 Changes in IO::Socket::INET from last year and broken promises

How to list Socket.io rooms by names or with certain port

Using node js library Socket io without port

NodeJs - express and socket.io same port integration

Sharing same port for REST endpoints and netty socket.io?

How to start node express, binaryserver and socket.io on same port?

Vue - How to use a different socket.io port in tests?

Run socket.io 2.0 and Express 4.0+ on the same port

Go & Socket.io HTTP + WSS on one port with CORS?

Socket.io client ignoring port when namespace used [Bug?]

socket.io try to listen to default apache port

Node.js - Using Socket.io and Express on the Same Port

express app and socket.io chat in different port?

Express server and socket.io not working in same port

How can I connect same port on Expressjs and Socket.io

node.js+socket.io port not opening with ssl

socket.io port for every chat room javascript

Can my ExpressJS website and socket.io port use the same port?

RAW socket and NF_INET_POST_ROUTING

How to connect INET socket to PTY device in Python

Failed to connect to port 8080 error when using socket.io and node.js on openshift even when port 8000 specified