TCP/IP Socket programming in Python:
Socket programming is a way of connecting two nodes on a network to
communicate with each other. One socket(node) listens on a particular port at
an IP, while other socket reaches out to the other to form a connection. Server
forms the listener socket while client reaches out to the server.
1. Socket programming is started by importing the socket library and making a simple socket.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here we made a socket instance and passed it two parameters. The first
parameter is AF_INET and the second one is SOCK_STREAM.
AF_INET refers to the address family ipv4.
The SOCK_STREAM
means connection
oriented TCP protocol.
2. A listening socket does just what it sounds like. It listens for
connections from clients. When a client connects, the server calls accept() to
accept, or complete, the connection.
3. The client calls connect() to establish a connection to the server and
initiate the three-way handshake.
4. The handshake step is important since it
ensures that each side of the connection is reachable in the network, in other
words that the client can reach the server and vice-versa. It may be that only
one host, client or server, can reach the other.
5. In the middle is the round-trip section, where data is exchanged
between the client and server using calls to send() and recv().
6. At the bottom, the client and server close() their respective sockets.
No comments:
Post a Comment