1

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

brothman01
  • 157
  • 1
  • 2
  • 12
  • 2
    Port 80 (and all other port numbers less than 1024) are privileged ports and need elevated permissions to bind (listen) to. Use another port number. (Eventually if you need port 80 you can use admin permissions to give yourself more permissions. But everyone runs their tests on other ports during development.) – markspace Oct 20 '18 at 14:20
  • @markspace Yes, I switched to port 2000 and now everything runs the way it should but it does not function the way it needs to. I changed my question up top so please read that and I would appreciate any answers you can offer to my current question. – brothman01 Oct 20 '18 at 15:09
  • @markspace ok I fixed that issue (it was with my classpath)and I added a System.out.println line and now everything works the way it is supposed to, thanks! – brothman01 Oct 20 '18 at 23:56

2 Answers2

0

If you are running your application from command prompt - try first opening cmd.exe as Administrator.

Type in windows start menu: cmd, Then right click on cmd.exe and choose "Run as Administrator".

Then in command prompt that opens execute

java knockclient 127.0.0.1 80 test

as you normally would.

PKey
  • 3,715
  • 1
  • 14
  • 39
  • I am running on a mac so I used `sudo` instead and you were right that gave permission for the program to run. But now I see that I am doing other things wrong, so I changed my question up top. I would really appreciate if you read it and any answers you have to my current question. – brothman01 Oct 20 '18 at 15:12
0

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.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • You have been so helpful, I feel a little bad asking you another question, lol that code seems to do exactly what I want, but I copied and pasted the code from that tutorial (changing nothing) and I am getting an error. The code and the error is all laid out above. I bet if we can fix that error that will be the end of this issue. thanks for your help so far! – brothman01 Oct 20 '18 at 18:33
  • @brothman regarding your new error check out this SO post https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – PKey Oct 20 '18 at 18:56
  • @Plirkee haha my mistake, it was a classpath issue. Anyway, I added a System.out.println line to it and now it works, thx :) – brothman01 Oct 20 '18 at 23:55