Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. /**
  6. *
  7. * @author Arlidio Dobra
  8. * UDP Project for Computer Networking
  9. */
  10.  
  11. public class ServerApplication {
  12.  
  13. public static void main() {
  14.  
  15. /**
  16. * Varaiables to be used to determain amount of messages recieved,
  17. * Length of the messages combined & the value of all messages combined -
  18. * including # as seperators
  19. */
  20. int messageCount = 0, textLength = 0, stringLength = 0;
  21. String combinedStrings = "";
  22.  
  23. //Below code implemented to force virtual machine to display console
  24. System.out.print("Server Client");
  25.  
  26.  
  27. //Defining inbound location of packets being sent
  28. int port = 8000;
  29.  
  30.  
  31. //Add proper comment
  32. try {
  33.  
  34. //Socket defined and instructed to seek port 8000 - where client will send messages
  35. DatagramSocket serverSocket = new DatagramSocket(port);
  36.  
  37. InetAddress host = InetAddress.getByName("localhost");
  38.  
  39. //Buffer created to reserve memory for incoming messages - anticipating large messages
  40. byte[] buffer = new byte[65507];
  41.  
  42.  
  43.  
  44. while (messageCount <= 3) {
  45.  
  46. //Datagram packet defined to be used as receiving point of packets - implements parameter buffer
  47. DatagramPacket serverPacket = new DatagramPacket(buffer, buffer.length);
  48.  
  49.  
  50. //Seeks and recieves any packets present in socket
  51. serverSocket.receive(serverPacket);
  52.  
  53. //Converts byte information into String and stores it into variable
  54. String gotMail = new String(serverPacket.getData());
  55.  
  56. //Cuts out excess information from allocated buffer memory - leaving only used information
  57. //within variable
  58. gotMail = gotMail.trim();
  59.  
  60. //nLength repsonsible for storing length of each recieved message after excess buffer is trimmed
  61. textLength += gotMail.length();
  62.  
  63. //For each iteration and message which is recieved - msgCount will increase by increment of 1
  64. //until while loop condition is met and all messages have been recieved
  65. messageCount += 1;
  66.  
  67. //IF loop respondisble for observing amount of messages recieved and placing seperators within the
  68. //message. For the first 2 recieved messages '#' will be added as seperator
  69. if (messageCount <= 2) {
  70. gotMail += '#';
  71. }
  72.  
  73. //combinedStrings holds value of message recieved from packet after '#' seperator has been appended
  74. combinedStrings += gotMail;
  75.  
  76. if(messageCount == 3){
  77. break;
  78. }
  79.  
  80. }
  81.  
  82. } catch (Exception ex) {
  83. return;
  84. }
  85.  
  86.  
  87.  
  88. //Debugging Process
  89. System.out.println("\n" + "Combined text:" + combinedStrings);
  90. System.out.println("\n" + "Length of text:" + textLength);
  91.  
  92.  
  93. serverResponse(combinedStrings, textLength);
  94. }
  95.  
  96.  
  97.  
  98. //Respondsible for returning message to clientApplication
  99. public static void serverResponse(String returnMessage, int messageLength){
  100.  
  101.  
  102. int port = 7650;
  103.  
  104. String messageLengthStr = String.valueOf(messageLength);
  105.  
  106.  
  107.  
  108. //Add proper comment
  109. try {
  110.  
  111. //severSocket defined once more into try / catch as - to be used as socket where messages are to be sent
  112. DatagramSocket responseSocket = new DatagramSocket();
  113.  
  114. /**
  115. * Parameters used to define IP address where packets are to be sent to.
  116. * "localhost" can be substituted for IP address of client machine -
  117. * For troubleshooting purposes "localhost" is defined to use current machine
  118. * to send/recieve messages
  119. */
  120.  
  121.  
  122.  
  123. InetAddress host = InetAddress.getByName("localhost");
  124.  
  125.  
  126. byte[] responseByte = returnMessage.getBytes();
  127. DatagramPacket responsePacket = new DatagramPacket(responseByte, responseByte.length,
  128. host,
  129. port
  130. );
  131. responseSocket.send(responsePacket);
  132.  
  133.  
  134. byte[] responseByteTwo = messageLengthStr.getBytes();
  135. DatagramPacket responsePacketTwo = new DatagramPacket(responseByteTwo, responseByteTwo.length,
  136. host,
  137. port
  138. );
  139. responseSocket.send(responsePacketTwo);
  140.  
  141.  
  142.  
  143.  
  144. }
  145.  
  146.  
  147. catch(Exception ex){return;}
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement