TCP - Connection

Map Of Internet 1973

About

This article is about a connection within the TCP protocol.

A TCP server may serve several clients concurrently with the same local socket address (ie local IP address and port number) by creating a child process for each client for each connection.

A TCP connection is the name for the full request and response process in which:

  • a client connects to the server,
  • the server generates a response,
  • and the server closes the connection.

Id

Every connection is uniquely identified by an identifier called a socket.

Connection Reset

The Connection reset error occurs when the server isn’t sending back any data.

Browser

Browsers may deal with closed connections by retrying, because the problem might be temporary.

Standard Stream

After having created a connection, an application sends a standard input (on a io level) to the host via the connection, and receive a standard output.

For instance, lets request the home page of this website via the below HTTP get request

GET / HTTP/1.1
Host: gerardnico.com

we can send it as standard input with echo to a connection created with netcat

For Https, you need to use another utility such as openssl because netcast does not support it.

echo -e "GET / HTTP/1.1\nHost: gerardnico.com\n" | nc gerardnico.com 80

and you should get a redirection response telling you that you need to go to https://gerardnico.com/

HTTP/1.1 301 Moved Permanently
Date: Mon, 13 Apr 2020 12:26:26 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Mon, 13 Apr 2020 13:26:26 GMT
Location: https://gerardnico.com/
Server: cloudflare
CF-RAY: 58352ceb1ba9bf78-AMS

Process

TCP connections process (how a TCP socket gets started)

Duration: anywhere from 30-100ms to establish a connection and if you had SSL, there is more round trip.

Properties

State

During the lifetime of a TCP connection the local end-point undergoes a series of state changes:

Management

List

Powershell

Get-NetTCPConnection
# All listening port
Get-NetTCPConnection -State Listen
# One port
Get-NetTCPConnection -State Listen –LocalPort 1313

Netstat

On Unix-like and Microsoft Windows based operating systems the netstat command line tool may be used to list all currently established connection (ie sockets).

  • Command line - Example: Count all tcp connection for a the web server nginx
netstat \
  -all \
  --programs \
  -n \ # only numeric value (no host name, no port name)
  --tcp \ # only tcp 
  |  { IFS= read a; echo $a; grep nginx; } # capture and print the first line
Active Internet connections (servers and established)
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      15549/nginx: worker
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15549/nginx: worker
tcp        0      0 164.132.99.202:443      162.158.183.132:28246   ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      143.176.206.82:7414     ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      108.162.246.202:22880   ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      143.176.206.82:7410     ESTABLISHED 15550/nginx: worker
tcp        0      0 127.0.0.1:56812         127.0.0.1:19999         ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      162.158.183.172:54598   ESTABLISHED 15550/nginx: worker
tcp        0      0 127.0.0.1:57484         127.0.0.1:19999         ESTABLISHED 15549/nginx: worker
tcp        0      0 127.0.0.1:57482         127.0.0.1:19999         ESTABLISHED 15549/nginx: worker
tcp        0      0 127.0.0.1:80            127.0.0.1:44084         ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      173.245.54.88:41618     ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      143.176.206.82:6910     ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      172.69.63.26:25284      ESTABLISHED 15549/nginx: worker
tcp        0      0 164.132.99.202:443      143.176.206.82:7377     ESTABLISHED 15550/nginx: worker
tcp6       0      0 :::443                  :::*                    LISTEN      15549/nginx: worker

  • UI

Netstat Tcp Listening Socket

Tcpview

Network - TCPview

Process Explorer

Windows Process Explorer

Tcp Connection State Process Explorer

Get file

See Network File

Create

  • On a utility level, the utility netcat create a TCP connection.
  • On a library level, check the network package of your language.

Count

Count all TCP connection with netstat

Example for the nginx programs

netstat \
  -all \
  --programs \
  --tcp \ # only tcp 
  | grep nginx
  | wc

Test

You can test if a connection is possible with the netcat utility and it's z options that report connection status only.

  • In case of a connection
nc -z localhost 80 && echo tcp connection is possible
  • In case of no possible connection
nc -z localhost 80 || echo no tcp connection possible

Documentation / Reference





Discover More
HTTP Connection management

An HTTP fetch creates a network connection. This article gives you an overview and direct your to the reference documentation for more information
Map Of Internet 1973
Network - Connection

session in network are known as connection. A socket is the known network connection implementation. When referring to a network connection, most of the times, a text will refer to a TCP connection...
Map Of Internet 1973
Network - Open Port

How to know from a localhost which port are open on a remote host (ie that a firewall is not used or that the process is up and listen) When the scanner sends a SYN packet to open a TCP connection...
Map Of Internet 1973
Network - Server

A network server is a service that listens on a socket for inbound connections. This server receives packets and returns packets A Web/HTTP Server listens for TCP connection on the HTTP/HTTPS port...
Map Of Internet 1973
Network - TCP (Transmission Control Protocol)

The Transmission Control Protocol (TCP) is a protocol of the Internet protocol suite (TCP/IP). It originated in the initial network implementation in which it complemented the Internet Protocol (IP)....
Tls Version Firefox
Network - TLS

TLS is a cryptographic protocol and the successor to SSL, and is described in the 2246RFC 2246 standard. The protocol name was changed from SSL to TLS when it became a standard. TLS is a protocol that...
Map Of Internet 1973
Network - User Datagram (UDP)

UDP is a User Datagram Protocol It establishes connections that are: low-latency data loss-tolerant It emphasizes reduced latency over reliability. For a reliable data stream, see TCP. UDP is...
Map Of Internet 1973
Network - netcat (nc, ncat)

netcat is a net client/server command line tool for TCP or UDP protocol. It can: reads and writes data across network connections acts as a client but also as a server You can see it as the equivalent...
Map Of Internet 1973
Network File

Network file: Internet socket, NFS file or UNIX domain socket with lsof: to list only network files with TCP state LISTEN, use: to list network files with all UDP states except Idle,...
Idea Firewall Public Allow
PHP - (Debug|Debugger) with Xdebug in Docker

How to debug a PHP application running in a docker container. This articles is split in two: a part that you need to perform only ones a debug part that you must perform each time you want to debug...



Share this page:
Follow us:
Task Runner