Skip to content
Search

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

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

  1. History of TCP/IP
  2. The ISO OSI reference model
  3. TCP/IP addressing scheme
  4. Client / Server model
  5. Building a chat program (Server) Step 1
  6. Building a chat program (Server) Step 2
  7. 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

Set the #SO_REUSEADDR option.

Socket options

SO_LINGER

The following is the data structure used for the SO_LINGER option.

struct linger
{
 int l_onoff;
 int l_linger;
}
  • 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 > 0 and l_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 > 0 and l_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

See also

Favorite site

Library

Guide

Topic

References


  1. TIME_WAIT_-_The_Missing_Papers.pdf ↩

  2. Sunyzero.tistory.com_-_Remove_TCP_TIME_WAIT.pdf ↩

  3. Wiki.kldp.org-socket_programming.zip ↩

  4. Joinc_-BSD_Socket_Programming-_beginning.pdf ↩

  5. Depiness_-_socket_codeing_warning.pdf ↩

  6. Tech.kakao.com_-_closewait-timewait.pdf ↩

  7. Things_to_consider_when_using_UDP_-Meetup-_TOAST_Cloud.pdf ↩

  8. Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_1-Meetup-_TOAST_Cloud.pdf ↩

  9. Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_2-Meetup-_TOAST_Cloud.pdf ↩

  10. Kernel_Parameter_Decisions_for_TCP_Network_Performance_on_Linux_Servers_-Part_3-Meetup-_TOAST_Cloud.pdf ↩

  11. Onlamp.com_-_tcp_tuning.pdf ↩

author Zer0 sitemap