0% found this document useful (0 votes)
20 views5 pages

705 Assignment 7

Uploaded by

khroudishnoor34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

705 Assignment 7

Uploaded by

khroudishnoor34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT 7

NAME: Kushagra Singh


REG NO: 705

TASK 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.

CODE:

--server.py--

import socket

def xor(a, b):

result = []

for i in range(1, len(b)):

if a[i] == b[i]:

result.append('0')

else:

result.append('1')

return ''.join(result)

def checkSum(dividend, divisor):

pick = len(divisor)

tmp = dividend[0: pick]


while pick < len(dividend):

if tmp[0] == '1':

tmp = xor(divisor, tmp) + dividend[pick]

else:

tmp = xor('0'*pick, tmp) + dividend[pick]

pick += 1

if tmp[0] == '1':

tmp = xor(divisor, tmp)

else:

tmp = xor('0'*pick, tmp)

checkword = tmp

return checkword

def server_program():

host = socket.gethostname()

port = 12345

server_socket = socket.socket()

server_socket.bind((host, port))

server_socket.listen(2)

conn, address = server_socket.accept()

print("Connection from: " + str(address))

while True:
data = conn.recv(1024).decode()

if not data:

break

print("from connected user: " + str(data))

data = 'Received "' + data + '" with CRC: ' +


checkSum(data,'1101')

conn.send(data.encode())

conn.close()

if __name__ == '__main__':

server_program()

--client.py--

import socket

def xor(a, b):

result = []

for i in range(1, len(b)):

if a[i] == b[i]:

result.append('0')

else:

result.append('1')
return ''.join(result)

def checkSum(dividend, divisor):

pick = len(divisor)

tmp = dividend[0: pick]

while pick < len(dividend):

if tmp[0] == '1':

tmp = xor(divisor, tmp) + dividend[pick]

else:

tmp = xor('0'*pick, tmp) + dividend[pick]

pick += 1

if tmp[0] == '1':

tmp = xor(divisor, tmp)

else:

tmp = xor('0'*pick, tmp)

checkword = tmp

return checkword

def client_program():

host = socket.gethostname()

port = 12345

client_socket = socket.socket()

client_socket.connect((host, port))
message = input(" -> ")

key = '1101'

l_key = len(key)

appended_data = message + '0'*(l_key-1)

encoded_data = checkSum(appended_data,key)

print("Remainder = ",encoded_data)

sent_data = message + encoded_data

error_data = '100100110'

print(sent_data)

while message.lower().strip() != 'exit':

client_socket.send(sent_data.encode())

data = client_socket.recv(1024).decode()

print('Received from server: ' + data)

message = input(" -> ")

client_socket.close()

if __name__ == '__main__':

client_program()

You might also like