In: Computer Science
Turn the following code into a chat system that can send messages CONTINUOSLY without the the other side needing to a response. Use a send and receive thread to allow this.
Client Side:
package com.company;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        byte[] receiveBytes = new byte[256];
        final String TERMINATE = "bye";
        final int SERVER_PORT = 8080;
        System.out.println("Client started. Type a message and hit Enter key to send the message to server.");
        try (Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket();) {
            // Get server address
            final InetAddress SERVER_ADDRESS = InetAddress.getLocalHost();
            DatagramPacket dataPacket = null;
            while (!ds.isClosed()) {
                String input = sc.nextLine();
                // Terminate the client if user says "bye"
                if (input.trim().equalsIgnoreCase(TERMINATE)) {
                    break;
                }
                // Construct Datagram packet to send message
                dataPacket = new DatagramPacket(input.getBytes(), input.getBytes().length, SERVER_ADDRESS, SERVER_PORT);
                ds.send(dataPacket);
                // Construct Datagram packet to receive message
                dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                ds.receive(dataPacket);
                System.out.println("Server Says:" + new String(receiveBytes, "UTF-8"));
            }
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Server side:
package com.company;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;
import java.net.InetAddress;
public class Main {
    public static void main(String[] args) {
        InetAddress ip;
        String hostname;
        byte[] receiveBytes = new byte[256];
        final int SERVER_PORT = 8080;
        try (DatagramSocket ds = new DatagramSocket(SERVER_PORT); Scanner sc = new Scanner(System.in);) {
            ip = InetAddress.getLocalHost();
            hostname = ip.getHostName();
            System.out.println("Your current IP address : " + ip);
            System.out.println("Your current Hostname : " + hostname);
            System.out.println("Your current Port : " + SERVER_PORT);
            System.out.println("Server started. Waiting for client message...");
            while (!ds.isClosed()) {
                // Construct Datagram packet to receive message
                DatagramPacket dp = new DatagramPacket(receiveBytes, receiveBytes.length);
                ds.receive(dp);
                String dataString = new String(dp.getData(), "UTF-8");
                System.out.println("Client Says:" + dataString);
                String input = sc.nextLine();
                // Construct Datagram packet to send message
                DatagramPacket sendPacket = new DatagramPacket(input.getBytes(), input.getBytes().length,
                        dp.getAddress(), dp.getPort());
                ds.send(sendPacket);
            }
            ds.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//------------------ Client.java
//Client Side:
package com.company;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
//this class will act as a Receiver thread
class ClientReceiver extends Thread {
    public void run() {
        byte[] receiveBytes = new byte[256];
        final String TERMINATE = "bye";
        final int RECEIVE_PORT = 8081;
        
        System.out.println("ClientReceiver started.");
        
        try (Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket(RECEIVE_PORT);) {
            DatagramPacket dataPacket = null;
            
            while (!ds.isClosed()) {
                // Construct Datagram packet to receive message
                dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                ds.receive(dataPacket);
                System.out.println("\t\t--- Server Says:" + new String(dataPacket.getData(), "UTF-8").trim());
                receiveBytes = new byte[256]; //reset receiveBytes array to clear previous contents
            }
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//this class will act as a Sender thread
public class Client {
    public static void main(String[] args) {
        ClientReceiver cr = new ClientReceiver();
        cr.start(); //start the Receiver thread
        byte[] receiveBytes = new byte[256];
        final String TERMINATE = "bye";
        final int SERVER_PORT = 8080;
        
        System.out.println("Client started.\nType a message and hit Enter key to send the message to server.");
        
        try (Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket();) {
            // Get server address
            final InetAddress SERVER_ADDRESS = InetAddress.getLocalHost();
            DatagramPacket dataPacket = null;
            
            while (!ds.isClosed()) {
                String input = sc.nextLine();
                // Terminate the client if user says "bye"
                if (input.trim().equalsIgnoreCase(TERMINATE)) {
                                        ds.close();
                                        System.exit(0);
                }
                // Construct Datagram packet to send message
                dataPacket = new DatagramPacket(input.getBytes(), input.getBytes().length, SERVER_ADDRESS, SERVER_PORT);
                ds.send(dataPacket);
                System.out.println("Client sent: " + input);
            }
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//------------------ end of Client.java
//------------------ Server.java
//Server Side:
package com.company;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
//this class will act as a Receiver thread
class ServerReceiver extends Thread {
    public void run() {
        byte[] receiveBytes = new byte[256];
        final String TERMINATE = "bye";
        final int RECEIVE_PORT = 8080;
        
        System.out.println("ServerReceiver started.");
        
        try (Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket(RECEIVE_PORT);) {
            DatagramPacket dataPacket = null;
            
            while (!ds.isClosed()) {
                // Construct Datagram packet to receive message
                dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                ds.receive(dataPacket);
                System.out.println("\t\t--- Client Says:" + new String(dataPacket.getData(), "UTF-8").trim());
                receiveBytes = new byte[256]; //reset receiveBytes array to clear previous contents
            }
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//this class will act as a Sender thread
public class Server {
    public static void main(String[] args) {
        ServerReceiver obj = new ServerReceiver();
        obj.start(); //start the Receiver thread
        byte[] receiveBytes = new byte[256];
        final String TERMINATE = "bye";
        final int CLIENT_PORT = 8081;
        
        System.out.println("Server started.\nType a message and hit Enter key to send the message to server.");
        
        try (Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket();) {
            // Get server address
            final InetAddress CLIENT_ADDRESS = InetAddress.getLocalHost();
            DatagramPacket dataPacket = null;
            
            while (!ds.isClosed()) {
                String input = sc.nextLine();
                // Terminate the client if user says "bye"
                if (input.trim().equalsIgnoreCase(TERMINATE)) {
                    ds.close();
                                        System.exit(0);
                }
                // Construct Datagram packet to send message
                dataPacket = new DatagramPacket(input.getBytes(), input.getBytes().length, CLIENT_ADDRESS, CLIENT_PORT);
                ds.send(dataPacket);
                System.out.println("Server sent: " + input);
            }
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//------------------ end of Server.java
