I have the problem that I get a 504 if I try to access the Jetty Server on my private server. The exact message is "ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.".
I create the server with
ContextHandler context = new ContextHandler("/");
SessionHandler sessions = new SessionHandler(new HashSessionManager());
sessions.setHandler(new WebHandler(this));
context.setHandler(sessions);
// Setup the server
final Server server = new Server(getConfig().getInt("server.port"));
server.setSessionIdManager(new HashSessionIdManager());
server.setHandler(sessions);
server.setStopAtShutdown(true);
// Start listening
getProxy().getScheduler().runAsync(this, new Runnable() {
#Override
public void run() {
try {
server.start();
getLogger().warning("Webserver started on URI: " + server.getURI() + ", state: " + server.getState());
} catch (Exception e) {
getLogger().warning("Unable to bind web server to port.");
e.printStackTrace();
}
}
});
If I start the server on localhost all things work well, but if I put it on my private Hetzner server (Debian 10) I get the error I've written above. The firewall is set to accept all ports. If I create a tunnel to my server via ssh the site loads infinitely long without showing a result.
I hope you can help me :)
Edit: I saw that he tried to connect via IPv6. I forced it to use IPv4, but same result. Here's the netstat output:
tcp6 0 0 :::8092 :::* LISTEN 4581/java
Related
I'm running a Docker container (GPC Spanner) and attempting to create an instance using org.testcontainers.containers (from a Junit BeforeClass). The connection is refused.
io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9010
Cause: java.net.ConnectException: Connection refused
Code:
private static final Integer[] SPANNER_EMULATOR_PORTS = {9010, 9020};
private static final WaitStrategy WAIT_FOR_START_LOG_MESSAGE =
Wait.forLogMessage(".*gRPC server listening.*", 1).withStartupTimeout(Duration.ofSeconds(30));
...
final GenericContainer<?> container =
new GenericContainer<>(DockerImageName.parse(emulatorDockerImage))
.withExposedPorts(SPANNER_EMULATOR_PORTS)
.waitingFor(
new WaitStrategy() {
#Override
public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) {
// do not wait on #start call, so that we can hook up logger and print output
// for errors which we can do only after #start is invoked
}
#Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
return this;
}
});
final StringBuilder containerOutput = new StringBuilder();
final long startTime = System.currentTimeMillis();
try {
System.out.println("Running Spanner Emulator Container");
container.start();
// TODO: See if there's a way to print the output as it's happening and not on timeout
recordOutput(containerOutput, startTime, "Container initialized\n");
} catch (Throwable ex) {
throw new RuntimeException(
"Failed to start up Spanner Emulator",
ex);
}
container.followOutput(
outputFrame -> recordOutput(containerOutput, startTime, outputFrame.getUtf8String()));
try {
WAIT_FOR_START_LOG_MESSAGE.waitUntilReady(container);
// we print only errors in the tests
System.err.println(
"It took "
+ (System.currentTimeMillis() - startTime) / 1000
+ " seconds to init Spanner");
} catch (Exception e) {
recordOutput(containerOutput, startTime, "Explicit timeout");
throw new RuntimeException("Spanner initialization timeout\n" + containerOutput, e);
}
SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer(container);
spannerEmulatorContainer.createSpannerClient(projectId);
return spannerEmulatorContainer;
...
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
// Create a instance
InstanceInfo instanceInfo =
InstanceInfo.newBuilder(
InstanceId.of(TestConstants.testProjectId, TestConstants.testInstanceId))
.setInstanceConfigId(
InstanceConfigId.of(TestConstants.testProjectId, TestConstants.testRegion))
.setNodeCount(1)
.setDisplayName(TestConstants.testInstanceId)
.build();
// ***** Errors out here ******
OperationFuture<Instance, CreateInstanceMetadata> instanceOperation =
instanceAdminClient.createInstance(instanceInfo);
Std Out:
2022-10-09T01:35:40.244-0700 [DEBUG] [TestEventLogger] It took 3 seconds to init ...
io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0d09b8a15f4 gcr.io/cloud-spanner-emulator/emulator:1.4.6 "./gateway_main --ho…" 9 minutes ago Up 9 minutes 0.0.0.0:55082->9010/tcp, 0.0.0.0:55081->9020/tcp sleepy_poitras
b1315b3c091e testcontainers/ryuk:0.3.0 "/app" 9 minutes ago Up 9 minutes 0.0.0.0:55080->8080/tcp testcontainers-ryuk-c0389f48-9da7-4731-9dc9-f5f03fc050e0
docker logs a0d09b8a15f4
WARNING: proto: file "google/rpc/status.proto" is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: file "google/rpc/status.proto" has a name conflict over google.rpc.Status
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: message google.rpc.Status is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
2022/10/09 08:37:50 gateway.go:140: Cloud Spanner emulator running.
2022/10/09 08:37:50 gateway.go:141: REST server listening at 0.0.0.0:9020
2022/10/09 08:37:50 gateway.go:142: gRPC server listening at 0.0.0.0:9010
docker logs b1315b3c091e
2022/10/09 08:37:49 Pinging Docker...
2022/10/09 08:37:49 Docker daemon is available!
2022/10/09 08:37:49 Starting on port 8080...
2022/10/09 08:37:49 Started!
2022/10/09 08:37:49 Connected
2022/10/09 08:37:49 Adding {"label":{"org.testcontainers.sessionId=c0389f48-9da7-4731-9dc9-f5f03fc050e0":true,"org.testcontainers=true":true}}
The problem was that I wasn't using the randomized port when making the connection from the test. The actual exposed port is different from the one it's listening to in the container.
String address = container.getHost();
Integer port = container.getFirstMappedPort();
I creating a gRPC server but everything seems to run okay but the server never starts up on the specifies port and application is throwing no errors. But when I test with telnet on that specific port, I get this from terminal
isaack$ telnet localhost 9000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
Below is my code to create the server (NB: All the services are generated okay with proto and the generated code has no errors)
import java.io.File;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptors;
import io.grpc.ServerServiceDefinition;
public class EmployeeServiceServer {
private Server server;
public static void main(String[] args) {
try {
EmployeeServiceServer service = new EmployeeServiceServer();
service.start();
} catch (Exception e) {
System.err.println(e);
}
}
private void start() throws InterruptedException {
File certificate = new File("/Users/i/certificates/cert.pem");
File key = new File("/Users/i/certificates/key.pem");
final int port = 9000;
EmployeeService employeeService = new EmployeeService();
ServerServiceDefinition serverServiceDefinition = ServerInterceptors.interceptForward(employeeService,
new HeaderServerInterceptor());
server = ServerBuilder.forPort(port).useTransportSecurity(certificate, key).addService(serverServiceDefinition)
.build();
System.out.println("Listening on Port " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("Shuttin Down Server");
EmployeeServiceServer.this.stop();
}
});
server.awaitTermination();
}
private void stop() {
if (server != null) {
server.isShutdown();
}
}
}
Below is the log from but when I ping it, I get nothing.
Listening on Port 9000
My client is throwing this error as well:
Exception in thread "main" io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)
at com.base.services.EmployeeServiceGrpc$EmployeeServiceBlockingStub.getBadgebyNumber(EmployeeServiceGrpc.java:373)
at com.base.client.Client.sendMetaData(Client.java:66)
at com.base.client.Client.main(Client.java:37)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9000
You may want to start() your server, as build suggests:
Builds a server using the given parameters.
The returned service will not been started or be bound a port. You will need to start it with Server.start().
Perhaps that server.awaitTermination(); line could become
server.start().awaitTermination();
though I am not entirely sure.
I have faced the same error. The reason being grpc server didn't start.
Server server = ServerBuilder.forPort(port).useTransportSecurity(certificate, key).addService(serverServiceDefinition)
.build().start();
I had written start method as chain to build.
To make it work i had to call start() method separately.
server.start();
This solved the error for me.
PS: I'm writing this answer as the above solution didn't clarify much and had to research alot before finding the solution. Hope this will be helpful for other developers.
I work on a java application.
I got a java socket server mapped with a #ServerEndpoint("/wsock")
Form my javascript code I access the WebSocket from this URL :
ws://192.9.200.73:8084/socketserver/wsock
I want now access to this socket from my java code. But how can I specify the address "socketserver/wsock" ? I've tried something but I got every time an error message.
This is my test :
Socket s = new Socket("localhost/socketserver/wsock", 8084);
But it doesn't work, I got everytime an error message: ".UnknownHostException: localhost/socketserver/wsock"
Any idea?
Thank's
public static boolean pingHost(String host, int port, int timeout) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), timeout);
return true;
} catch (IOException e) {
return false; // Either timeout or unreachable or failed DNS lookup.
}
}
Try this. Its something like ping. If you get true its connected. But your server should be ready.
You use a Websocket client. You can't use a Socket directly for this. There is a superimposed protocol.
I'm using Netty to build a client-server network communication. Is it possible to find out to which app a client has connected to in case of success?
It's the following problem I try to solve: If a Tomcat Server is listening to port 8080 my client app successfully connects to the "server". Looks like it doesn't matter who is listening to the port.
How can I find out if my server app is currently started and listening to the port instead of e.g. Tomcat?
This is the connection code of the client:
public void run(){
//disconnectTest();
createBootstrap( new Bootstrap(), new NioEventLoopGroup(), true);
}
public void createBootstrap( Bootstrap b, EventLoopGroup eventLoop, boolean initialAttempt){
mWorkerGroup = eventLoop;
try {
b.group(mWorkerGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.handler(new ClientChannelInitializer());
logger.info("Connecting client...");
b.connect(mHost, mPort)
.addListener( new ConnectionListener(this, initialAttempt));
} catch (Exception e) {
logger.error("Failed to connect client to server '" +mHost +": " +mPort +". Error: ", e);
}
}
Snippet from the ConnectionListener:
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
System.out.println("success");
}else{
System.out.println("error");
}
}
EDIT:
If you want check the availability of the server withing the client App, you can use certain tools that Java7 can give us, for example, using this code:
private static boolean available(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return false;
} catch (IOException ignored) {
return true;
}
}
This does not have to be a specific function Netty. More info here:
Sockets: Discover port availability using Java
Enjoy.
To check it outside you client app:
To test the server status I use the Hercules software client.If you know that server will respond someting, using hercules you can send a dummy data y wait the server response.
How you can see, Hercules, allows too makes a ping to the server :)
Hope it helps.
I have been trying to get a simple networking test program to run with no results.
Server:
import java.io.*;
import java.net.*;
public class ServerTest {
public static void main(String[] args) {
final int PORT_NUMBER = 44827;
while(true) {
try {
//Listen on port
ServerSocket serverSock = new ServerSocket(PORT_NUMBER);
System.out.println("Listening...");
//Get connection
Socket clientSock = serverSock.accept();
System.out.println("Connected client");
//Get input
BufferedReader br = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
System.out.println(br.readLine());
br.close();
serverSock.close();
clientSock.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
Client:
import java.io.*;
import java.net.*;
public class ClientTest {
public static void main(String[] args) throws IOException {
final int PORT_NUMBER = 44827;
final String HOSTNAME = "xx.xx.xx.xx";
//Attempt to connect
try {
Socket sock = new Socket(HOSTNAME, PORT_NUMBER);
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
//Output
out.println("Test");
out.flush();
out.close();
sock.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
The program works just fine when I use 127.0.0.1 or my internal IP for the hostname. But whenever I switch to my external IP address, it throws a java.net.ConnectException: Connection refused: connect error.
I purposely picked such an uncommon port to see if that was the problem, with no luck.
I can connect with no problems using telnet, but when I try to access the port with canyouseeme.org, it tells me the connection timed out.
I even tried to disable all firewalls and antivirus including the Windows default ones and the router firewall, with all ports forwarded and DMZ enabled, and it still says that the connection timed out. I use Comcast as my ISP, and I doubt that they block such a random port.
When I use a packet tracer, it shows TCP traffic with my computer sending SYN and receiving RST/ACK, so it looks like a standard blocked port, and no other suspicious packet traffic was going on.
I have no idea what is going on at this point; I have pretty much tried every trick I know. If anyone know why the port might be blocked, or at least some way to make the program work, it would be very helpful.
These problem comes under the following situations:
Client and Server, either or both of them are not in network.
Server is not running.
Server is running but not listening on port, client is trying to connect.
Firewall is not permitted for host-port combination.
Host Port combination is incorrect.
Incorrect protocol in Connecting String.
How to solve the problem:
First you ping destination server. If that is pinging properly,
then the client and server are both in network.
Try connected to server host and port using telnet. If you are
able to connect with it, then you're making some mistakes in the client code.
For what it's worth, your code works fine on my system.
I hate to say it, but it sounds like a firewall issue (which I know you've already triple-checked) or a Comcast issue, which is more possible than you might think. I'd test your ISP.
Likely the server socket is only being bound to the localhost address. You can bind it to a specific IP address using the 3-argument form of the constructor.
I assume you are using a Router to connect to Internet. You should do Port Forwarding to let public access your internal network. Have a look at How do you get Java sockets working with public IPs?
I have also written a blog post about Port forwarding, you might wanna have a look :) http://happycoders.wordpress.com/2010/10/03/how-to-setup-a-web-server-by-yourself/
But I still couldn't get this accessed over public IP, working on it now...
I had the same problem because sometimes the client started before server and, when he tried to set up the connection, it couldn't find a running server.
My first (not so elegant) solution was to stop the client for a while using the sleep method:
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
I use this code just before the client connection, in your example, just before Socket sock = new Socket(HOSTNAME, PORT_NUMBER);
My second solution was based on this answer. Basically I created a method in the client class, this method tries to connect to the server and, if the connection fails, it waits two seconds before retry.
This is my method:
private Socket createClientSocket(String clientName, int port){
boolean scanning = true;
Socket socket = null;
int numberOfTry = 0;
while (scanning && numberOfTry < 10){
numberOfTry++;
try {
socket = new Socket(clientName, port);
scanning = false;
} catch (IOException e) {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
return socket;
}
As you can see this method tries to create a socket for ten times, then returns a null value for socket, so be carefull and check the result.
Your code should become:
Socket sock = createClientSocket(HOSTNAME, PORT_NUMBER);
if(null == sock){ //log error... }
This solution helped me, I hope it helps you as well. ;-)