Server Client TCP and UDP socket - sharmasadhna/mylearnings GitHub Wiki

https://www.bogotobogo.com/cplusplus/sockets_server_client.php

TCP (stream) vs UDP (data gram) major differences: Both protocols used in Transport layer of OSI

TCP

  1. APPLICATION: web server, mail server and their clients

UDP

  1. connectionless (i.e. a client & server connection need not be maintained)?
  2. unreliable-> packets might arrive not in order, can be dropped.
  3. speed is more since no acknowledgement of every message is needed
  4. limited message size
  5. APPLICATIONS: tftp, audio video streaming, VOIP : all theses applications use another protocol on top of UDP to achieve more reliability

7 steps in server socket: All calls return -1 on error

  1. create socket ==socket() --> return fd
  2. bind == //connect to port
  3. listen ==
  4. accept === new client fd;
  5. send/recv or read/write
  6. shutdown
  7. close

5 steps in client socket:

  1. create socket ==socket() --> return fd
  2. connect ===
  3. send/recv or read/write
  4. shutdown
  5. close

** socket is a way to communicate with other process using usual file descriptors,

  • TCP: LOCAL (UNIX) Sockets and network Sockets: If n/w sockets on loopback address performance is same as local sockets, since local interface is optimized and no real n/w connection is happening.

TCP SERVER:

  1. int socket(int domain, int type, int protocol) --> creates a file desc, and
  • if Local socket: file created under /tmp or /usr/tmp
  • if n/w socket port number is the file desc. port number 0-1023 are reserved by other protocols, and it can be no between 0-65535
  1. bind() --> name the socket
  2. listen() --> server socket, listens for incoming clients, if more than 1 client put them in a queue
  3. accept() --> blocking call (if not specifically marked as unblocking), wait for connect() from client and creates a new socket, dedicated for this client. This allows server to communicate with "n" no. of clients simultaneously.
  4. send & recv data --> simplest is read(), write(). recv are blocking, send is not blocking a. 1st recv() (from client) after this server is blocked untill recieved mesg from client b. after recved something (then process )and then send()

---2/3/4 are not needed for UDP---

TCP CLIENT:

  1. socket () --> create socket
  2. connect() --> connect to IP of server, blocks untill send/write from client to server or bind() --> in case of UDP
  3. send & recv data --> simplest is read(), write(). recv are blocking, send is not blocking b. 1st send() to server, then a. block on recv() until received something from server

---2. connect() is not needed for UDP, but bind() is needed. also sendTo and recvFrom are instead of send(write)/recv(read)---


  1. int socket(int domain, int type, int protocol) : a. domain = for internet socket: IPV4/IPv4 AF_INET/AFINET6 b. type= TCP/UDP = SOCK_STREAM/SOCK_DGRAm c. protocol = 0 = use default protocol

  2. int bind(int fd, struct sockaddr *local_addr, socklen_t addr_length) a. bind the socket to the current IP address on port b. INADDR_ANY

  3. int listen(int fd, int backlog_queue_size) a. backlog_queue_size : maximum number of queued connections requests

  4. int accept(int fd, struct sockaddr *remote_host, socklen_t addr_length) a. *remote_host --> address from remote host is written here b. addr_length --> actual address length from remote

  5. int connect(int fd, struct sockaddr *remote_host, socklen_t addr_length) a. This is a blocking call.

** The port number and IP address used in the AF_INET socket address structure are expected to follow the network byte ordering (big-endian). This is the opposite of x86's little-endian byte ordering, so these values must be converted.