Practical JAVA
Practical JAVA
Practical JAVA
import java.util.Scanner;
public class CRC{
static String data = "";
static String checkValue = "";
static String genPoly = "";
static int dataLength, i,j;
static int N;
public static void XOR() {
StringBuilder result = new StringBuilder();
for (j = 1; j < N; j++) {
result.append((checkValue.charAt(j) ==
genPoly.charAt(j)) ? '0' : '1');
}
checkValue = result.toString();
}
public static void receiver() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the received data: ");
String receivedData = scanner.next();
data=receivedData;
System.out.println("\n----------------------");
System.out.println("Data received: " +
receivedData);
crc();
for (i=0; (i<N-1) && (checkValue.charAt(i) != '
1'); i++) {
}
if (i <N-1) {
System.out.println("\nError detected\n"
);
} else {
System.out.println("\nNo error detected
\n");
}
scanner.close();
}
public static void crc() {
checkValue = data;
N = genPoly.length();
do {
if (checkValue.charAt(0) == '1') {
XOR();
}
checkValue = checkValue.substring(1) + (i
< data.length() ? data.charAt(i) : '0');
i++;
} while (i <= dataLength + N - 1);
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter data to be
transmitted: ");
String inputData = scanner.next();
data=inputData;
System.out.print("\nEnter the Generating
polynomial: ");
String genPolyInput = scanner.next();
genPoly = genPolyInput;
dataLength=inputData.length();
for (i = dataLength; i < dataLength +
genPoly.length() - 1; i++) {
data += '0';
}
System.out.println("\n
--------------------------------------");
System.out.println("Data padded with n-1
zeros: " + data);
System.out.println(
"----------------------------------------");
crc();
System.out.println("\nCRC or Check value is
: " + checkValue);
for (i = dataLength; i < dataLength +
genPoly.length() - 1; i++) {
data = data.substring(0, i) +
checkValue.charAt(i - dataLength) + data.substring(i +
1);
}
System.out.println("\n
-----------------------");
System.out.println("Final data to be sent:"
+ data);
System.out.println(
"--------------------------");
receiver();
}
}
//GoBack-N
import java.util.*;
for(int i=r+n;i>0;i--){
int l = log2(i);
if(Math.pow(2,l) != i){
a[r+n-i] = b[index++];
}else{
a[r+n-i]=-1;
}
}
String str="",str2="";
while(!str.equals("stop")){
System.out.println("Enter Client side
message:");
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
//Socket Program Server side (2/2)
import java.net.*;
import java.io.*;
class ServerSocketDemo{
public static void main(String args[])throws
Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.
getInputStream());
DataOutputStream dout=new DataOutputStream(s.
getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
System.out.println("Enter Server side
message:");
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
//IPv4 Subnet calculations
import java.util.Scanner;