Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.Arrays;
  5.  
  6. /**
  7. *
  8. * @author Arlidio Dobra
  9. * UDP Project for Computer Networking
  10. */
  11.  
  12.  
  13. public class ClientApplication {
  14.  
  15.  
  16. public static void main(String[] args) {
  17.  
  18. //Scanner to be used to accept user messages
  19. Scanner userIn = new Scanner(System.in);
  20.  
  21. //Port number to be used to send / search for messages
  22. int port = 8000;
  23.  
  24. //Below message input is accepted and stored within variables
  25. System.out.println("Enter the first message:");
  26. String messageOne = userIn.next();
  27.  
  28.  
  29. System.out.println("Enter the second message:");
  30. String messageTwo = userIn.next();
  31.  
  32. System.out.println("Enter the third message:");
  33. String messageThree = userIn.next();
  34.  
  35. //try / catch statement used to attempt below code and respond if an issue occurs
  36. try {
  37.  
  38.  
  39. //Socket created to function as entry point for UDP messages
  40. DatagramSocket messageSocket = new DatagramSocket();
  41.  
  42. /**
  43. * Parameters used to define IP address
  44. * where packets are to be sent to.
  45. */
  46. InetAddress host = InetAddress.getByName("localhost");
  47.  
  48.  
  49. //Converts user messages as bytes to be transmitted with UDP and sent as Datagram Packets
  50. //Packets sent through socket created priorly
  51. byte[] messageA = messageOne.getBytes();
  52. DatagramPacket packetOne = new DatagramPacket(messageA, messageA.length,
  53. host,
  54. port
  55. );
  56. messageSocket.send(packetOne);
  57.  
  58.  
  59. byte[] messageB = messageTwo.getBytes();
  60. DatagramPacket packetTwo = new DatagramPacket(messageB, messageB.length,
  61. host,
  62. port
  63. );
  64. messageSocket.send(packetTwo);
  65.  
  66.  
  67. byte[] messageC = messageThree.getBytes();
  68. DatagramPacket packetThree = new DatagramPacket(messageC, messageC.length,
  69. host,
  70. port
  71. );
  72. messageSocket.send(packetThree);
  73.  
  74.  
  75. }
  76.  
  77. //Add proper explanation
  78. catch (Exception ex) {
  79.  
  80. }
  81. //Debug
  82. System.out.println("\n" + "Sent packets");
  83.  
  84. String mergedInput = messageOne + messageTwo + messageThree;
  85.  
  86. receiveResponse(mergedInput);
  87.  
  88.  
  89.  
  90. }
  91.  
  92.  
  93. //-----------------------------------------------------------------------------------------
  94. // Below is the part of the program which accepts and verfies Server-side response
  95.  
  96.  
  97.  
  98. //Respondsible for receiving serverResponse
  99. public static void receiveResponse(String combinedInput){
  100.  
  101.  
  102. int port = 7650;
  103.  
  104.  
  105.  
  106.  
  107. String messageArray[] = new String[2]; //declaration and instantiation
  108.  
  109.  
  110.  
  111. //Add proper explanation
  112. try{
  113.  
  114.  
  115. DatagramSocket receiveSocket = new DatagramSocket(port);
  116.  
  117. InetAddress host = InetAddress.getByName("localhost");
  118.  
  119.  
  120. //Buffer created to reserve memory for incoming messages - anticipating large messages
  121. byte [] buffer = new byte[65507];
  122.  
  123.  
  124.  
  125.  
  126. for(int i = 0; i < 2; i++){
  127.  
  128. DatagramPacket responsePacket = new DatagramPacket(buffer, buffer.length);
  129.  
  130. receiveSocket.receive(responsePacket);
  131. String receivedString = new String(responsePacket.getData());
  132. receivedString = receivedString.trim();
  133. messageArray[i] = receivedString;
  134. //System.out.println(Arrays.toString(messageArray));
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. }
  142. catch (Exception ex){}
  143.  
  144.  
  145. //
  146. //messageArray[1] = messageArray[1].replaceAll("[^\\d.]", "");
  147.  
  148. //Debugging Purposes
  149. //System.out.println(Arrays.toString(messageArray));
  150.  
  151.  
  152. String combinedString = messageArray[0],
  153. textLengthStr = messageArray[1].replaceAll("[^\\d.]", "");
  154.  
  155. combinedString = combinedString.replaceAll("#","");
  156. //textLengthStr = textLengthStr.replaceAll();
  157.  
  158. int textLength = Integer.valueOf(textLengthStr),
  159. inputLength = combinedInput.length();
  160.  
  161.  
  162. System.out.println(combinedString);
  163. System.out.println(textLength);
  164.  
  165.  
  166. if(combinedString.equals(combinedInput) && inputLength == textLength)
  167. {
  168. System.out.println("The server was able to successfully recieve and merge the inputted words" +
  169. "\n" + "Your merged text is: " + combinedInput +
  170. "\n" + "The Server-side merged text is: " + combinedString);
  171.  
  172. System.out.println("The server was able to successfully determain the length of user input" +
  173. "\n" + "User input length was: " + textLength);
  174.  
  175. }
  176.  
  177. else
  178. {
  179. System.out.println("The server was unable to successfully verify input." +
  180. "\n" + "The dev must be a moron");
  181.  
  182. System.out.println("Heres the debug - moron: " +"\n" + "Your Length: " +
  183. textLength + "ACTUAL Length: " + inputLength + "\n" + "Your words: " +
  184. combinedString + "ACTUAL words: " + combinedInput);
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement