The Socket and Its Methods
The Socket and Its Methods
The different terms related to socket used in network programming are as follows −
Domain
Domain is the family of protocols that is used as the transport mechanism. These
values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
Type
Protocol
This may be used to identify a variant of a protocol within a domain and type. Its
default value is 0. This is usually left out.
Hostname
Port
Each server listens for clients calling on one or more ports. A port may be a Fixnum
port number, a string containing a port number, or the name of a service.
Python’s Socket Module for Socket Programming
To implement socket programming in python, we need to use the Socket module.
Following is a simple syntax to create a Socket −
import socket
s = socket.socket (socket_family, socket_type, protocol = 0)
Here, we need to import the socket library and then make a simple socket. Following
are the different parameters used while making socket −
Socket Methods
In this section, we will learn about the different socket methods. The three different
set of socket methods are described below −
In the client-server architecture, there is one centralized server that provides service
and many clients receive service from that centralized server. The clients also do the
request to server. A few important server socket methods in this architecture are as
follows −
The client in the client-server architecture requests the server and receives services
from the server. For this, there is only one method dedicated for clients −
Other than client and server socket methods, there are some general socket
methods, which are very useful in socket programming. The general socket methods
are as follows −
Server-side program
In this server side socket program, we will use the socket.bind() method which
binds it to a specific IP address and port so that it can listen to incoming requests on
that IP and port. Later, we use the socket.listen() method which puts the server
into the listen mode. The number, say 4, as the argument of the socket.listen()
method means that 4 connections are kept waiting if the server is busy and if a 5th
socket tries to connect then the connection is refused. We will send a message to the
client by using the socket.send() method. Towards the end, we use the
socket.accept() and socket.close() method for initiating and closing the
connection respectively. Following is a server side program −
import socket
def Main():
host = socket.gethostname()
port = 12345
serversocket = socket.socket()
serversocket.bind((host,port))
serversocket.listen(1)
print('socket is listening')
while True:
conn,addr = serversocket.accept()
print("Got connection from %s" % str(addr))
msg = 'Connecting Established'+ "\r\n"
conn.send(msg.encode('ascii'))
conn.close()
if __name__ == '__main__':
Main()
Client-side program
In the client-side socket program, we need to make a socket object. Then we will
connect to the port on which our server is running — 12345 in our example. After
that we will establish a connection by using the socket.connect() method. Then by
using the socket.recv() method, the client will receive the message from server. At
last, the socket.close() method will close the client.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 12345
s.connect((host, port))
msg = s.recv(1024)
s.close()
print (msg.decode('ascii'))
Now, after running the server-side program we will get the following output on
terminal −
socket is listening
Got connection from ('192.168.43.75', 49904)
And after running the client-side program, we will get the following output on other
terminal −
Connection Established
There are two blocks namely try and except which can be used to handle network
socket exceptions. Following is a Python script for handling exception −
import socket
host = "192.168.43.75"
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.bind((host,port))
s.settimeout(3)
data, addr = s.recvfrom(1024)
print ("recevied from ",addr)
print ("obtained ", data)
s.close()
except socket.timeout :
print ("No connection between client and server")
s.close()
Output
In the above script, first we made a socket object. This was followed by providing
the host IP address and port number on which our server is running — 12345 in our
example. Later, the try block is used and inside it by using the socket.bind()
method, we will try to bind the IP address and port. We are using
socket.settimeout() method for setting the wait time for client, in our example we
are setting 3 seconds. The except block is used which will print a message if the
connection will not be established between server and client.