Session on Sockets
By
Dr. Rajiv R Bhandari
IP Addresses versus Port Numbers
PORT ranges by IANA (Internet Assigned Number
Authority)
What is Socket Programming?
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 the other socket reaches
out to the other to form a connection. The
server forms the listener socket while the
client reaches out to the server.
Methods associated with socket
Socket Creation:
int sockfd = socket(domain, type, protocol)
Domain:
AF_INET uses the TCP/IP protocol.
AF_UNIX creates filesystem objects and it only works
between processes on the same host.
AF_UNIX is much faster than AF_INET.
Methods associated with socket
Socket Creation:
int sockfd = socket(domain, type, protocol)
type: communication type
SOCK_STREAM: TCP(reliable, connection-oriented)
SOCK_DGRAM: UDP(unreliable, connectionless)
Protocol: Protocol value for Internet Protocol(IP),
which is 0. This is the same number that appears on the
protocol field in the IP header of a packet
Methods associated with socket
Bind
A server has a bind() method which binds it to a specific
IP and port so that it can listen to incoming requests on
that IP and port.
Syntax: [Link](('localhost', 8080))
Listen
A server has a listen() method which puts the server into
listen mode.
Syntax: [Link](5) #5 represents the number of unaccepted connections that the system will allow before refusing new
connections.
Methods associated with socket
Accepting Connections
accept(): This method is used by a server to accept a connection from a client. It returns a new socket
object representing the connection and the address of the client.
Syntax: conn, addr = [Link]()
Connecting
Connect():
This is used on the client side to connect to a server. You specify the server’s
IP address and port number.
Syntex: [Link](('localhost', 8080))
Methods associated with socket
Sending and Receiving
Datasend(): Sends data through the socket.
Example: [Link](b'Hello, World!’)
recv():
Receives data from the socket. You specify the buffer size.
Example: data = [Link](1024)
Methods associated with socket
Closing the Socket
close():
Closes the socket when communication is complete.
Socket Server Program
#server
import socket
while True:
# Create a socket object # Receive data from the client
data = client_socket.recv(1024).decode()
server_socket = [Link](socket.AF_INET, if not data:
socket.SOCK_STREAM) break # No data received, so the client has
# Bind the socket to a specific host and port disconnected
print(f"Received: {data}")
server_socket.bind(("[Link]", 12345))
# Listen for incoming connections # Send a response back to the client
response = input("Enter a response: ")
server_socket.listen(5) client_socket.send([Link]())
print("Server is listening on port 12345")
while True: # Close the client socket
# Accept a connection from a client client_socket.close()
client_socket, client_address = server_socket.accept()
print(f"Accepted connection from {client_address}")
Socket Client Program
import socket
# Create a socket object # Receive a response from the server
client_socket = [Link](socket.AF_INET,
socket.SOCK_STREAM) response = client_socket.recv(1024).decode()
print(f"Server Response: {response}")
# Connect to the server
client_socket.connect(("[Link]", 12345))
# Close the client socket
while True: client_socket.close()
# Send a message to the server
message = input("Enter a message (or 'q' to quit): ")
if message == 'q':
break
client_socket.send([Link]())
Running Steps
ubuntu@ubuntu:~$ python3 [Link]
Server is listening on port 12345
Accepted connection from ('[Link]', 33472)
Received: hii
Enter a response: hello
ubuntu@ubuntu:~$ python3 [Link]
Enter a message (or 'q' to quit): hii
Server Response: hello
Enter a message (or 'q' to quit): q
ubuntu@ubuntu:~$