Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Arrays;
  7. import java.util.Random;
  8. import java.util.Scanner;
  9.  
  10. public class Sender {
  11.  
  12. Random random=new Random();
  13. CRC crc;
  14. byte[] msg= new byte[17]; //potwierdzenie, odrzucenie
  15.  
  16. final byte[] byteArray = new byte[] {0x41, 0x42, 0x43,0x44,0x45,0x46,0x47,0x48,
  17. 0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50};
  18.  
  19. byte[] byteArrayWithCRC = new byte[] {0x41, 0x42, 0x43,0x44,0x45,0x46,0x47,0x48,
  20. 0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x0};
  21.  
  22. private static final byte[] ack = new byte[16];
  23. private static final byte[] nAck = new byte[16];
  24. double noise;
  25.  
  26. static {
  27. //ustawia każdy bajt informacji o przyjęciu wiadomości na zera
  28. Arrays.fill(ack, (byte)0);
  29. //każdy bajt informacji o odrzuceniu na jedynki
  30. Arrays.fill(nAck, (byte)-1);
  31. }
  32.  
  33.  
  34. private Socket clientSocket;
  35. private PrintWriter out;
  36. private BufferedReader in;
  37.  
  38.  
  39. public void nextGaus(){
  40. noise=random.nextGaussian();
  41. while((noise>1)||(noise<0)){
  42. noise=random.nextGaussian();
  43. }
  44. }
  45.  
  46. public void makeNoise(byte[] byteArray){
  47.  
  48. for(int i=0; i<16;i++){
  49.  
  50. nextGaus();
  51. if(noise<0.002){
  52. nextGaus();
  53. if(noise<0.5){
  54. this.byteArrayWithCRC[i]+=0x5;
  55. }
  56. else{
  57. this.byteArrayWithCRC[i]-=0x5;
  58. }
  59. }
  60. }
  61.  
  62. }
  63.  
  64. public void startConnection(String ip, int port) {
  65. Scanner scanner = new Scanner(System.in);
  66.  
  67. try{
  68. clientSocket = new Socket(ip, port);
  69. //out = new PrintWriter(clientSocket.getOutputStream(), true);
  70. //in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  71. }
  72. catch (IOException e){ }
  73. }
  74.  
  75. public void sendMessage(byte[] msg) {
  76. try{
  77. clientSocket.getOutputStream().write(byteArrayWithCRC);
  78. }
  79. catch(IOException a){
  80. }
  81. }
  82.  
  83. public void readMessage() {
  84.  
  85. try{
  86. clientSocket.getInputStream().read(msg);
  87. }
  88. catch(IOException a){
  89. a.getMessage();
  90. }
  91. }
  92.  
  93. public void stopConnection() {
  94. try{
  95. in.close();
  96. out.close();
  97. clientSocket.close();
  98. }
  99. catch (IOException ac){}
  100. }
  101.  
  102.  
  103.  
  104. public static void main(String[] args) {
  105. int loops = 0;
  106. Scanner scanner = new Scanner(System.in);
  107. CsvWriter writer = new CsvWriter("senderErrors.csv");
  108. Sender sender = new Sender();
  109. CRC crc = new CRC();
  110. crc.reset();
  111. crc.update(sender.byteArray);
  112. sender.byteArrayWithCRC[16] = (byte) crc.getValue(); //17BIT TO CRC
  113. //sender.msg=null;
  114. System.out.println(sender.byteArrayWithCRC[16]);
  115. sender.startConnection("localhost", 2000); //WPISAC IP
  116. sender.sendMessage(sender.byteArrayWithCRC);
  117.  
  118.  
  119. int count = 0, incorrect = 0;
  120.  
  121.  
  122. //while
  123. while (true) {
  124. loops=0;
  125. count=0;
  126. while ((loops <= 0) && (loops % 100 != 0)) {
  127. System.out.println("How many loop do you want to perform. Enter x (x mod 100==0");
  128. loops = scanner.nextInt();
  129. }
  130.  
  131. if((count%100)==99)
  132. {
  133. writer.write(incorrect);
  134. System.out.println(incorrect);
  135. incorrect=0;
  136.  
  137. }
  138.  
  139. while (count < loops) {
  140. sender.readMessage();
  141. if (sender.msg != null) {
  142. if (sender.msg[1] == ack[1]) {
  143. sender.makeNoise(sender.byteArrayWithCRC);
  144. sender.sendMessage(sender.byteArrayWithCRC);
  145. System.out.println(count + ". Sending Success");
  146.  
  147.  
  148. } else if (sender.msg[1] == nAck[1]) {
  149. incorrect++;
  150. for (int i = 0; i < 16; i++)
  151. sender.byteArrayWithCRC[i] = sender.byteArray[i];
  152. sender.makeNoise(sender.byteArrayWithCRC);
  153. sender.sendMessage(sender.byteArrayWithCRC);
  154. System.out.println(count + ". Sending again due to incorrect CRC");
  155.  
  156. } else {
  157. System.out.println("ERROR: INCORRECT REPLY FROM RECEIVER");
  158.  
  159. }
  160. }
  161. try {
  162. Thread.sleep(50);
  163. } catch (InterruptedException e) {
  164.  
  165. }
  166. count++;
  167.  
  168. }
  169.  
  170.  
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement