Network Programming


Contents:

  •           What is Client/Server Architecture?
  •           Sockets: Communication Endpoints
  •           Networking Programming in Python
  •           The SocketServer Module
  •           Twisted Framework
  •           Related Modules

Client Server Architecture:

       It is simple., the Server is a piece of hardware or software provides a "service" that is needed by one or more clients (user of the service). Its purpose is to wait for the client's request and respond to those clients and then wait for more requests.
       Clients on the other hand, contacts server for a particular request, send over any necessary data, and then wait foe the server ti reply, either completing the request or indicating cause of failure.

       The common notion of client/server architecture today is illustrated in the diagram below, which says clients retrieving information from the server across the Internet. Although this system is indeed example of client/server architecture, it isn't the only one. Furthermore, client/server architecture can be applied to hardware as well as software.

Related image
     

Sockets -> Communication Endpoints:

           A Communication endpoint is created which allows a server to listen for requests. Once a communication endpoint has been established, our listening server can now enter its infinite loop, waiting for clients to connect and responding to requests.
           
           Sockets are computer networking data structures that embody the concept of the "communication endpoint".Network must create sockets before any type of communication can commence.
         
           Unix sockets are the first family of sockets and have family name, AF_UNIX or address family:UNIX (a.k.a AF_LOCAL as referred in POSIX1.g standard). Python uses address families and the abbreviation AF
           AF_LOCAL is supposed to replace AF_UNIX; however for the backward compatibility,many systems use both and just make them aliases to the same constant. Python itself still uses AF_UNIX.

           The Second type of socket is networked-based and it has its own family name, AF_INET or address family:INTERNET . Another address family, AF_INET6, is used for internet protocol version 6 (IPv6) addressing.
              
           The AF_NETLINK family of (connection-less) sockets allow for IPC(inter-process communication) between user and kernel-level code using the standard BSD socket interface. 

           Another feature for Linux is support for the Transparent Inter-process Communication (TIPC) protocol. TIPC is used to allow clusters of computers to talk to each other without using IP-based addressing. Python supports TIPC comes in the form of the AF_TIPC family.

Overall, Python supports only
  • AF_UNIX
  • AF_NETLINK
  • AF_TIPC
  • AF_INET{,6} familes.  

Connection-Oriented Sockets

           This type of socket-connection must establish connection before the communication can occur. This type of communication is also referred to as virtual circuit or stream socket.

           The protocol that implements such connection is Transmission Control Protocol (acronym TCP). To create TCP sockets., one must use SOCK_STREAM as the socket type. Sock_Stream name for the TCP socket is based on one of its denotations as stream socket because the networked version of these sockets (AF_INET) use the Internet Protocol (IP) to find hosts in the network, the entire system generally goes by the combined names of both protocols (TCP and IP), or TCP/IP. 

tcpSocket = socket(AF_INET , SOCK_STREAM)


Connection-Less Sockets

           This type of socket-connection uses datagram type of socket, which is connectionless. This means no connection  is required before the communication can occur. There are no guarantees of sequencing, reliability, or non-duplication in the process of data delivery. 
           
           The protocol that implements such connection types is the User Datagram Protocol (acronym UDP). To create UDP sockets, we must use SOCK_DGRAM as the socket type. The SOCK_DGRAM name for a UDP socket, because ofcourse, it comes from the word 'datagram' and these sockets also use the Internet Protocol to find hosts in the network, the entire system generally goes by the combined names of both protocols (UDP and IP), or UDP/IP. 

udpSocket = socket(AF_INET , SOCK_DGRAM)



Sockets Object (Built-In) Methods


  • Common Socket Object Methods and Attributes.


Application Programming by WESLEY CHUN 

Creating a TCP Server

           Lets look a pseudo code to create a generic TCP Server:

                      s = socket()                                                       # create server socket
                      s.bind()                                                              # bind socket to address
                      s.listen()                                                            # listen for connections
                      loop:                                                                   # server infinite loop
                                 cs = s.accept()                                     # accept client connection
                                 inner_loop:                                           # communication loop
                                            cs.recv() / cs.send()              # dialog ( receive / send )
                                 cs.close()                                               # close client socket
                      s.close()                                                              # close server socket

Creating a TCP Client

           Lets look a pseudo code to create a generic TCP Server:

                      c = socket()                                               # create client socket
                      c.connect()                                               # attempt server connection
                    comm_loop:                                              # communication loop
                           c.recv() / c.send()                         # dialog ( receive / send )
                      c.close()                                                    # close client socket


Lets get our Hands on

Lets create a TCP server.,


Explanation:

  • We are importing ctime() from the time module and all the attributes from the socket module.
  • Note that HOST variable is blank, which is an indication to the bind() method that it can use any available address
  • We also choose a random port number which doesn't fall between the reserved by the system. Any random free port from 1024 to 65535 can be selected.

  • TCP server socket is allocated :-> s = socket(AF_INET, SOCK_STREAM)
  • bind() method call is to bind the socket to the server's address and to start the TCP listener.
  • The argument for the listen(() method is simply a maximum number of incoming connection requests to accept before connections are turned away or refused.

  • Once we are inside the server's infinite loop, We will be waiting for a connection.
  • When a connection comes in, we enter the dialog loop where we wait for the client to send its message. 
  • If the message is blank, that means that the client has quit so we will break from the inner loop and close the client connection, and then go back to wait for another client.
  • If the client send some message to the server., we are going to format it by adding current timestamp with the message.


Now Lets create a TCP client.,


Explanation:

  • We are importing all the attributes from the socket module.

  • Note that ADDR variable contains two arguments HOST,PORT_NUMBER. 
  • HOST contains 'localhost' since we are running the program in the same computer. The HOST,PORT_NUMBER refers to the server's hostname and port_number respectively.
  • Note that: The port number PORT should be the same as the what we set as the server's PORT number.

  • The Client also has the infinte loop but it wont run forever like the server's loop. The loop gets exit when the user enters no input or the server somehow quit and our call to recv() method fails.
  • Otherwise, if the user enters in some messages., it is sent to the server for processing.

Executing Our TCP and Client:

START THE SERVER FIRST.
  • Save the server program with  filename.py in a location.(in my case I've stored it in DESKTOP).
  • Open command prompt and locate to the place where you stored the file. (in my case I've stored it in DESKTOP).
  • Then type python filename.py
  • You will be getting an output. That is "Waiting for Connection.....".

NOW START THE CLIENT :
  • Save the client program with  filename.py in a location.(in my case I've stored it in ECLIPSE).
  • Open command prompt and locate to the place where you stored the file. (in my case I've stored it in ECLIPSE).
  • Then type python filename.py
  • You will be getting an output. That is ">".
  • Now if check the server cmd prompt, you'll see something like "...Connected from and an address".

  • Now Open Client cmd prompt and type some message and press enter.

  • You'll get something like " the current time and the message you've sent".
  • If you simply press enter without a message, then the client closes and the server waits for the next client.

Comments