How do I open a network socket

[Main | Local]


Ports and Sockets

The basics..

In principle, ports and sockets are closely linked to one another - therefore also here together.


What is a port?

If two processes want to communicate with each other, regardless of whether via the network or within the system, the processes identify themselves to TCP / IP with one Port number. This is a 16-bit number, so there are 65535 ports for each transport protocol (UDP and TCP). The port number therefore indicates to which process a certain packet would like to be forwarded. The processes must have made it clear beforehand which ports they want to use. Sometimes this happens "on the fly", sometimes port numbers are fixed: "well known ports". These are always checked by the appropriate"well known services"is used, e.g. portmap service, DNS, DHCP etc.

How are ports assigned?

This is a little difficult: information is available on all ports at the IANA (Internet Assigned Numbers Authority, www.iana.org). However, only the ports between 0 and 1023 are correctly assigned or controlled - the "well known ports". On most systems, these ports can only be used by the root user (or services with these rights).
The range from 1024 - 49151 is released for registration - anyone can reserve a port for their application if they explain what they want to do and why (the IANA also signs non-disclosure agreements). However, these ports are not under the control of IANA - it's just a good idea to register a port for an application with a wide range of uses.
The other ports up to 65535 are dynamic or for private use. These ports are sometimes called "ephemeral"Denotes (volatile) ports (from AIX, for example). It used to be something like this in the RFC1700, Today there is a website: All ports known to IANA are currently listed here.

You can usually influence how the operating system allocates the ports. Under AIX you can see this with the command:
tcp_ephemeral_low = 32768
tcp_ephemeral_high = 65535
udp_ephemeral_low = 32768
udp_ephemeral_high = 65535
"Ephemeral" are the dynamic, volatile ports. For both TCP and UDP, you can specify the area that should be available for volatile protocols.

What is a socket?

In principle, sockets are a Application Programming Interface (API) for the communication protocols. It was introduced in 4.2BSD and is the de facto standard. There are two sockets "Familys": Internet and Unix-Domain. 4.3BSD then brought support for the Xerox Network System. 4.4BSD brought support for the ISO OSI protocols. AIX supports Internet (AF_INET) and Unix-Domain Sockets (AF_UNIX).

In practice, a socket is the end point of a connection, either over the network or internally on a system. The " to open"of a socket, the corresponding data structures are created and it is possible to specifically define the parameters that affect this connection Control blocks, the Protocol Control Block (PCB) and the control blocks for UDP, TCP and others (e.g. raw sockets).

Under Unix everything is a file, including a socket. A socket is therefore viewed as a file handle that is used by a process to request network services. The process simply writes the data to be sent as a stream to the socket, just as it would be written to a file. In the case of UDP, the process is responsible for ensuring that the Datagrams have the right size.

The socket is given by the following "Triple"addressed:
Protocol, local address, local process (port)
For a network socket (i.e. within the framework of TCP / IP) it would look like this:
tcp, 192.168.140.5, 1234
This is now one side of a connection, one "Association".
Now comes the other side:
Protocol, local address, local process (port), remote address, remote process (port)
So two socket addresses and a common protocol:
tcp, 192.168.140.5, 1234, 192.168.140.89, 23
So that would be a Telnet (port 23) from machine .89 to port 1234 on machine .5.
A socket is the end point of a connection over the network. The combination of IP address and port number makes it unique in the world. Two sockets give the whole connection.

On the basis of sockets and thus ports, the multiplex data transmission (all information on a common wire) is split up again into its individual connections: demultiplexing.

____________ ____________ ____________ | Process1 | | Process2 | | Process3 | ------------ ------------ ------------. Port x Port y Port z. A transport protocol - UDP or TCP. The Internet Protocol Each process is assigned a port. Packets passed on by the IP layer are first separated according to the transport protocol and then assigned to the individual ports.

System calls that have something to do with sockets

  • Initialize:
    int sockfd = socket (int family, int type, int protocol)
    family: the address family: AF_INET, AF_UNIX, AF_NS, AF_IUCV. Which method should the socket use?
    type: what kind of socket it should be: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET
    protocol: UDP, TCP, IP, ICMP
    sockfd: this is the integer number that is returned on a socket call. Like a file handle.

  • Bind a socket to a port address: int bind (int sockfd, struct sockaddr * localaddr, int addrlen)


[Main | Local]

[General | UNIX | AIX | TCP-IP | TCP | ROUTING | DNS | NTP | NFS | FreeBSD | Linux | RPi | SMTP | Tracing | GPS | LW]

Copyright 2001-2021 by Orm Hager - The GPL applies
Please send your feedback to: Orm Hager ([email protected])