Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.net.*;
- import java.util.Arrays;
- /**
- *
- * @author Arlidio Dobra
- * UDP Project for Computer Networking
- */
- public class ClientApplication {
- public static void main(String[] args) {
- //Scanner to be used to accept user messages
- Scanner userIn = new Scanner(System.in);
- //Port number to be used to send / search for messages
- int port = 8000;
- //Below message input is accepted and stored within variables
- System.out.println("Enter the first message:");
- String messageOne = userIn.next();
- System.out.println("Enter the second message:");
- String messageTwo = userIn.next();
- System.out.println("Enter the third message:");
- String messageThree = userIn.next();
- //try / catch statement used to attempt below code and respond if an issue occurs
- try {
- //Socket created to function as entry point for UDP messages
- DatagramSocket messageSocket = new DatagramSocket();
- /**
- * Parameters used to define IP address
- * where packets are to be sent to.
- */
- InetAddress host = InetAddress.getByName("localhost");
- //Converts user messages as bytes to be transmitted with UDP and sent as Datagram Packets
- //Packets sent through socket created priorly
- byte[] messageA = messageOne.getBytes();
- DatagramPacket packetOne = new DatagramPacket(messageA, messageA.length,
- host,
- port
- );
- messageSocket.send(packetOne);
- byte[] messageB = messageTwo.getBytes();
- DatagramPacket packetTwo = new DatagramPacket(messageB, messageB.length,
- host,
- port
- );
- messageSocket.send(packetTwo);
- byte[] messageC = messageThree.getBytes();
- DatagramPacket packetThree = new DatagramPacket(messageC, messageC.length,
- host,
- port
- );
- messageSocket.send(packetThree);
- }
- //Add proper explanation
- catch (Exception ex) {
- }
- //Debug
- System.out.println("\n" + "Sent packets");
- String mergedInput = messageOne + messageTwo + messageThree;
- receiveResponse(mergedInput);
- }
- //-----------------------------------------------------------------------------------------
- // Below is the part of the program which accepts and verfies Server-side response
- //Respondsible for receiving serverResponse
- public static void receiveResponse(String combinedInput){
- int port = 7650;
- String messageArray[] = new String[2]; //declaration and instantiation
- //Add proper explanation
- try{
- DatagramSocket receiveSocket = new DatagramSocket(port);
- InetAddress host = InetAddress.getByName("localhost");
- //Buffer created to reserve memory for incoming messages - anticipating large messages
- byte [] buffer = new byte[65507];
- for(int i = 0; i < 2; i++){
- DatagramPacket responsePacket = new DatagramPacket(buffer, buffer.length);
- receiveSocket.receive(responsePacket);
- String receivedString = new String(responsePacket.getData());
- receivedString = receivedString.trim();
- messageArray[i] = receivedString;
- //System.out.println(Arrays.toString(messageArray));
- }
- }
- catch (Exception ex){}
- //
- //messageArray[1] = messageArray[1].replaceAll("[^\\d.]", "");
- //Debugging Purposes
- //System.out.println(Arrays.toString(messageArray));
- String combinedString = messageArray[0],
- textLengthStr = messageArray[1].replaceAll("[^\\d.]", "");
- combinedString = combinedString.replaceAll("#","");
- //textLengthStr = textLengthStr.replaceAll();
- int textLength = Integer.valueOf(textLengthStr),
- inputLength = combinedInput.length();
- System.out.println(combinedString);
- System.out.println(textLength);
- if(combinedString.equals(combinedInput) && inputLength == textLength)
- {
- System.out.println("The server was able to successfully recieve and merge the inputted words" +
- "\n" + "Your merged text is: " + combinedInput +
- "\n" + "The Server-side merged text is: " + combinedString);
- System.out.println("The server was able to successfully determain the length of user input" +
- "\n" + "User input length was: " + textLength);
- }
- else
- {
- System.out.println("The server was unable to successfully verify input." +
- "\n" + "The dev must be a moron");
- System.out.println("Heres the debug - moron: " +"\n" + "Your Length: " +
- textLength + "ACTUAL Length: " + inputLength + "\n" + "Your words: " +
- combinedString + "ACTUAL words: " + combinedInput);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement