[go: up one dir, main page]

0% found this document useful (0 votes)
4 views4 pages

CRC

The document contains Java code for a CRC (Cyclic Redundancy Check) implementation that calculates a codeword from a given dataword and divisor using binary division. Additionally, it includes Python code for a TCP/IP server and client that handle basic communication, including sending a name and registration number, and responding to client messages. The server listens for connections, processes incoming data, and sends appropriate responses back to the client.

Uploaded by

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

CRC

The document contains Java code for a CRC (Cyclic Redundancy Check) implementation that calculates a codeword from a given dataword and divisor using binary division. Additionally, it includes Python code for a TCP/IP server and client that handle basic communication, including sending a name and registration number, and responding to client messages. The server listens for connections, processes incoming data, and sends appropriate responses back to the client.

Uploaded by

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

CRC

import java.util.Scanner;

public class CRC {


// Function to perform XOR operation
static String xor(String a, String b) {
StringBuilder result = new StringBuilder();
for (int i = 1; i < b.length(); i++) {
result.append(a.charAt(i) == b.charAt(i) ? '0' : '1');
}
return result.toString();
}

// Function to perform CRC calculation


static String calculateCRC(String input1, String input2) {
int l = input2.length();
// Step 1: Append (l-1) zeros to input1
String dividend = input1 + "0".repeat(l - 1);

String temp = dividend.substring(0, l);

// Step 2: Perform binary division using XOR


for (int i = l; i <= dividend.length(); i++) {
if (temp.charAt(0) == '1') {
temp = xor(input2, temp) + (i < dividend.length() ?
dividend.charAt(i) : "");
} else {
temp = xor("0".repeat(l), temp) + (i < dividend.length() ?
dividend.charAt(i) : "");
}
}

// Step 3: The final remainder is stored in temp


String remainder = temp;

// Step 4: Append remainder to original input1 to get the final


codeword
return input1 + remainder;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Take inputs
System.out.print("Enter the dataword (binary): ");
String input1 = sc.next();
System.out.print("Enter the divisor (binary): ");
String input2 = sc.next();
// Calculate CRC
String codeword = calculateCRC(input1, input2);

// Display results
System.out.println("Final Codeword: " + codeword);
}
}

SOCKET

Server.py

import socket

def start_server():
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the address and port


server_address = ('localhost', 12345)
print(f'Starting server on {server_address[0]}:{server_address[1]}')
server_socket.bind(server_address)

# Listen for incoming connections


server_socket.listen(1)

while True:
print('Waiting for a connection...')
client_socket, client_address = server_socket.accept()
try:
print(f'Connection from {client_address}')

while True:
# Receive data from client
data = client_socket.recv(1024).decode()
print(f'Received: {data}')

if data.startswith('My name is'):


# Extract name and send greeting
name = data.replace('My name is ', '')
response = f'Hello {name}'
client_socket.send(response.encode())

elif data.startswith('Registration'):
# Process registration number
reg_num = data.replace('Registration ', '')
response = f'Registration number {reg_num} is enrolled in
VIT'
client_socket.send(response.encode())

elif data == 'Bye':


# Send goodbye message and close connection
client_socket.send('Goodbye'.encode())
break

elif not data:


break

finally:
print('Closing connection')
client_socket.close()

if __name__ == '__main__':
try:
start_server()
except KeyboardInterrupt:
print("\nServer shutting down...")

Client.py

import socket
import sys

def start_client():
# Create a TCP/IP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server


server_address = ('localhost', 12345)
print(f'Connecting to {server_address[0]}:{server_address[1]}')
client_socket.connect(server_address)

try:
# Send name
name = input("Enter your message (format: My name is <your
name>): ")
client_socket.send(name.encode())

# Receive response
response = client_socket.recv(1024).decode()
print(f'Server response: {response}')
# Send registration number
reg_num = input("Enter your registration number: ")
message = f'Registration {reg_num}'
client_socket.send(message.encode())

# Receive response
response = client_socket.recv(1024).decode()
print(f'Server response: {response}')

# Send goodbye
bye_message = input("Enter 'Bye' to send to the server: ")
while bye_message.strip().lower() != 'bye':
bye_message = input("Please enter 'Bye': ")
client_socket.send(bye_message.encode())

# Receive final response


response = client_socket.recv(1024).decode()
print(f'Server response: {response}')

finally:
print('Closing socket')
client_socket.close()

if __name__ == '__main__':
try:
start_client()
except KeyboardInterrupt:
print("\nClient shutting down...")

You might also like