Network socket
lastmod 2024-10-07created 2017-01-1320 edits
A network socket is an endpoint of inter-process communication across a computer network. Since most communication between computers today is based on the Internet Protocol, most network sockets are Internet sockets. Programs for network communication create sockets and exchange data with each other through them. Sockets are specified in RFC 147.
An Internet socket consists of the following elements.
- Internet protocol (TCP, UDP, raw IP)
- Local IP address
- Local port
- Remote IP address
- Remote port
Internet sockets can be broadly classified into two types.
- Those using the UDP protocol.
- Those using the TCP protocol.
Socket programming (network programming) generally refers to this.
Category
- Berkeley sockets
- Winsock (Windows sockets)
- C:sockaddr_in (same as sockaddr_in)
- C:socket
- C:bind
- C:listen
- C:setsockopt
- C:send
- C:recv
- C:shutdown
- Maximum transmission unit (MTU)
- Maximum segment size (MSS)
- Nagle
- Python:socket (Python)
- C standard library - Includes a list of socket-related functions.
Documentation
- RFC 147
Connection and data communication flow between server and client
Socket_-_server_and_client_flow.png
Win32 socket programming tutorial
Download: Tipssoft_com-socket_programming.zip
- History of TCP/IP
- The ISO OSI reference model
- TCP/IP addressing scheme
- Client / Server model
- Building a chat program (Server) Step 1
- Building a chat program (Server) Step 2
- Building a chat program (Server)
send & recv byte size
The size of data sent with send and the size of data received with recv differ. See below for details.
Streamed sockets:
| The sockets are generally used in a streamed way: you'll receive all the data sent, but not necessarily all at once. You may as well receive pieces of data. Your approach of sending the length is hence valid: once you've received the length, you cann then load a buffer, if needed accross successive reads, until you got everything that you expected. So you have to loop on receives, and define a strategy on how to ahandle extra bytes received. |
Datagramme (packet oriented) sockets:
| If your application is really packet oriented, you may consider to create a datagramme socket, by requesting linux or windows socket(), the SOCK_DGRAM, or better SOCK_SEQPACKET socket type. |
Risk with your binary size data:
| Be aware that the way you send and receive your size data appers to be assymetric. You have hence a major risk if the sending and receiving between machine with CPU/architectures that do not use the same endian-ness. You can find here some hints on how to ame your code platform/endian-independent. |
TCP vs UDP
TCP:
- Connection-based
- Guarantees reliability and ordering
- Automatically splits data into packets
- Sends at an appropriate rate (as much as the line can handle)
- Easy to use, like writing to a file
UDP:
- Not connection-based. You have to implement it yourself
- Not reliable, and ordering is not guaranteed. Data may be lost or duplicated.
- If the data is large, you have to split it into packets yourself when sending.
- You have to split it up and send only as much as the line can handle.
- If packets are lost, you have to detect this in various ways and resend them if needed.
- Data may or may not be delivered, but what UDP guarantees is the 'amount' of data. If one side sends 256 bytes, the receiving side always receives 256 bytes. It cannot receive just the first 100 bytes.
TIME_WAIT
- [Recommended] What is the TIME_WAIT state? 1
- [Recommended] How to get rid of TCP TIME_WAIT 2
Set the #SO_REUSEADDR option.
Socket options
SO_LINGER
The following is the data structure used for the SO_LINGER option.
-
l_onoff: Flag for whether to enable or disable the linger option -
l_linger: Time to wait when the linger option is enabled
The values of these two member variables determine one of the following three close behaviors.
-
l_onoff == 0: This disables linger and is the socket's default. A normal graceful shutdown takes place, sending all data remaining in the socket buffer. -
l_onoff > 0andl_linger == 0: close returns immediately, the state is terminated, and an abortive shutdown takes place that discards the data remaining in the socket buffer. If a TCP connection is established, an RST packet is sent to the peer host to reset it. -
l_onoff > 0andl_linger > 0: Waits for the specified time and sends all data remaining in the buffer. If all data is sent within the specified time, it returns normally; if the time runs out, it returns with an error. Depending on the conditions, either an abortive or a graceful shutdown takes place.
SO_REUSEADDR
Python example:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# ...
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Transferring socket ownership
- Stackoverflow - Sending file descriptor by Linux socket
- Stackoverflow - Transfer ownership of unix domain sockets from one process to other
- Stackoverflow - Passing socket descriptor between two processes using shared memory
See also
Favorite site
- Wikipedia (en) Network socket
- libsocket 0.8.0: Links
- KLDP Socket programming with GCC on Linux 3
- Joinc: Introduction to BSD socket programming 4
Library
Guide
- Lifting the TCP connection limit (lifting the port number limit)
- Lifting the Linux socket connection limit
- Increasing the number of sockets by lifting the socket connection limit on Ubuntu (How to increase maximum socket connection in Ubuntu?)
- Practical things to watch out for when coding sockets 5
- [Recommended] CLOSE_WAIT & TIME_WAIT final analysis 6
Topic
- Stackoverflow: IPC performance: Named Pipe vs Socket
- Pipe vs Unix Socket Performance (Pipe is faster than Socker)
References
-
TIME_WAIT_-_The_Missing_Papers.pdf ↩
-
Sunyzero.tistory.com_-_Remove_TCP_TIME_WAIT.pdf ↩
-
Wiki.kldp.org-socket_programming.zip ↩
-
Joinc_-BSD_Socket_Programming-_beginning.pdf ↩
-
Depiness_-_socket_codeing_warning.pdf ↩
-
Tech.kakao.com_-_closewait-timewait.pdf ↩
-
Things_to_consider_when_using_UDP_-Meetup-_TOAST_Cloud.pdf ↩
-
Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_1-Meetup-_TOAST_Cloud.pdf ↩
-
Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_2-Meetup-_TOAST_Cloud.pdf ↩
-
Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_3-Meetup-_TOAST_Cloud.pdf ↩
-
Onlamp.com_-_tcp_tuning.pdf ↩