// Programmer: COSC 439/522, F '21 // Server program // File name: "TCPServer.java" // When you run this program, you must give the service port // number as a command line argument. For example, //...

1 answer below »
do the 4 tasks which given in the assignment in client and server. coding have to done source code is provided


// Programmer: COSC 439/522, F '21 // Server program // File name: "TCPServer.java" // When you run this program, you must give the service port // number as a command line argument. For example, // java TCPServer 22222 import java.io.*; import java.net.*; public class ntaTCPServer { private static ServerSocket servSock; public static void main(String[] args) { System.out.println("Opening port...\n"); try{ // Create a server object servSock = new ServerSocket(Integer.parseInt(args[0])); } catch(IOException e){ System.out.println("Unable to attach to port!"); System.exit(1); } do { run(); }while (true); } private static void run() { Socket link = null; try{ // Put the server into a waiting state link = servSock.accept(); // Set up input and output streams for socket BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter(link.getOutputStream(),true); // print local host name String host = InetAddress.getLocalHost().getHostName(); System.out.println("Client has estabished a connection to " + host); // Receive and process the incoming data int numMessages = 0; String message = in.readLine(); while (!message.equals("DONE")) { System.out.println(message); numMessages ++; message = in.readLine(); } // Send a report back and close the connection out.println("Server received " + numMessages + " messages"); } catch(IOException e){ e.printStackTrace(); } finally{ try{ System.out.println("!!!!! Closing connection... !!!!!\n" + "!!! Waiting for the next connection... !!!"); link.close(); } catch(IOException e){ System.out.println("Unable to disconnect!"); System.exit(1); } } } } // Programmer: COSC 439/522, F '21 // Client program // File name: TCPClient.java // When you run this program, you must give both the host name and // the service port number as command line arguments. For example, // java TCPClient localhost 22222 import java.io.*; import java.net.*; public class ntaTCPClient { private static InetAddress host; public static void main(String[] args) { try { // Get server IP-address host = InetAddress.getByName(args[0]); } catch(UnknownHostException e){ System.out.println("Host ID not found!"); System.exit(1); } run(Integer.parseInt(args[1])); } private static void run(int port) { Socket link = null; try{ // Establish a connection to the server link = new Socket(host,port); // Set up input and output streams for the connection BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Set up stream for keyboard entry BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in)); String message, response; // Get data from the user and send it to the server do{ System.out.print("Enter message: "); message = userEntry.readLine(); out.println(message); }while (!message.equals("DONE")); // Receive the final report and close the connection response = in.readLine(); System.out.println(response); } catch(IOException e){ e.printStackTrace(); } finally{ try{ System.out.println("\n!!!!! Closing connection... !!!!!"); link.close(); } catch(IOException e){ System.out.println("Unable to disconnect!"); System.exit(1); } } } }
Answered Same DaySep 23, 2021

Answer To: // Programmer: COSC 439/522, F '21 // Server program // File name: "TCPServer.java" // When you run...

Ramachandran answered on Sep 24 2021
133 Votes
Order-91887/nta_proj1/nta_TCPClient.java
Order-91887/nta_proj1/nta_TCPClient.java
// Programmer: COSC 439/522, F '21
// Client program
// File name: TCPClient.java
// When you run this program, you must give both the host name and
// the service port number as command line arguments. For example,
// java TCP
Client localhost 22222
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class nta_TCPClient {
    private static final String PORT_IDENTIFIER = "-p";
    private static final String HOST_IDENTIFIER = "-h";
    private static final String USER_NAME_IDENTIFIER = "-u";
    private static final int DEFAULT_SERVER_PORT = 22222;
    private static int serverPort = DEFAULT_SERVER_PORT;
    private static InetAddress serverHost;
    private static String userName;
    public static void main(String[] args) {
        try {
            parseArguments(args);
            while (!isValidUserName()) {
                System.out.println("Please enter a UserName : ");
                Scanner scanner = new Scanner(System.in);
                userName = scanner.next();
            }
        } catch (UnknownHostException e) {
            System.out.println("Host ID not found!");
            System.exit(1);
        } catch (Exception e) {
            System.out.println("Exception occurred while running the program " + e.getMessage());
            System.exit(1);
        }
        run(serverPort);
    }
    /**
     * Method to check if the user name is valid
     *
     * @return boolean true if username is valid, false otherwise
     */
    private static boolean isValidUserName() {
        return userName != null && !userName.isEmpty();
    }
    /**
     * Method to parse the command line arguments, initialize argument variable with default if not passed
     *
     * @param args : command line arguments
     * @throws UnknownHostException : exception indicating invalid host address
     */
    private static void parseArguments(String[] args) throws UnknownHostException {
        serverPort = DEFAULT_SERVER_PORT;
        serverHost = InetAddress.getLocalHost();
        for (int argIndex = 0; argIndex < args.length; argIndex += 2) {
            switch (args[argIndex]) {
                case USER_NAME_IDENTIFIER:
                    userName = args[argIndex + 1];
                    break;
                case PORT_IDENTIFIER:
                    serverPort = Integer.parseInt(args[argIndex + 1]);
                    break;
                case HOST_IDENTIFIER:
                    serverHost = InetAddress.getByName(args[argIndex + 1]);
                    break;
                default:
                    System.out.println("invalid command line param " + args[argIndex]);
            }
        }
    }
    private static void run(int port) {
        Socket link = null;
        try {
            // Establish a connection to the server
            l...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here