I have implemented a websocket server using nanohttpd in java. I can access the websocket server from js in a web page. It works great.
However, now I'd like to create a java based client that will connect to the same server.
Does nanohttpd have a set of java classes to connect to the websocket server? In other words, the server is running in java but now I want a separate java client program to connect to it.
If so, what is the minimum java code to connect to the server?
If not, how would you suggest I connect to the websocket server in java?
There are plenty of third party Java Web Socket client implementations you can use. This is one of them.
Related
I have a client server application. The server is on .core 6.0 and WinForm clients connect to it via SignalR. I want to re-release the same server in Java only. Is it possible to configure a java websocket to behave like a SignalR server on dot.net so that I can connect to it from my Winform SignalR clients? And how to do it?
I tried to look in the Microsoft documentation https://learn.microsoft.com/en-us/aspnet/core/signalr/java-client?view=aspnetcore-7.0, but I understand that this is a solution how to organize a java client and not a server part
At the moment i have an android client app which connects to my java server through socket - serversocket. It sends and receive strings. The java server is connected to a mysql database (actually mariadb) using the jdbc driver.
I succeed to create a jbossas application and upload the code of the java server to openshift, but i didn't find any detailed tutorial on how do i connect to this new uploaded server from my socket client (This one (RMI or socket connection to Java Program on OpenShift) gives some tips but i'm still stucked).
More on this, how do i know that my server runs just fine on openshift and how do i control de calls to the database after i connect it (found this: $ rhc app create MyApp jbossas-7
$ rhc cartridge add mysql-5.5 -a MyApp), using org.mariadb.jdbc.Driver and java.sql is still working ?
Any small guide or tip is highly appreciated. I'm new to these things so please don't be too heavy on comments.
You can only make connections to your OpenShift server on http/https or ws/wss ports. If you want to connect to your java application and pull data from it from an android device, I would suggest using a RESTful api or a servlet, etc.
I had similar problem: My app server originally was running as a ServerSocket listener, and any clients/devices connect to it directly via Socket binding.
To deploy it into OpenShift, my previous initial solution was to change its host:port configuration by following the suggestion as described in this link [Socket connection to Java Program on OpenShift]. It worked nice as far as my app server was successfully up and running. But it did not work well with the port forwarding approach in order to accept remote requests.
So for the final solution, I modified the app server by wrapping my original code with a RESTful webservice around it, and deploy it as a web service.
This is very new thing for me.
I am just wondering If we could send the ProtoBuf serialized data from java application to Web application (javascript) and de-serialize there. I am using TCP/IP connection in websocket to connect java application to javascript.
I have been looking at
https://github.com/dcodeIO/ProtoBuf.js/
but they are using node.js ,which is not in my case.
Thank you
ProtoBuf.js also runs in the browser. Basically, all you have to do is to connect your client to your Java server using a (binary) WebSocket and to send ProtoBuf packets back and forth.
Here is an example that decodes a message on the client side.
I want to create an application which will connect to a file server and download a few video files. The server is a shared hosting Linux server.
I don't want code or anything like that, I just want to know whether this is possible and if so, what should I be researching. Should I be using java sockets? Or can Java sockets only connect to java based servers?
Should I be using java sockets?
Depends on the type of server you connect to. You can use an existing library which will abstract the interaction with the server for you (recommended) or implement the required protocol yourself (not recommended).
Can Java sockets only connect to java based servers?
Sockets in Java are just an interface to the native socket API of the OS you are on. Every program that connects to a server over the network has to use them, regardless of whether it is a C/C++/Python/Java/... application. So, to answer your question; no, "Java sockets" can connect to any server.
Read more about sockets in this Wikipedia article about sockets in general or this one about Berkeley sockets (the socket API implemented by most operating systems).
I had set up a system that had a Java program running on a server and a Java applet embedded in a page on a client's browser and the two communicating via Java sockets. I'm wondering if I can switch over from a Java applet to just HTML5 and javascript, using a WebSocket on the client side for communication with the Java socket on the server.
Is there a simple way to make a WebSocket communicate with a Java Socket?
Is there a simple way to make a WebSocket communicate with a Java Socket?
From what I understand, WebSocket works by the client side opening a port 80 connect to the server side, and sending a variant HTTP 1.1 request to the server to negotiate a WebSocket connection. If the server recognizes this, it will send a suitable response, and then allow the still open TCP connection to be used for full-duplex client-server interactions.
It looks like it would be possible to quickly put together a server-side that just understood WebSocket negotation and not full HTTP. However, I think you are better off looking at existing WebSocket implementations, including those embedded in HTTP servers / protocol stacks.
This Wikipedia page compares a number of WebSocket implementations, and should help you in deciding which server-side implementation to use.
But to directly answer your literal question, a WebSocket client can only connect to a WebSocket-aware server; i.e. that one that can perform the initial negotiation. (On the client side, you could implement starting from a bare Socket, but you would need to implement all of the "HTTP stuff" on top of that ... for the setup phase.)
Nope, you cannot communicate using regular sockets with client WebSockets.
WebSockets are special HTTP requests, with an upgrade in the HTTP Header, and a standard protocol to establish a connection (see the official RFC doc).