Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.net.*;
- /**
- *
- * @author Arlidio Dobra
- * UDP Project for Computer Networking
- */
- public class ServerApplication {
- public static void main() {
- /**
- * Varaiables to be used to determain amount of messages recieved,
- * Length of the messages combined & the value of all messages combined -
- * including # as seperators
- */
- int messageCount = 0, textLength = 0, stringLength = 0;
- String combinedStrings = "";
- //Below code implemented to force virtual machine to display console
- System.out.print("Server Client");
- //Defining inbound location of packets being sent
- int port = 8000;
- //Add proper comment
- try {
- //Socket defined and instructed to seek port 8000 - where client will send messages
- DatagramSocket serverSocket = new DatagramSocket(port);
- InetAddress host = InetAddress.getByName("localhost");
- //Buffer created to reserve memory for incoming messages - anticipating large messages
- byte[] buffer = new byte[65507];
- while (messageCount <= 3) {
- //Datagram packet defined to be used as receiving point of packets - implements parameter buffer
- DatagramPacket serverPacket = new DatagramPacket(buffer, buffer.length);
- //Seeks and recieves any packets present in socket
- serverSocket.receive(serverPacket);
- //Converts byte information into String and stores it into variable
- String gotMail = new String(serverPacket.getData());
- //Cuts out excess information from allocated buffer memory - leaving only used information
- //within variable
- gotMail = gotMail.trim();
- //nLength repsonsible for storing length of each recieved message after excess buffer is trimmed
- textLength += gotMail.length();
- //For each iteration and message which is recieved - msgCount will increase by increment of 1
- //until while loop condition is met and all messages have been recieved
- messageCount += 1;
- //IF loop respondisble for observing amount of messages recieved and placing seperators within the
- //message. For the first 2 recieved messages '#' will be added as seperator
- if (messageCount <= 2) {
- gotMail += '#';
- }
- //combinedStrings holds value of message recieved from packet after '#' seperator has been appended
- combinedStrings += gotMail;
- if(messageCount == 3){
- break;
- }
- }
- } catch (Exception ex) {
- return;
- }
- //Debugging Process
- System.out.println("\n" + "Combined text:" + combinedStrings);
- System.out.println("\n" + "Length of text:" + textLength);
- serverResponse(combinedStrings, textLength);
- }
- //Respondsible for returning message to clientApplication
- public static void serverResponse(String returnMessage, int messageLength){
- int port = 7650;
- String messageLengthStr = String.valueOf(messageLength);
- //Add proper comment
- try {
- //severSocket defined once more into try / catch as - to be used as socket where messages are to be sent
- DatagramSocket responseSocket = new DatagramSocket();
- /**
- * Parameters used to define IP address where packets are to be sent to.
- * "localhost" can be substituted for IP address of client machine -
- * For troubleshooting purposes "localhost" is defined to use current machine
- * to send/recieve messages
- */
- InetAddress host = InetAddress.getByName("localhost");
- byte[] responseByte = returnMessage.getBytes();
- DatagramPacket responsePacket = new DatagramPacket(responseByte, responseByte.length,
- host,
- port
- );
- responseSocket.send(responsePacket);
- byte[] responseByteTwo = messageLengthStr.getBytes();
- DatagramPacket responsePacketTwo = new DatagramPacket(responseByteTwo, responseByteTwo.length,
- host,
- port
- );
- responseSocket.send(responsePacketTwo);
- }
- catch(Exception ex){return;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement