You are on page 1of 3

ASSIGNMENT–07

NAME-ROHIT KUMAR
REG.NO.- 728
ROLL NO.-CSE/21068
SUBJECT CODE- CSC611
EMAIL:- cse21068@iiitkalyani.ac.in

1. Write a TCP socket program (in C/C++/Java/Python) to implement a client-server


program using TCP sockets. The client will send a message to the server, and the server
will perform a cyclic redundancy check (CRC) on the message to detect errors. The
server will then send the result back to the client.Display appropriate messages to the
user indicating the status of the connection and the result of the CRC check.
Server_code:-
import socket

socket_family = socket.AF_INET
protocol = socket.SOCK_STREAM
IP_address = '127.0.0.1'
port_number = 12345

server_socket = socket.socket(socket_family, protocol)

# Function to perform CRC encoding


def crc_encode(data, key):
key_length = len(key)
data_length = len(data)
key = list(key)
data = list(data + '0' * (key_length - 1))

for i in range(data_length):
if data[i] == '1':
for j in range(key_length):
data[i + j] = str(int(data[i + j]) ^ int(key[j]))

return ''.join(data[-key_length + 1:])

try:
# Bind the IP address and Port number
server_socket.bind((IP_address, port_number))
# Listen and accept the client’s request for the connection
server_socket.listen(1)
print("Server is listening for incoming connections...")

# Establish the connection with the client


client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)

# Receive data and key (generator polynomial) from client


data, key = client_socket.recv(1024).decode().split()
print("Data:", data)
print("Key (Generator Polynomial):", key)

# Perform CRC encoding


encoded_data = data + crc_encode(data, key)
print("Encoded Data:", encoded_data)

# Send encoded data to client


client_socket.sendall(encoded_data.encode())

except Exception as e:
print("Error:", e)

finally:
server_socket.close()

Client_code:-
import socket
socket_family = socket.AF_INET
protocol = socket.SOCK_STREAM
IP_address = '127.0.0.1'
port_number = 12345

client_socket = socket.socket(socket_family, protocol)

# Function to perform CRC encoding


def crc_encode(data, key):
key_length = len(key)
data_length = len(data)
key = list(key)
data = list(data + '0' * (key_length - 1))

for i in range(data_length):
if data[i] == '1':
for j in range(key_length):
data[i + j] = str(int(data[i + j]) ^ int(key[j]))

return ''.join(data[-key_length + 1:])

try:
# Connect to the server
client_socket.connect((IP_address, port_number))
print("Connected to server...")

# Input data and key from the user


data = input("Enter data: ")
key = input("Enter key (Generator Polynomial): ")

# Send data and key to the server


client_socket.sendall((data + " " + key).encode())

# Receive encoded data from the server


encoded_data = client_socket.recv(1024).decode()
print("Received encoded data:", encoded_data)

# Perform CRC decoding


rem = crc_encode(encoded_data, key)
key_length = len(key)
print("Received encoded data and performed divison modulo with remainder:"
, rem)

if rem == '0' * (key_length - 1):


print("No errors detected!")
else:
print("error")

except Exception as e:
print("Error:", e)

finally:
client_socket.close()

Output:-

You might also like