0% found this document useful (0 votes)
9 views7 pages

CN Lab

The document contains multiple Java programs demonstrating client-server communication using TCP and UDP, as well as implementations of routing algorithms like Link State and Distance Vector. It also includes a simple error correction code simulation using CRC. Each section provides code examples for server and client applications along with explanations of their functionalities.

Uploaded by

rajstarkumar100
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)
9 views7 pages

CN Lab

The document contains multiple Java programs demonstrating client-server communication using TCP and UDP, as well as implementations of routing algorithms like Link State and Distance Vector. It also includes a simple error correction code simulation using CRC. Each section provides code examples for server and client applications along with explanations of their functionalities.

Uploaded by

rajstarkumar100
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/ 7

1.

Simple Client Server Program TCP

Server:

//SimpleServer.java

import java.io.*;
import java.net.*;

public class SimpleServer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
System.out.println("Server is waiting...");
Socket s = ss.accept();

DataInputStream dis = new DataInputStream(s.getInputStream());


String msg = dis.readUTF();
System.out.println("Client says: " + msg);

ss.close();
s.close();
}
}

Client:
//SimpleClient.java
import java.io.*;
import java.net.*;

public class SimpleClient {


public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());


dos.writeUTF("Hello from Client!");

s.close();
}
}
2.Socket Program for Echo / Ping Commands

2(A). TCP Echo

Server:
//SimpleTCPEchoServer.java
import java.net.*;
import java.io.*;

public class SimpleTCPEchoServer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
System.out.println("Server is running...");

Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());

String msg = dis.readUTF();


System.out.println("Received: " + msg);
dos.writeUTF("Echo: " + msg); // Echoing back the same message

s.close();
ss.close();
}
}

Client:
//SimpleTCPEchoClient.java
import java.net.*;
import java.io.*;

public class SimpleTCPEchoClient {


public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());


DataInputStream dis = new DataInputStream(s.getInputStream());

dos.writeUTF("Hello Server!");
String reply = dis.readUTF();
System.out.println("Server replied: " + reply);

s.close();
}
}
2(B).Ping Program

// PingServer.java
import java.io.*;
import java.net.*;

class PingServer {
public static void main(String args[]) {
try {
String str;
System.out.print("Enter the IP Address to be Ping: ");
BufferedReader buf1 = new BufferedReader(new InputStreamReader(System.in));
String ip = buf1.readLine();

Runtime runtime = Runtime.getRuntime();


Process p = runtime.exec("ping " + ip);

InputStream in = p.getInputStream();
BufferedReader buf2 = new BufferedReader(new InputStreamReader(in));

while ((str = buf2.readLine()) != null) {


System.out.println(str);
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
3.Link State Routing Algorithm

// SimpleLSR.java
public class SimpleLSR {
public static void main(String[] args) {
int[][] graph = {
{0, 1, 4},
{1, 0, 2},
{4, 2, 0}
};
int[] dist = dijkstra(graph, 0);
for (int i = 0; i < dist.length; i++) {
System.out.println("Distance from 0 to " + i + " is " + dist[i]);
}
}

static int[] dijkstra(int[][] g, int src) {


int n = g.length, INF = 9999;
int[] dist = new int[n];
boolean[] visited = new boolean[n];

for (int i = 0; i < n; i++) dist[i] = INF;


dist[src] = 0;

for (int i = 0; i < n; i++) {


int u = -1, min = INF;
for (int j = 0; j < n; j++)
if (!visited[j] && dist[j] < min) {
min = dist[j];
u = j;
}
if (u == -1) break;
visited[u] = true;
for (int v = 0; v < n; v++)
if (!visited[v] && g[u][v] != 0 && dist[u] + g[u][v] < dist[v])
dist[v] = dist[u] + g[u][v];
}
return dist;
}
}
4.Distance Vector Routing Algorithm

// MiniDVR.java
public class MiniDVR {
public static void main(String[] args) {
int INF = 9999;
int V = 3;
int[][] graph = {
{0, 1, 4},
{1, 0, 2},
{4, 2, 0}
};
int[][] dist = new int[V][V];

// Initialize dist with graph values


for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

// One iteration of Distance Vector update


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
for (int k = 0; k < V; k++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

// Print updated distances


for (int i = 0; i < V; i++) {
System.out.print("From node " + i + ": ");
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
System.out.print("INF ");
else
System.out.print(dist[i][j] + " ");
}
System.out.println();
}
}
}
6.Simple Client Server Program - UDP

Server:
// SimpleUDPServer.java
import java.net.*;

public class SimpleUDPServer {


public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9876);
byte[] buffer = new byte[1024];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);


socket.receive(packet);

String msg = new String(packet.getData(), 0, packet.getLength());


System.out.println("Received: " + msg);

socket.close();
}
}

Client:
// SimpleUDPClient.java
import java.net.*;

public class SimpleUDPClient {


public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
byte[] msg = "Hello UDP".getBytes();

InetAddress address = InetAddress.getByName("localhost");


DatagramPacket packet = new DatagramPacket(msg, msg.length, address, 9876);

socket.send(packet);
socket.close();
}
}
7.Simulation of Error Correction Code ( Like CRC )

// TinyCRC.java
import java.util.Scanner;

public class TinyCRC {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter data bits (e.g. 1101): ");


String data = sc.nextLine();

System.out.print("Enter divisor bits (e.g. 1011): ");


String divisor = sc.nextLine();

// Append zeros equal to divisor length - 1


String dividend = data + "0".repeat(divisor.length() - 1);
String remainder = xorDivide(dividend, divisor);
String crc = data + remainder;
System.out.println("CRC code: " + crc);
System.out.print("Enter received code: ");
String received = sc.nextLine();
String check = xorDivide(received, divisor);

if (check.contains("1")) System.out.println("Error detected");


else System.out.println("No error detected");

sc.close();
}

static String xorDivide(String dividend, String divisor) {


String tmp = dividend.substring(0, divisor.length());

int cur = divisor.length();

while (cur <= dividend.length()) {


StringBuilder sb = new StringBuilder();

// XOR operation between tmp and divisor


for (int i = 1; i < divisor.length(); i++) {
sb.append(tmp.charAt(i) == divisor.charAt(i) ? '0' : '1');
}

if (cur == dividend.length()) break;

tmp = sb.toString() + dividend.charAt(cur);


cur++;
}
return tmp.substring(1);
}
}

You might also like