I've been working on a service with java tutorial, and was listening to it through localhost:8082. It was working just fine, but since I've turned off the computer it returns the error connection refused when I try to listen to it, and the only ports that present different responses are 8080 and 8081, which get the same error when I try to listen to the service through them.
Here's the code:
package io.vertx.book.message;
import io.vertx.core.json.JsonObject;
import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.eventbus.Message;
import rx.Single;
public class HelloConsumerMicroservice extends AbstractVerticle {
#Override
public void start() {
vertx.createHttpServer()
.requestHandler(
req -> {
Single<JsonObject> obs1 = vertx.eventBus()
.<JsonObject>rxSend("hello", "Luke")
.map(Message::body);
Single<JsonObject> obs2 = vertx.eventBus()
.<JsonObject>rxSend("hello", "Leia")
.map(Message::body);
Single
.zip(obs1, obs2, (luke, leia) ->
new JsonObject()
.put("Luke", luke.getString("message")
+ " from " + luke.getString("served-by"))
.put("Leia", leia.getString("message")
+ " from " + leia.getString("served-by"))
)
.subscribe(
x -> req.response().end(x.encodePrettily()),
t -> req.response().setStatusCode(500).end(t.getMessage())
);
})
.listen(8082);
}
}
That's the response I've got:
I don't know why it stopped working, but I've already browsed through a lot of answers and already tried cleaning my cache, but no improvements. Could anyone help?
I'm using Linux 18.04, Chrome/Firefox (both get connection refused).
As your using Ubuntu it might be your firewall not allowing connections to that port.
Open a terminal and type
sudo ufw allow 8082
Hopefully that helps
Somehow after shytting everything down and restarting the machine, everything came back to normal, so... Don't know what happened, but it's all right now... It might've been used by something else. Thanks anyways for the help!
Related
It's been days that I struggle to make a basic Client / server communication using websockets
My client is a java client with libGDX and my server is a basic java server
My main goal is to compile my client into HTML5 to communicate with my server using websockets.
I tryed the following solution when searching on google :
https://github.com/czyzby/gdx-lml/tree/master/websocket
https://github.com/pepedeab/libGDX-Net
https://github.com/TooTallNate/Java-WebSocket
The 1 seemed to be the best solution but, it doesn't seems to have the TCP_NODELAY socket setting (which is essential in my case)
The 2 seemed an other good solution too, But it relies on http://code.google.com/p/gwt-ws/(which at this time don't understand the point of this)
The 3 is what I choosed, a simple WebSocket Java API to let me write client and server really easily.
It worked very well for desktop and android, but when I tryed to html:dist, gradle give me error about websocket which was not inherit etc...
My main build.gradle file contains this line for each project (core, desktop, android, html) : compile "org.java-websocket:Java-WebSocket:1.3.7"
So to resume my primary question : How to correctly establish a websocket connection with a client compiled with GWT in ligdx, with in addition TCP_NODELAY?
My client is a very simple class :
package com.mygdx.game;
import java.net.URI;
import java.nio.ByteBuffer;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;
public class WebSocketsNet extends WebSocketClient {
public WebSocketsNet(URI serverUri, Draft draft) {
super(serverUri, draft);
}
public WebSocketsNet(URI serverURI) {
super(serverURI);
}
#Override
public void onOpen(ServerHandshake handshakedata) {
send("Hello, it is me. Mario :)");
System.out.println("new connection opened");
}
#Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("closed with exit code " + code + " additional info: " + reason);
}
#Override
public void onMessage(String message) {
System.out.println("received message: " + message);
}
#Override
public void onMessage(ByteBuffer message) {
System.out.println("received ByteBuffer");
}
#Override
public void onError(Exception ex) {
System.err.println("an error occurred:" + ex);
}}
According to https://bugs.webkit.org/show_bug.cgi?id=102079 and https://groups.google.com/forum/#!topic/native-client-discuss/T8zdrMjiTAE, found via https://github.com/websockets/ws/issues/791 and https://github.com/varspool/Wrench/pull/104, most browsers already use TCP_NODELAY. At least from the websocket standard, there is nothing you can do to influence this on the client - on the server there may be more options.
If TCP_NODELAY is already set on the client, you can set it on the server as well to ensure both sides are sending small messages as soon as possible.
Another thought that is suggested in questions like https://stackoverflow.com/a/13406438/860630 is to respond to every message sent right away, so that the network stack flushes all remaining messages as soon as possible.
Finaly I found a way to make it work, so I post a answer here for those interested.
I used https://github.com/czyzby/gdx-lml/tree/master/websocket
Particulary the example package, and rigorously follow everything that need to be added on build.gradle and on differents xml files, so now it work !
So to conclude :
Server Listening websockets with java-web-socket
LIBGDX client use gdx-websockets to connect to the server (Watch-out for different build gradle file and xml !!)
Hope to help some people who were in the same problem like me !
Can someone please tell me what I'm doing wrong?
I'm trying to put together a quick web service test just to see if I can get it to work for now.
The problem I'm having is that, as shown below, it does not work, but if I change the URL and replace "myWebsite.com" with "localhost" it works. So, I know the server side is working (I've still checked and double checked it though). But I'll need this to work through remote clients, and I just cannot get it to work.
Any help would be greatly appreciated.
package stickman.Server;
import java.net.*;
import javax.xml.namespace.*;
import javax.xml.ws.*;
import stickman.Combined.*;
public class TestApp {
public static void main(String[] args) throws Exception {
// --------------------------------------------
// changing "myWebite.com" to "localhost" works
URL url = new URL(
"http://myWebsite.com:32768/home/rhyan/workspace/Stickman/bin/stickman/Server");
// --------------------------------------------
QName qname = new QName("http://Server.stickman/","StickmanServerService");
Service service = Service.create(url, qname);
StickmanServerInterface ssi = service.getPort(StickmanServerInterface.class);
Account a = ssi.getAccount("This is a test");
System.out.println(a.getUserId());
}
}
Edit: here's the error tracking...
Exception in thread "main" com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
java.net.ConnectException: Connection refused
java.net.ConnectException: Connection refused
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:161)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.<init>(Service.java:76)
at javax.xml.ws.Service.create(Service.java:700)
at stickman.Server.ServerTestApp.main(ServerTestApp.java:17)
Could be a DNS problem? What IP address do you resolve myWebsite.com to?
Your exception indicates that the wsdl for the service is not accessible - not the service itself. Can you confirm that the wsdl for the service is available at this location - http://myWebsite.com:32768/home/rhyan/workspace/Stickman/bin/stickman/Server, and the url in the wsdl points to a proper working endpoint.
It took 8 days of reading a plethora of stuff online before I finally found the answer to my problem through a random google search I came up with: Publishing a WS with Jax-WS Endpoint
"localhost" should be 0.0.0.0, and I totally should have known this.
Thanks to the people who tried to help.
I have been working on this project where two modules on different machines need to be in communication through RMI.
I start both client and server modules on my laptop. RMI seems to work correctly when i am at work and connected to work network, but when i am home, connected to my home network it does not work. It says remote object could not be found.
Here is the method i use at CLIENT side to get the reference to remote object
public static MyRMIApp getRemoteApp() throws RemoteException, NotBoundException, AccessException {
Registry registry = LocateRegistry.getRegistry("localhost", 28999); // tried 127.0.0.1 instead of localhost here, still not working
MyRMIApp app = (MyRMIApp) registry.lookup("COM");
return app;
}
Digging up a bit with some debugging, when i check the object value returned from getRemoteApp method, it shows me the end point is 67.215.65.132. Which is openDNS i am using to connect to internet. Shouldn't that be 127.0.0.1 ?
Then i used my mobile internet and tried again. It seems to be working but end-point is not 127.0.0.1 again it is the address assigned to me, which is 192.168.x.x
So can anybody please tell me what is wrong i am doing here ? I really would appreciate the help.
Oh and this is the piece of code at SERVER side
//Somwhere up top
private final static MyRMIApp rmiApp = new RMIServer();
//Down below
MyRMIApp stub = (MyRMIApp) UnicastRemoteObject.exportObject(rmiApp, 0);
Registry registry = LocateRegistry.createRegistry(28999);
registry.rebind("COM", stub);
See item A.1 of the RMI FAQ: specifically, 'The appropriate workaround is to set the system property java.rmi.server.hostname when starting the server.'
I am trying to get started with WebSockets, and trying to write a simple application to send messages back and forth via a websoket.
However, it looks like the socket that I am trying to create never gets connected. Why can that be?
Below is the code of my WebSockets class. When .onConnect() is called, it logs:
I am socket, I was connected. Am i connected? - false
Update: in JavaScript, where I create the socket in question, the readyState is 1, which means "socket open, communication is possble".
import a.b.Misc; //writes logs.
import com.sun.grizzly.websockets.BaseServerWebSocket;
import com.sun.grizzly.websockets.DataFrame;
import com.sun.grizzly.websockets.WebSocketListener;
public class ChatWebSocket_v2 extends BaseServerWebSocket {
private String user;
public ChatWebSocket_v2(WebSocketListener... listeners) {
super(listeners);
}
public String getUser() {
if (user == null) {
Misc.print("User is null in ChatWebSocket");
throw new NullPointerException("+=The user is null in chat web socket");
}
return user;
}
public void setUser(String user) {
Misc.print("Just set user: " + user);
this.user = user;
}
#Override
public void onMessage(String message) {
Misc.print(message +"\n");
}
#Override
public void onMessage(byte[] message) {
Misc.print(new String(message) +" << Bytes\n");
}
#Override
public void onConnect() {
Misc.print("I am socket, i was connected. Am i connected? - " + this.isConnected());
}
#Override
public void onClose(DataFrame df) {
Misc.print("I am socket, i was closed");
}
}
If you're just trying to make a connection somewhere, you might want to try this instead. There is a live working demo and you can download the javascript code and play with it yourself. Note that the javascript code only works if you have it installed on a server (due to browser security because it's 'fancy'.) There is also a step by step browser-based client tutorial in the works that I will post as soon as it's ready. Most proxy servers haven't been upgraded to handle websockets so they will screw up connection request and most people won't be able to connect to websocket servers from work. Firefox 7 (release) or Google Chrome 14 or later support the latest version of the websocket protocol that the demo server runs.
If you want to try to get the grizzly demo working, you might have some debugging to do and maybe I'll help with that. Note that in comments below the article, other people said they couldn't get it working either and I haven't found any follow up. At this point it seems no better than the echo app above even if we do get it running and is possibly overly complicated and underly documented if you're just trying to get started. But if you want to try to get it running, you should 'git' the latest version of the code here, which was at least committed recently and may be fixed.
Then make sure that app.url in the application javascript file is set to your installation directory. His is hard-coded as:
url: 'ws://localhost:8080/grizzly-websockets-chat/chat',
If you're using Firefox 7, the javascript needs to be modified to use the Moz prefix, for example:
if (typeof MozWebSocket != "undefined") { // window.MozWebSocket or "MozWebSocket" in window
ok
} else if (window.WebSocket) { // he uses if ("WebSocket" in window)
ok
} else {
do your print "browser doesn't support websockets"
}
.... then if the browser supports websockets
websocket = new WebSocket(app.url); or
websocket = new MozWebSocket(app.url);
// depending on which it is.
The HLL websocket server demo code has this all sorted out.
(another) UPDATE: As I work through grizzly myself, I found on the Quick Start in the glassfish admin console, there's a hello sample that's pretty easy to set up and run. You'll find instructions there. The sample directory also contains a war file named: websocket-mozilla; so I guess its supposed to use websockets. Someone who's familiar with jsp should review the source code. All I can see is that it's using an http session. No mention of a websocket at all. It's a lot like the hello sample.
It's been working perfectly fine all day long, now suddenly I can't connect. I can connect through the browser (and so can you: http://secure.exoterragame.com/noxastra/login.php), but it won't work in my Java application. I get an UnknownHostException.
URL register = new URL("http://secure.exoterragame.com/noxastra/login.php" +
"?username=" + request.Username +
"&password=" + request.Password);
URLConnection conn = register.openConnection();
I am completely stumped. Does anyone know why this would happen?
(Yes, we'll be using https in the final version :P)
Do a packet sniff to see what's going on behind the scenes. (I recommend Wireshark for all platforms.)
I had a similar problem in Flash once and was tearing my hair until I realized it was a bug in the VM and it was giving me timeouts without ever sending any packets!
I don't think Java will have such a bug, but the point is, sniffing is sometimes invaluable.
Maybe there's a DNS/hosts-file update which your browser has gotten, but the program not (or the other way around?) Maybe the program's getting stuck on a (local) firewall? Maybe it does login successfully but breaks on a redirect response? (I don't even know if URL is capable of doing that, but still...) Maybe you've enabled/disabled IPv6 somewhere?
jcomeau#intrepid:/tmp$ cat test.java; javac test.java; java test
import java.net.*;
public class test {
public static void main(String args[]) throws Exception {
URL register = new URL(
"http://secure.exoterragame.com/noxastra/login.php" +
"?username=" + "guest" +
"&password=" + "guest");
URLConnection conn = register.openConnection();
}
}
Doesn't throw an exception for me. Perhaps https://stackoverflow.com/users/800237/adithya-surampudi 's suggestion will work.