Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- public class Sender {
- Random random=new Random();
- CRC crc;
- byte[] msg= new byte[17]; //potwierdzenie, odrzucenie
- final byte[] byteArray = new byte[] {0x41, 0x42, 0x43,0x44,0x45,0x46,0x47,0x48,
- 0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50};
- byte[] byteArrayWithCRC = new byte[] {0x41, 0x42, 0x43,0x44,0x45,0x46,0x47,0x48,
- 0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x0};
- private static final byte[] ack = new byte[16];
- private static final byte[] nAck = new byte[16];
- double noise;
- static {
- //ustawia każdy bajt informacji o przyjęciu wiadomości na zera
- Arrays.fill(ack, (byte)0);
- //każdy bajt informacji o odrzuceniu na jedynki
- Arrays.fill(nAck, (byte)-1);
- }
- private Socket clientSocket;
- private PrintWriter out;
- private BufferedReader in;
- public void nextGaus(){
- noise=random.nextGaussian();
- while((noise>1)||(noise<0)){
- noise=random.nextGaussian();
- }
- }
- public void makeNoise(byte[] byteArray){
- for(int i=0; i<16;i++){
- nextGaus();
- if(noise<0.002){
- nextGaus();
- if(noise<0.5){
- this.byteArrayWithCRC[i]+=0x5;
- }
- else{
- this.byteArrayWithCRC[i]-=0x5;
- }
- }
- }
- }
- public void startConnection(String ip, int port) {
- Scanner scanner = new Scanner(System.in);
- try{
- clientSocket = new Socket(ip, port);
- //out = new PrintWriter(clientSocket.getOutputStream(), true);
- //in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- }
- catch (IOException e){ }
- }
- public void sendMessage(byte[] msg) {
- try{
- clientSocket.getOutputStream().write(byteArrayWithCRC);
- }
- catch(IOException a){
- }
- }
- public void readMessage() {
- try{
- clientSocket.getInputStream().read(msg);
- }
- catch(IOException a){
- a.getMessage();
- }
- }
- public void stopConnection() {
- try{
- in.close();
- out.close();
- clientSocket.close();
- }
- catch (IOException ac){}
- }
- public static void main(String[] args) {
- int loops = 0;
- Scanner scanner = new Scanner(System.in);
- CsvWriter writer = new CsvWriter("senderErrors.csv");
- Sender sender = new Sender();
- CRC crc = new CRC();
- crc.reset();
- crc.update(sender.byteArray);
- sender.byteArrayWithCRC[16] = (byte) crc.getValue(); //17BIT TO CRC
- //sender.msg=null;
- System.out.println(sender.byteArrayWithCRC[16]);
- sender.startConnection("localhost", 2000); //WPISAC IP
- sender.sendMessage(sender.byteArrayWithCRC);
- int count = 0, incorrect = 0;
- //while
- while (true) {
- loops=0;
- count=0;
- while ((loops <= 0) && (loops % 100 != 0)) {
- System.out.println("How many loop do you want to perform. Enter x (x mod 100==0");
- loops = scanner.nextInt();
- }
- if((count%100)==99)
- {
- writer.write(incorrect);
- System.out.println(incorrect);
- incorrect=0;
- }
- while (count < loops) {
- sender.readMessage();
- if (sender.msg != null) {
- if (sender.msg[1] == ack[1]) {
- sender.makeNoise(sender.byteArrayWithCRC);
- sender.sendMessage(sender.byteArrayWithCRC);
- System.out.println(count + ". Sending Success");
- } else if (sender.msg[1] == nAck[1]) {
- incorrect++;
- for (int i = 0; i < 16; i++)
- sender.byteArrayWithCRC[i] = sender.byteArray[i];
- sender.makeNoise(sender.byteArrayWithCRC);
- sender.sendMessage(sender.byteArrayWithCRC);
- System.out.println(count + ". Sending again due to incorrect CRC");
- } else {
- System.out.println("ERROR: INCORRECT REPLY FROM RECEIVER");
- }
- }
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- }
- count++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement