Cnprograms
Cnprograms
Cnprograms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class crc {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Generator:");
String gen = br.readLine();
System.out.println("Enter Data:");
String data = br.readLine();
String code = data;
while(code.length() < (data.length() + gen.length() - 1))
code = code + "0";
code = data + div(code,gen);
System.out.println("The transmitted Code Word is: " + code);
System.out.println("Please enter the received Code Word: ");
String rec = br.readLine();
if(Integer.parseInt(div(rec,gen)) == 0)
System.out.println("The received code word contains no errors.");
else
System.out.println("The received code word contains errors.");
}
import java.util.Scanner;
public class hamming {
int[] arr ;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sender's message : ");
else{
System.out.println("Error in received code " + error);
}
sc.close();
}
static int Hamming(String sender , String receiver)
{
int count =0;
if(sender.length() != receiver.length())
{
return -1;
}
for(int i =0 ;i<sender.length();i++)
{
if(sender.charAt(i ) != receiver.charAt(i))
{
System.out.println(i);
count ++;
}
}
return count;
}
}
Program :
Server :
package exp2;
import java.io.*;
import java.net.*;
class gobacknserver {
public static void main(String args[]) throws IOException {
System.out.println("Server");
System.out.println("Waiting for connection...");
InetAddress addr = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(8011);
Socket client = new Socket();
client = ss.accept();
BufferedInputStream in = new BufferedInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
System.out.println("Received request for sending frames");
int p = in.read();
boolean f[] = new boolean[p];
int pc = in.read();
System.out.println("Sending...");
if (pc == 0) {
for (int i = 0; i < p; ++i) {
System.out.println("sending frame number " + i);
out.write(i);
out.flush();
System.out.println("Waiting for acknowledgement");
try {
Thread.sleep(7000);
} catch (Exception e) {
}
int a = in.read();
System.out.println("received acknowledgement for frame " + i + " as " + a);
}
out.flush();
} else {
for (int i = 0; i < p; ++i) {
if (i == 2) {
System.out.println("sending frame no " + i);
} else {
System.out.println("sending frame no " + i);
out.write(i);
out.flush();
System.out.println("Waiting for acknowledgement ");
try {
Thread.sleep(7000);
} catch (Exception e) {
}
int a = in.read();
if (a != 255) {
System.out.println("received ack for frame no: " + i + " as " + a);
f[i] = true;
}
}
}
for (int a = 0; a < p; ++a) {
if (f[a] == false) {
System.out.println("Resending frame " + a);
out.write(a);
out.flush();
System.out.println("Waiting for ack ");
try {
Thread.sleep(5000);
} catch (Exception e) {
}
int b = in.read();
System.out.println("received ack for frame no: " + a + " as " + b);
f[a] = true;
}
}
out.flush();
}
in.close();
out.close();
client.close();
ss.close();
System.out.println("Quitting");
}
}
Client :
package exp2;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class gobacknclient {
public static void main(String args[]) throws IOException {
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr);
Socket connection = new Socket(addr, 8011);
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
Scanner scr = new Scanner(System.in);
System.out.println("Client");
System.out.println("Enter the number of frames to be requested to the server");
int c = scr.nextInt();
out.write(c);
out.flush();
System.out.println("Enter the type of trans. Error=1 ; No Error=0");
int choice = scr.nextInt();
out.write(choice);
int check = 0;
int i = 0;
int j = 0;
if (choice == 0) {
for (j = 0; j < c; ++j) {
i = in.read();
System.out.println("received frame no: " + i);
System.out.println("Sending acknowledgement for frame no: " + i);
out.write(i);
out.flush();
}
out.flush();
} else {
for (j = 0; j < c; ++j) {
i = in.read();
if (i == check) {
System.out.println("received frame no: " + i);
System.out.println("Sending acknowledgement for frame no: " + i);
out.write(i);
++check;
} else {
--j;
System.out.println("Discarded frame no: " + i);
System.out.println("Sending NEGATIVE ack");
out.write(-1);
}
out.flush();
}
}
in.close();
out.close();
System.out.println("Quitting");
}
}
Program :
Server :
package exp2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class selectiverepserver {
static ServerSocket Serversocket;
static DataInputStream dis;
static DataOutputStream dos;
Client :
package exp2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.Random;
public class selectiverepclient {
static Socket connection;
}
for (int i = 0; i < p; i++)
if (v[i] == -1) {
System.out.println("Request to retransmit from packet no "
+ (i+1) + " again!!");
n = i;
out.write(n);
out.flush();
}
System.out.println();
v[n] = in.read();
System.out.println("Received frame is: " + v[n]);
System.out.println("quiting");
}
catch (Exception e) {
System.out.println(e);
}
}
}
Program :
Server :
package exp2;
import java.io.*;
import java.net.*;
class StopWaitReceiver {
public static void main(String args[]) throws Exception {
StopWaitReceiver swr = new StopWaitReceiver();
swr.run();
}
public void run() throws Exception {
String temp = "any message", str = "exit";
ServerSocket myss = new ServerSocket(9999);
Socket ss_accept = myss.accept();
BufferedReader ss_bf = new BufferedReader(new
InputStreamReader(ss_accept.getInputStream()));
PrintStream myps = new PrintStream(ss_accept.getOutputStream());
while (temp.compareTo(str) != 0) {
Thread.sleep(1000);
temp = ss_bf.readLine();
if (temp.compareTo(str) == 0) {
break;
}
System.out.println("Frame " + temp + " was received");
Thread.sleep(500);
myps.println("Received");
}
System.out.println("ALL FRAMES WERE RECEIVED SUCCESSFULLY");
}
}
Client :
package exp2;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class stopwaitclient {
public static void main(String args[]) throws Exception {
stopwaitclient sws = new stopwaitclient();
sws.run();
}
public void run() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of frames to be sent:");
int n = sc.nextInt();
Socket myskt = new Socket("localhost", 9999);
PrintStream myps = new PrintStream(myskt.getOutputStream());
for (int i = 0; i <= n;) {
if (i == n) {
myps.println("exit");
break;
}
System.out.println("Frame no " + i + " is sent");
myps.println(i);
BufferedReader bf = new BufferedReader(new InputStreamReader(myskt.getInputStream()));
String ack = bf.readLine();
if (ack != null) {
System.out.println("Acknowledgement was Received from receiver");
i++;
Thread.sleep(4000);
} else {
myps.println(i);
}
}
sc.close();
}
}
Program :
Server :
import java.io.*;
import java.net.*;
public class echoserver {
public static void main(String args[]) throws Exception
{
String echoin;
ServerSocket svrsoc;
Socket soc;
BufferedReader br;
try
{
svrsoc = new ServerSocket(2210);
soc = svrsoc.accept();
br = new BufferedReader (new InputStreamReader(soc.getInputStream()));
PrintStream ps = new PrintStream(soc.getOutputStream());
System.out.println("Connected for echo:");
while((echoin=br.readLine())!=null)
{
if(echoin.equals("end"))
{
System.out.println("Client disconnected");
br.close();
break;
}
else
ps.println(echoin);
}
}
catch(UnknownHostException e)
{
System.out.println(e.toString());
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
Client :
import java.io.*;
import java.net.*;
public class echoclient {
public static void main(String args[]) throws Exception
{
String sockin;
try
{
Socket csock = new Socket(InetAddress.getLocalHost(),2210);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br_sock = new BufferedReader(new InputStreamReader(csock.getInputStream()));
PrintStream ps = new PrintStream(csock.getOutputStream());
System.out.println("Start echoing... type 'end' to terminate");
while((sockin=br.readLine())!=null)
{
ps.println(sockin);
if(sockin.equals("end"))
break;
else
System.out.println("echoed from server:"+br_sock.readLine());
}
}
catch(UnknownHostException e)
{
System.out.println(e.toString());
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
Program :
Server :
package exp3b;
import java.io.IOException;
import java.net.*;
public class pingserver {
private static final int PORT = 12345;
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
System.out.println("Server started. Listening on port " + PORT);
while (true) {
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
System.out.println("Received ping from client: " + message);
Client :
package exp3b;
import java.io.IOException;
import java.net.*;
public class pingclient {
private static final String SERVER_IP = "localhost";
private static final int SERVER_PORT = 12345;
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
for (int i = 0; i < 5; i++) {
String message = "Ping " + (i + 1);
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
serverAddress, SERVER_PORT);
socket.send(sendPacket);
Thread.sleep(1000);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
Program :
Server :
package exp3c;
import java.io.*;
import java.net.*;
import java.util.*;
public class talkserver {
private static Set<PrintWriter> clientWriters = new HashSet<>();
public static void main(String[] args) {
System.out.println("Server is running on port 12345 : ");
try (ServerSocket serverSocket = new ServerSocket(12345)) {
while (true) {
new ClientHandler(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
}
}
private static class ClientHandler extends Thread {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
synchronized (clientWriters) {
clientWriters.add(out);
}
String message;
while ((message = in.readLine()) != null) {
System.out.println("Received: " + message);
broadcast(message);
}
} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
synchronized (clientWriters) {
clientWriters.remove(out);
}
}
}
private void broadcast(String message) {
synchronized (clientWriters) {
for (PrintWriter writer : clientWriters) {
writer.println(message);
}
}
}
}
}
Client :
package exp3c;
import java.io.*;
import java.net.*;
public class talkclient {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 12345);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in)) ) {
System.out.println("Connected to the server.");
String message;
while (true) {
System.out.print("You: ");
message = consoleInput.readLine();
out.println(message);
if (message.equals("bye")) {
break;
}
String response = in.readLine();
System.out.println("Server: " + response);
}
} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
}
}
}
Program :
Server :
import java.io.*;
import java.net.*;
public class tcpserver{
public static void main(String[] args) {
try {
Client :
import java.io.*;
import java.net.*;
public class tcpclient{
public static void main(String[] args) {
try {
Program :
Server :
import java.io.*;
import java.net.*;
public class udpserver {
public static void main(String args[]) {
try {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
System.out.println("Server started. Waiting for connections...");
ByteArrayInputStream byteArrayInputStream = new
ByteArrayInputStream(receivePacket.getData());
DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
String fileName = dataInputStream.readUTF();
FileOutputStream fileOutputStream = new FileOutputStream("output.txt", true);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = dataInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
fileOutputStream.close();
dataInputStream.close();
serverSocket.close();
System.out.println("File " + fileName + " received from client.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client :
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class udpclient{
public static void main(String args[]) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
int port = 9876;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the file to send: ");
String fileName = scanner.nextLine();
File file = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] fileData = new byte[(int) file.length()];
fileInputStream.read(fileData);
fileInputStream.close();
DatagramPacket sendPacket = new DatagramPacket(fileData, fileData.length, IPAddress, port);
clientSocket.send(sendPacket);
System.out.println("File " + fileName + " sent to server.");
clientSocket.close();
scanner.close();
}
}