Network Programming

 Sockets

https://notes.shichao.io/unp/ch3/ 

Slideshare PPT

Socket Programming - Youtube

Socket Address

socket is one endpoint of a two way communication link between two programs running on the network. The socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points between which the communication take place. 

sockets is created using ‘socket’ system call. The socket provides bidirectional FIFO Communication facility over the network. A socket connecting to the network is created at each end of the communication. Each socket has a specific address. This address is composed of an IP address and a port number.

The server creates a socket, attaches it to a network port addresses then waits for the client to contact it. The client creates a socket and then attempts to connect to the server socket. When the connection is established, transfer of data takes place.

Types of Sockets : There are two types of Sockets: the datagram socket and the stream socket.

  1. Datagram Socket : This is a type of network which has connection less point for sending and receiving packets. It is similar to mailbox. The letters (data) posted into the box are collected and delivered (transmitted) to a letterbox (receiving socket).

  1. Stream Socket In Computer operating system, a stream socket is type of interprocess communications socket or network socket which provides a connection-oriented, sequenced, and unique flow of data without record boundaries with well defined mechanisms for creating and destroying connections and for detecting errors.





IPv4 Socket Address

An IPv4 socket address structure, commonly called an "Internet socket address structure," is
named sockaddr_in and is defined by including the <netinet/in.h> header.

The four socket functions that pass a socket address structure from the process to the kernel:
  •  bind
  •  connect
  •  sendto
  •  sendmsg
The five socket functions that pass a socket address structure from the kernel to the process:
  •  accept
  •  recvfrom
  •  recvmsg
  •  getpeername
  •  getsockname

Sockets: A Comprehensive Overview

1. Socket Address Structures

  • Purpose: To hold the address of a socket, including IP address and port number.
  • Common Structures:
    • struct sockaddr_in: For IPv4 addresses.
    • struct sockaddr_in6: For IPv6 addresses.
    • struct sockaddr_un: For Unix domain sockets.
  • Key Fields:
    • sin_family: Address family (AF_INET, AF_INET6, AF_UNIX).
    • sin_port: Port number in network byte order.
    • sin_addr: IP address in network byte order.
  • Usage: These structures are used in various socket functions like bind, connect, and listen to specify the address of the socket.

2. Value-Result Arguments

  • Concept: A type of argument passed to a function where the function modifies the argument's value and returns it to the caller.
  • Socket Context: Socket address structures are often passed as value-result arguments. The function fills the structure with the appropriate information (e.g., remote address) and returns it to the caller.
  • Example: The accept function takes a sockaddr structure as an argument. After accepting a connection, the function fills the structure with the address of the client and returns it to the caller.

3. Byte Ordering and Manipulation Functions

  • Byte Order: Refers to the way bytes are arranged in memory.
    • Big-Endian: Most significant byte first.
    • Little-Endian: Least significant byte first.
  • Manipulation Functions:
    • htons: Converts a 16-bit host value to network byte order.
    • htonl: Converts a 32-bit host value to network byte order.
    • ntohs: Converts a 16-bit network value to host byte order.
    • ntohl: Converts a 32-bit network value to host byte order.
  • Purpose: These functions are essential for ensuring correct data transmission over the network, as different systems may have different byte orders.

4. Elementary TCP Sockets

  • Socket Function: Creates a socket and returns a socket descriptor.
    • socket(domain, type, protocol)
  • Connect Function: Connects a socket to a remote address.
    • connect(sockfd, addr, addrlen)
  • Bind Function: Assigns a local address to a socket.
    • bind(sockfd, addr, addrlen)
  • Listen Function: Puts a socket into listening mode.
    • listen(sockfd, backlog)
  • Accept Function: Accepts a connection request from a client.
    • accept(sockfd, addr, addrlen)
  • Fork Function: Creates a child process.
    • fork()
  • Exec Function: Replaces the current process image with a new process image.
    • execve(pathname, argv, envp)
  • Concurrent Servers: Servers that can handle multiple clients simultaneously, often achieved using the fork or select mechanisms.

5. Close Function and Related Functions

  • Close Function: Closes a socket descriptor.
    • close(sockfd)
  • Shutdown Function: Disables specific types of communication on a socket.
    • shutdown(sockfd, how)
  • Purpose: These functions are crucial for proper resource management and graceful termination of network connections.

Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)