The code examples and setup are provided at the links below
Raw socket code example with connect and bind API – TCP
Raw socket code example with connect and bind API – UDP
The TCP/UDP headers are added by the application. The output of the TCP and UDP code is provided below
TCP code output
UDP Code output
The port numbers are inconsequential for a raw socket and the port numbers are shown only to denote that the packet cam from the application that added the TCP/UDP headers.
The output for the “ss -w -a” shell command for both TCP and UDP are provided below
TCP SS output
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.1.14:tcp 192.168.1.11:ipproto-20675
ESTAB 0 0 192.168.1.11:tcp 192.168.1.14:ipproto-20931
UDP SS output
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 214272 0 192.168.1.14:udp 192.168.1.11:ipproto-20675
ESTAB 214272 0 192.168.1.11:udp 192.168.1.14:ipproto-20931
As can be seen from the “ss shell” command output, the port numbers are not shown. However the IP address links are shown and the connection is shown between client and server.
Not aware what the 20675 and 20931 numbers denote :). If anybody is aware – please share it in the comment box.
Raw Socket communication with Data link Layer