Question: Java: Client connecting to a server. Our professor had us download a chat server that is called ChatServer and is run on the cmd line
Java: Client connecting to a server.
Our professor had us download a chat server that is called ChatServer and is run on the cmd line with java ChatServer. It is supposed to be running on port 4688. When I run it, it says local port 4688 remote port 59443.
So, just to test connecting to the server, I used the example provided on Oracle (See below)
I'm trying to make sure they connect so I can do the other parts of the assignment, but I can't seem to get the args right. It doesn't recognize the Host. How do I know what hostname to use?
package application;
import java.io.*; import java.net.*;
public class ChatClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) { System.err.println( "Usage: java EchoClient
String hostName = args[0]; int portNumber = Integer.parseInt(args[1]);
try ( Socket echoSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream())); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)) ) { String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
