0
votes

Playing with a few tutorials on sockets but struggling to connect.

My tomcat server is at eric.server.com and is running a ajp connector on port 8052.

I want to use the knock knock java tutorial to connect. I've uploaded the KnockKnockProtocol.class and KnockKnockServer.class to eric.server.com/apps/knock

On the tutorial it shows this:

kkSocket = new Socket("tarranis", 4444);

Which I have changed to:

kkSocket = new Socket("eric.server.com/apps/knock", 8052);

I then run the client program from eclipse but I just get the UnknownHostException.

Could someone please explain what I'm doing wrong, totally new to Tomcat and servlets in general?

TIA

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

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

    Socket kkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        kkSocket = new Socket("eric.server.com", 8052);
        out = new PrintWriter(kkSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to.");
        System.exit(1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while ((fromServer = in.readLine()) != null) {
        System.out.println("Server: " + fromServer);
        if (fromServer.equals("Bye."))
            break;

        fromUser = stdIn.readLine();
    if (fromUser != null) {
            System.out.println("Client: " + fromUser);
            out.println(fromUser);
    }
    }

    out.close();
    in.close();
    stdIn.close();
    kkSocket.close();
}

}

1

1 Answers

2
votes

You are using a host name of "eric.server.com/apps/knock", when what you really want is "eric.server.com".

Then once you have connected, you can then start engaging in the socket's protocol (ajp) to communicate with it. The URI portion goes in the req_uri portion of the protocol, not in the host.

But the bigger issue is that Tomcat is really an HTTP server, not a socket server. You should either write a servlet that implements the server portion of the tutorial and run that in Tomcat, or just write it as a standalone server process.