Description:
Client-server is a software architecture model consisting of two parts, client systems and server systems, both communicating over a computer network or on the same computer. A client-server application is a distributed system made up of both client and server software. Client server application provide a better way to share the workload.The client process always initiates a connection to the server, while the server process always waits for requests from any client.
For handling multiple clients , the concept of Threading is used here.
Explanation:
--socket(socket.AF_INET, socket.SOCK_STREAM):
AF_INET ia an address family that indicates the type of address that the socket can communicate with. AF_INET indicates ipv4 addresses. when a socket is creating it is necessary to specify the address family. linux kernel supports 29 other address families
SOCK_STREAM indicates that it is a TCP socket . for UDP we use SOCK_DGRAM
--setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1):
socket.SO_REUSEADDR, causes the port to be released immediately after the socket is closed so that it is re-usable.
SOL_SOCKET is the socket layer itself. It is used for options that are protocol independent
--socket.bind((hostname, port)):
Bind to a particular port in preparation to receive connections on this port. bind() takes one parameter, which is a two value tuple consisting of a hostname (string) and port number (integer).
--socket.accept():
Used for accepting a new connection. We can get the IP address and port number.
--socket.listen():
Tells the socket to listen for incoming connections
Implementation:
Server.py:
import socket
from threading import Thread
def client_thread(conn, ip, port):
client_status = True
while client_status:
data = conn.recv(1024)
message=data.decode()
print("message from "+ip+":"+port+">>",message)
if "exit" in message:
print("Connection " + ip + ":" + port + " closed")
conn.close()
client_status = False
else:
reply="hello , Message recieved: "+message
conn.sendall(reply.encode())
def main():
host = "127.0.0.1"
port = 8888
Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Server.bind((host, port))
Server.listen(3)
print("Server is ready for requests")
while True:
conn, address = Server.accept()
ip, port = str(address[0]), str(address[1])
print("Connected with client ")
print("ip: " + ip + " port:" + port)
try:
newclient = Thread(target=client_thread, args=(conn, ip, port))
newclient.start()
except:
print("Error executing thread.")
Server.close()
if __name__ == "__main__":
main()
Client.py:
import socket
import sys
def main():
Client1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8888
try:
Client1.connect((host, port))
except:
print("Connection error")
sys.exit()
print("Enter 'exit' to disconnect")
message = input(" >> ")
while message != 'exit':
Client1.send(message.encode())
reply = Client1.recv(1024).decode()
print("SERVER >",reply)
message = input(" >> ")
Client1.send(b'exit')
if __name__ == "__main__":
main()
Output:
Establishing connection with 2 clients:
Receiving client requests and responding back:
Comments