Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Implementation of Socket Programming and Client – Server model.

Exp.No:1A TCP Server/Client


Aim
To implement a server and client model in python using TCP sockets.
Algorithm
Server
1. Create a server socket
2. To call bind function using socket object and pass a parameter IP Address, Port
Number.
3. To call listen function using socket object.
4. Wait for client to be connected.
5. Read client's message and display it
6. Close all streams
7. Close the server and client socket
8. Stop
Client
1. Create a client socket.
2. To establish connection with the server using connect function and pass IP Address,
Port Number as Parameter.
3. Get a message from user and send it to server using send function.
4. Close all input/output streams
5. Close the client socket
6. Stop
Program

Server.py

import socket
T_PORT = 5000
TCP_IP = '127.0.0.1'
BUF_SIZE = 300
# create a socket object name 'k'
k = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
print("Socket is created")
k.bind((TCP_IP, T_PORT))
print ("socket binded to %s" %(T_PORT))
k.listen(1)
print ("socket is listening")
con, addr = k.accept()
print ('Connection Address is: ' , addr)
data = con.recv(BUF_SIZE).decode()
print ("Received data", data)
con.close()
Client.py

import socket

T_PORT = 5000

TCP_IP = '127.0.0.1'

BUF_SIZE = 1024

MSG = "Hello karl"

# create a socket object name 'k'

k = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

k.connect((TCP_IP, T_PORT))

k.send(MSG.encode())

k.close

output

Server Window

Socket is created

socket binded to 5000

socket is listening

Connection Address is: ('127.0.0.1', 53505)

Received data Hello karl

Process finished with exit code 0

You might also like