TCP with Python


TCP with socket programming in python 


TCP (Transmission Control Protocol) 

  • Connection-oriented : Client and Server connection must be established before transferring data 
  • Three way handshake, re-transmission, error-detection increases the reliability but lengthens latency
  • TCP abstracts the application's communication from the underlying networking details 
  • Used by : WWW, email, FTP, secure shell, peer to peer file sharing, streaming media 
  • Optimized for reliability, and this may require long delivery time 

Functions provided by TCP 

    • Three way handshake 
    • Requesting re-transmission of lost data 
    • Rearrange out of order data
    • Helps minimize network congestion to reduce the occurrence of other problems

 TCP segment header 

https://en.wikipedia.org/wiki/Transmission_Control_Protocol

    • Source port : Identifies the sending port 
    • Destination port : Identifies the receiving port 
    • Sequence number 
      • SYN flag == 1 : this is the initial sequence number. The sequence number of the actual first data byte and the acknowledged number in the corresponding ACK are then this sequence number plus 1 
      • SYN flag == 0 : this is the accumulated sequence number of the first data byte of this segment 
    • Acknowledgment number : next sequence number that the sender of the ACK is expecting 
    • Data offset : pass 
    • Reserved : future use 
    • FLAGS : pass 
    • Window Size : pass 
    • Checksum : for error checking of the TCP header, payload and an IP pseudo header 

TCP message types 

    • SYN : Used to initiate and establish a connection. Also helps you to synchronize sequence numbers between devices 
    • ACK : Helps to confirm to the other side that it has received the SYN 
    • SYN-ACK : SYN message from local device and ACK of the earlier packet 
    • FIN : Used to terminate a connection  

TCP three way handshake - Establishing connection 

    1. Client establishes a connection with a server by sending  
    2. Server responds to the client with SYN-ACK
    3. Client acknowledges the response of the server 
    4. After 3 way handshake, full duplex communication is established 
https://www.guru99.com/tcp-3-way-handshake.html

 

 TCP four way handshake - Connection termination 

    1. HOST A -> HOST B : sends packet with FIN flag set 
    2. HOST B -> HOST A : sends packet with ACK flag set 
    3. HOST B -> HOST A : sends packet with FIN flag set 
    4. HOST A -> HOST B : sends packet with ACK flag set 
https://en.wikipedia.org/wiki/Transmission_Control_Protocol#/media/File:TCP_CLOSE.svg

TCP (socket) programming with python - Client 

This is the client side code that can connect to a server within a same network.
  • socket.gethostname() :  returns a string that contains the hostname of your machine
  • socket.gethostbyname() : translate hostname to IPv4 address format
  • socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    • AF_INET for IPv4
    • AF_INET6 for IPv6
    • SOCK_STREAM for TCP
    • SOCK_DGRAM for UDP
  • socket.connect(address) : connect to remote socket at address.
    • A pair (host, port) is used for the AF_INET(IPv4) address family
    • Host : string that is either a hostname in internet domain or IPv4 address
    • For IPv6, use (host, port, flowinfo, scope_id) for the address
  • socket.sendall(bytes)
    • Strings must be encoded before being sent across the network
    • There is also a send() method, but when we use send(), we have to check whether all the data has been sent successfully
    • Unlike the send() method, sendall() continues to send data until either all data has been sent or an error occurs.
    • None is returned on success
  • socket.recv(buffer_size)


TCP (socket) programming with python - Server 

This is the server side code that can connect to a server within a same network 

  • The major difference from the client side code is that 
    • bind the socket before use 
    • server socket can limit the number of connections with listen() method 
    • server socket will wait for incoming client connections using accept() method
  • socket.bind(address) 
    • Bind the socket to address. 
    • Socket must not be already be bound. 
  • socket.listen([backlog]) 
    • Enable a server to accept connections 
    • Backlog specifies the number of pending connections the queue will hold.
    • So if you call sock.listen(5) and 6 connections requests come in before you call accept, one of them is getting dropped. 
  • socket.accept()
    • Method to accept a connection 
    • Return value is a pair of (conn, address), where conn is a new socket object used to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection 

Can we connect to a host located at different network ? 

  • Yes we can but we will have to understand what public and private IP addresses are and what port forwarding is. 
  • When we were connecting devices in a same network, we were using the private IP address and a port number. When we want to connect to devices located at different network, we have to use the public IP address and a port number. In addition to that, we will have to set a table which maps the port and specified application. 
  • Check out this video and I think this is one of the best video that will help you understand port forwarding and how to set things work.  

REFERENCE 

Comments

Popular posts from this blog

Operating System - Learn Thread with C/C++, Python

UDP with Python