0% found this document useful (0 votes)
33 views16 pages

CN Program

The document contains multiple Java programs implementing client-server communication using TCP and UDP protocols, including examples for file transfer, ARP requests, and Hamming code error correction. Each program demonstrates socket creation, data transmission, and handling of client-server interactions. Additionally, there is a C program for generating and checking Hamming codes for error detection and correction.

Uploaded by

e22cs064
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)
33 views16 pages

CN Program

The document contains multiple Java programs implementing client-server communication using TCP and UDP protocols, including examples for file transfer, ARP requests, and Hamming code error correction. Each program demonstrates socket creation, data transmission, and handling of client-server interactions. Additionally, there is a C program for generating and checking Hamming codes for error detection and correction.

Uploaded by

e22cs064
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/ 16

Program:

EClient:
import java.io.*;
import java.net.*;
public class EClient {
public static void main(String[] args) throws IOException {
try {
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the IP address: ");
String ipAddress = userInput.readLine(); // Prompt the user to enter the IP address
Socket socket = new Socket(ipAddress, 5000);
System.out.println("CONNECTION ESTABLISHED");
System.out.print("Enter the data: ");
String data = userInput.readLine();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(data);
System.out.println("Client received: " + in.readLine());
socket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

EServer:
import java.io.*;
import java.net.*;
public class EServer {
public static void main(String[] args) throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server is waiting for connection...");
Socket socket = serverSocket.accept();
System.out.println("CONNECTION ACCEPTED");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String receivedData = in.readLine();
System.out.println("Server received: " + receivedData);
out.println(receivedData);
socket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Program:
TCPClient1:
import java.io.*;
import java.net.*;
public class TCPClient1 {
public static void main(String[] args) {
Socket c = null;
String line;
DataInputStream is, is1;
PrintStream os;
try {
c = new Socket("localhost", 9999);
} catch (IOException e) {
System.out.println(e);
}
try {
os = new PrintStream(c.getOutputStream());
is = new DataInputStream(System.in);
is1 = new DataInputStream(c.getInputStream());
do {
System.out.println("Client: ");
line = is.readLine();
os.println(line);
System.out.println("Server: " + is1.readLine());
} while (!line.equalsIgnoreCase("quit"));
is1.close();
os.close();
} catch (IOException e) {
System.out.println("Socket Closed! Message Passing is over");
}
}
}
TCPServer1:
import java.io.*;
import java.net.*;
public class TCPServer1 {
public static void main(String[] args) {
ServerSocket s = null;
String line;
DataInputStream is = null, is1 = null;
PrintStream os = null;
Socket c = null;
try {
s = new ServerSocket(9999);
} catch (IOException e) {
System.out.println(e);
}

try {
c = s.accept();
is = new DataInputStream(c.getInputStream());
is1 = new DataInputStream(System.in);
os = new PrintStream(c.getOutputStream());

do {
line = is.readLine();
System.out.println("Client: " + line);
System.out.println("Server: ");
line = is1.readLine();
os.println(line);
} while (!line.equalsIgnoreCase("quit"));

is.close();
os.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Program:
FClient:
import java.io.*;
import java.net.*;
public class FClient {
public static void main(String[] args) throws IOException {
try {
Socket socket = new Socket("127.0.0.1", 5000);
System.out.println("Connection request. Connected");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.print("Enter the filename: ");
String fileName = userInput.readLine();
out.println(fileName);
String serverResponse;
while ((serverResponse = in.readLine()) != null) {
System.out.println(serverResponse);
}
socket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

FServer:
import java.io.*;
import java.net.*;
public class FServer {
public static void main(String[] args) throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Waiting for clients...");
Socket socket = serverSocket.accept();
System.out.println("Connection Established");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String fileName = in.readLine();
System.out.println("Client wants file: " + fileName);
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = fileReader.readLine()) != null) {
out.println(line);
}
fileReader.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Program:
UDPClient:
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
System.out.print("Enter the hostname : ");
String hostname = inFromUser.readLine();
sendData = hostname.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData()).trim();
System.out.println("IP Address: " + modifiedSentence);
clientSocket.close();
}
}

UDPDNSServer:
import java.net.*;
import java.io.IOException;
public class UDPDNSServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
System.out.println("Press Ctrl + C to Quit");
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String hostname = new String(receivePacket.getData()).trim();
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
System.out.println("Request for host " + hostname);
String ipAddress;
try {
InetAddress host = InetAddress.getByName(hostname);
ipAddress = host.getHostAddress();
} catch (UnknownHostException e) {
ipAddress = "Host Not Found";
}
sendData = ipAddress.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
clientAddress, clientPort);
serverSocket.send(sendPacket);
}
}
}
Program:
ArpClient:
import java.io.*;
import java.net.*;
class ArpClient {
public static void main(String args[]) throws IOException {
try {
Socket ss = new Socket(InetAddress.getLocalHost(), 1100);
PrintStream ps = new PrintStream(ss.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String ip;
System.out.println("Enter the IPADDRESS:");
ip = br.readLine();
ps.println(ip);
String str;
BufferedReader br2 = new BufferedReader(new InputStreamReader(ss.getInputStream()));
System.out.println("ARP From Server::");
do {
str = br2.readLine();
System.out.println(str);
} while (!str.equalsIgnoreCase("end"));
} catch (IOException e) {
System.out.println("Error" + e);
}
}
}

ArpServer:
import java.io.*;
import java.net.*;
class ArpServer {
public static void main(String args[]) throws IOException {
try {
ServerSocket ss = new ServerSocket(1100);
Socket s = ss.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
String ip = br1.readLine();
Runtime r = Runtime.getRuntime();
Process p = r.exec("arp -a " + ip);
BufferedReader br2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str;
while ((str = br2.readLine()) != null) {
ps.println(str);
}
ps.println("end");
} catch (IOException e) {
System.out.println("Error" + e);
}
}
}
Program:
Clientrarp12:
import java.net.*;
import java.io.*;
public class Clientrarp12 {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
InetAddress addr = InetAddress.getByName("localhost");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
byte[] sendbyte = new byte[1024];
byte[] receivebyte = new byte[1024];
System.out.print("Enter the Physical address (MAC): ");
String macAddress = br.readLine();
sendbyte = macAddress.getBytes();
DatagramPacket sender = new DatagramPacket(sendbyte, sendbyte.length, addr, 1309);
ds.send(sender);
DatagramPacket receiver = new DatagramPacket(receivebyte, receivebyte.length);
ds.receive(receiver);
String ipAddress = new String(receiver.getData()).trim();
System.out.println("The Logical Address is (IP): " + ipAddress);
ds.close();
}
}

Serverrarp12:
import java.net.*;
import java.util.*;
public class Serverrarp12 {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(1309);
byte[] receivebyte = new byte[1024];
byte[] sendbyte = new byte[1024];
DatagramPacket receiver = new DatagramPacket(receivebyte, receivebyte.length);
ds.receive(receiver);
String macAddress = new String(receiver.getData()).trim();
System.out.println("Received MAC Address: " + macAddress);

// Get the local system's IP address


InetAddress localHost = InetAddress.getLocalHost();
String ipAddress = localHost.getHostAddress();
sendbyte = ipAddress.getBytes();
InetAddress addr = receiver.getAddress();
int port = receiver.getPort();
DatagramPacket sender = new DatagramPacket(sendbyte, sendbyte.length, addr, port);
ds.send(sender);
ds.close();
}
}
Program:
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k, count, err_pos = 0, flag = 0;
char dw[12], cw[12], data[8];

// Input data as a binary bit stream (7 bits)


printf("Enter data as binary bit stream (7 bits):\n");
scanf("%s", data);

// Initialize data word with appropriate bits and positions


for (i = 1, j = 0, k = 0; i < 12; i++) {
if (i == (int)pow(2, j)) { // Check for parity bit positions
dw[i] = '0'; // Initialize parity bits to '0'
j++;
} else {
dw[i] = data[k]; // Assign data bits
k++;
}
}

// Calculate parity bits


for (i = 0; i < 4; i++) {
count = 0;
for (j = (int)pow(2, i); j < 12; j += (int)pow(2, i + 1)) {
for (k = 0; k < (int)pow(2, i) && j + k < 12; k++) {
if (dw[j + k] == '1')
count++;
}
}
dw[(int)pow(2, i)] = (count % 2 == 0) ? '0' : '1';
}
// Display the code word
printf("Code word is:\n");
for (i = 1; i < 12; i++)
printf("%c", dw[i]);
printf("\n");

// Input received code word


printf("\nEnter the received hamming code:\n");
scanf("%s", cw + 1);

// Check for errors


for (i = 0; i < 4; i++) {
count = 0;
for (j = (int)pow(2, i); j < 12; j += (int)pow(2, i + 1)) {
for (k = 0; k < (int)pow(2, i) && j + k < 12; k++) {
if (cw[j + k] == '1')
count++;
}
}
if (count % 2 != 0)
err_pos += (int)pow(2, i);
}

// Print error information


if (err_pos == 0) {
printf("\nThere is no error in received code word.\n");
} else {
printf("\nThere is an error in bit position %d of received code word.\n", err_pos);
if (cw[err_pos] == dw[err_pos]) {
printf("There are 2 or more errors in received code...\n");
printf("Sorry...! Hamming code cannot correct 2 or more errors...\n");
flag = 1;
} else {
cw[err_pos] = (cw[err_pos] == '1') ? '0' : '1';
printf("\nCorrected code word is:\n");
for (i = 1; i < 12; i++)
printf("%c", cw[i]);
printf("\n");
}
}
return 0;
}

You might also like