Right format to connect to remote tomcat server? - java

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

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.

Related

Server Client Communication via Internet (Java)

I have written a simple Java TCP Server and a Client (See below).
The idea is quite simple: the Client sends a message to the Server the Server reads it, modify it and sends it back to the client.
import java.io.*;
import java.net.*;
public class ServerTest2 {
public static void main(String[] argv) {
try {
ServerSocket serverSocket = new ServerSocket(2000); // Create Socket and bind to port 2000
System.out.println("Created");
Socket clientSocket = serverSocket.accept(); //Wait for client and if possible accept
System.out.println("Connection accepted");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); // for outputs
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // for inputs
String request; // requst/input of client
String answer; // the answer for the client
System.out.println("Start Waiting");
request = br.readLine(); //Wait for input from client
answer = "answer to "+request;
bw.write(answer); // Send answer to client
System.out.println("send");
bw.newLine();
bw.flush();
//Shut everything down
bw.close();
br.close();
clientSocket.close();
serverSocket.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
The Client Implementation
import java.io.*;
import java.net.*;
public class ClientTest2 {
public static void main(String[] argv) {
try {
String host = "185.75.149.8"; //public ip of router
Socket clientSocket = new Socket(host,2000); //Create and connect Socket to the host on port 2000
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); // for outputs
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // for inputs
String answer;
String request = "HelloWorld";
bw.write(request); //Write to server
bw.newLine();
bw.flush();
System.out.println("Waiting");
answer = br.readLine(); //Wait for answer
System.out.println("Host = "+host);
System.out.println("Echo = "+answer);
//Shut eveything down
bw.close();
br.close();
clientSocket.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
It works perfectly on my local network.
Now I want to use it via the Internet so I installed Port Forwarding on Port 2000 in my Router which sends it to Port 2000 of my PC.
My PC is directly connected to my Router without any Subnets in between.
The Problem is that the Server does not accept the connection(Stops at serverSocket.accept()).
It does not throw an Exception it just waits forever.
The Client does also not throw an Exception (If the Port isn't open it would throw a Connection refused Exception)
This means that the Port Forwarding should work (I have also tested whether the port is open with a Webtool (its open)).
But strangely the Client stops waiting after about 10 seconds and continues with the program.
Since the Port Forwarding should work and my Code works fine in my local Network I absolutely don't know how or where I could find the problem.
I appreciate any help.
Thank you very much!

Server and Client Interactions

Hello programmers on the internet. I am currently stepping through an operating systems book and there are some exercises that involve the following pieces of code.
This is the server code
import java.net.*;
import java.io.*;
public class DateServer{
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(6013);
// now listen for connections
while (true) {
Socket client = sock.accept();
PrintWriter pout = new
PrintWriter(client.getOutputStream(), true);
// write the Date to the socket
pout.println(new java.util.Date().toString());
// close the socket and resume
// listening for connections
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
This is the client code
import java.net.*;
import java.io.*;
public class DateClient{
public static void main(String[] args) {
try {
//make connection to server socket
Socket sock = new Socket("127.0.0.1",6013);
InputStream in = sock.getInputStream();
BufferedReader bin = new
BufferedReader(new InputStreamReader(in));
// read the date from the socket
String line;
while ( (line = bin.readLine()) != null)
System.out.println(line);
// close the socket connection
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
So to my understanding the server is creating a socket and writing a date value to it. The client is then coming a long and connecting to the server and writing out the value in that socket. Am I interpreting this code correctly? This is my first experience with sockets.
Now for my actual question. I want to have the client connect to the server (and print out a message saying you are connected) and then be able to send a value over to the server so that the server can process it. How would I go about doing this? I have tried tinkering with DataOutputStream and DataInputStream but I have never used either before. Any help would be GREATLY appreciated.
You are correct. You have the server writing to the socket and the client reading from the socket. You want to reverse that.
Server Should look like:
ServerSocket sock = new ServerSocket(6013);
// now listen for connections
while (true)
{
Socket client = sock.accept();
InputStream in = client.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
// read the date from the client socket
String line;
while ((line = bin.readLine()) != null)
System.out.println(line);
// close the socket connection
client.close();
}
The client should look like:
try
{
// make connection to server socket
Socket sock = new Socket("127.0.0.1", 6013);
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
// send a date to the server
out.println("1985");
sock.close();
}
catch (IOException ioe)
{
System.err.println(ioe);
}

Java sockets send and receiving data

Hi I am trying to understand how Sockets work can implement a multiplayer side to a monopoly game.I understood how to create the connection, but now it seems I have trouble sending and receiving the data between the client and the server .Here is my code:
Client code:
public class EchoClient
{
public static void main(String[] args)
{
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
echoSocket = new Socket("127.0.0.1", 5000);;
out = new PrintWriter(echoSocket.getOutputStream());
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);
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: taranis");
}
catch (IOException e)
{
System.err.println("Couldent get I/O for "
+ " the connection to : taranis.");
}
}
}
Server code:
public class ServerSide
{
ServerSocket connect;
Socket connection;
PrintWriter out;
BufferedReader in;
public void go()
{
try
{
connect = new ServerSocket(5000);
connection = connect.accept();
in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String userInput;
while ((userInput = in.readLine()) != null)
{
System.out.println("echo: " + in.readLine());
}
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
new ServerSide().go();
}
}
I was trying to create here a simple connection between a client and a server.On the client side when the user inputs data I want it to be sent to the server and then print it it on the server console.It seems that the way I wright the code it isent working what did I do wrong?
First of all you need to declare a console object and then print on it.
Then don't forget to:
After every print on the console at the server side you need to flush the stream so the all the data will be printed.
Your code looks fine to me. Usually with sockets and keyboard input, you run into the case where the reader.readLine() hangs because it is still trying to read input from the other side. Typically, i will put an empty out.println() at the end of my client so the server will terminate the reading while loop. I've tried flush() before as Mike suggested but that seems to not work for me.
On the client, you're missing out.flush; after the out.println.
On the server,
while ((userInput = in.readLine()) != null) {
System.out.println("echo: " + userInput); // not in.readLine
}

Using sockets in Java with a proxy

I'm writing a very simple transport simulation (please don't ask why I use this approach, it's not really the point of my question).
I have three threads running (although you could consider them as seperate programs). One as a client, one as a server, and one as a proxy.
The first is used as the client, and the main code for that is given here:
try {
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getLocalHost(), 4466));
Socket socket = new Socket(proxy);
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 4456);
socket.connect(socketAddress);
// send data
for (String straat : straten) {
socket.getOutputStream().write(straat.getBytes());
}
socket.getOutputStream().flush();
socket.getOutputStream().close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
The second is the server side, given here:
public void run() {
try {
ServerSocket ss = new ServerSocket(4456);
Socket s = ss.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String incoming;
while ((incoming = in.readLine()) != null) {
panel.append(incoming + "\n");
}
panel.append("\n");
s.getInputStream().close();
s.close();
ss.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
And then there's the proxy-thread:
public void run() {
try {
ServerSocket ss = new ServerSocket(4466);
Socket s = ss.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
panel.append(verzenderId + "\n");
String incoming;
while ((incoming = in.readLine()) != null) {
panel.append(incoming + "\n");
}
panel.append("\n");
s.getInputStream().close();
s.close();
ss.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Somehow, this won't work. The message is sent directly to the server, and the proxy doesn't receive any socket request.
How can I make this work so that port 4466 becomes a proxy for the communication between the client-thread and the server-thread?
The goal is to make this socket between the client and the server to become an SSLSocket, so that the proxy can't read anything that goes over it. Therefore, setting up two sockets, one between the client and the proxy and one between the proxy and the server, is not the solution I'm looking for.
Thanks a lot in advance.

question regarding Echoclient program

I am always getting the message Don't know about host: taranis. while running echoclient program. here is the program below
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis",3218);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
You need to use a valid host name, or a valid IP of your server (assuming you have one) when you initialize your socket (new Socket("taranis",3218) ). It is great to take those tutorials (as pointed by icktoofay), but especially when it comes to networking, you have to make sure you have the matching application running on the other side, and that the parameters match it. IP and port usually change from machine to machine and from application to application.

Categories