javasocketsportircecho-server

Simple Java IRC Client


I am trying to write an IRC client that is very simple, with the hopes of later expanding it.

At this point I have two classes written in java that are supposed to work together and were copied from the Oracle tutorial. What I am trying to do is have the EchoClient connect to a host on a certain port so that the host running EchoServer can print out what the client types. I am trying to do exactly what the tutorial says that it does, but I am getting an error after copying and pasting the code.

EchoClient.java:

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.err.println(
            "Usage: java EchoClient <host name> <port number>");
        System.exit(1);
    }

    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);
    }
}

}

EchoServer.java:

import java.net.*;
import java.io.*;

public class EchoServer {
  public static void main(String[] args) throws IOException {

    if (args.length != 1) {
        System.err.println("Usage: java EchoServer <port number>");
        System.exit(1);
    }

    int portNumber = Integer.parseInt(args[0]);

    try (
        ServerSocket serverSocket =
            new ServerSocket(Integer.parseInt(args[0]));
        Socket clientSocket = serverSocket.accept();
        PrintWriter out =
            new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
    ) {
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            out.println(inputLine);
        }
    } catch (IOException e) {
        System.out.println("Exception caught when trying to listen on port "
            + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}

}

When I try to run the compiled EchoServer from my terminal with java EchoServer 2000 I get the error, Error: Could not find or load main class EchoClient and I get the same error from java EchoServer 2000


Solution

  • The Echo server example shows you how to do this. First set up an output stream of some sort, using the socket that the client creates:

    Socket echoSocket = new Socket(hostName, portNumber);
    PrintWriter out =
        new PrintWriter(echoSocket.getOutputStream(), true);
    

    Then when you have a message to send, write the result to the out object with println().

    String userInput;
    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        //... 
    }
    

    You can find these lines in the code listing on the tutorial page: https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

    This isn't the only way of writing to a socket, but that's how the example does it and you should probably stick with that for now.