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).
Related
I'm developing a Client application which talks to a Server using WebSockets. The Client is in C++ and the Server is in Java.
Can anyone suggest me any library which I can use on both Client side and Server side for communication using web sockets.
I never had experience with WebSocket, but try library cURL (libcurl). It was easiest for me to write clients for HTTP and FTP, using it. It have to help (but curl is useful just for clients, not for server).
If you are talking about sockets, normal sockets that connect on a port and wait for a connection on the server side and that connect to a given address on the client side, then I would recommend the boost asio socket on the c++ side and the standard java socket on the java side.
Just remind yourself of making sure that you transmit the datatype you expect.
Another cool implementation for both, java and c++, is ZeroMQ. I would recommend to take a look at it because it is easy to use and has implemented some really cool communication patterns.
I want to write a program in Netty4 that should act as a server to other clients and also it itself is a client to another server. How to do this in Netty4? So far all examples I have seen are either client or server. Thanks.
There are no special difficulties here. You need to create a part that will act as a server (using ServerBootstrap), and a part that will act as a client (using Bootstrap).
If you need to establish a connection to another server while handling incoming connection from a client, you can place that logic into a ChannelHandler of the server's pipeline.
Netty provides two examples of this approach:
Hex dumping proxy
SOCKS proxy
I want to create a web utility which will use a local port for sending a TCP/IP request.
Does it possible to use a client side port to send a TCP/IP request?
I know it is possible if we send a TCP/IP request from a web server and the specific port is allowed on server. But I want to send the request using the client side port.
What would I need to do? Should I create a Java Applet/Plugin or is it possible using PHP/.net?
It depends what you want to do, but fundamentally you can't initiate operations on the client using server side code like PHP.
Javascript is capable of some networking operations using XHR (Ajax) and WebSockets, and wrapping libraries such as socket.io. Perhaps that will suffice for your needs.
A Java Applet will be more capable, but will be restricted in what it can do unless you sign the code. And of course will be slower to start up for the client.
i have the problem when i want to try web socket technology on my application.
but i want to ask about requiretment of web socket.
1) is posible i'm using Server with javascript language ex is socket.io
var io = require('socket.io').listen(3001);
io.sockets.on('connection', function (socket) {
socket.emit('news', {hello: 'can you hear me'});
});
and the client using Java language example is socket.io.java
i'm using this method to get message from server
public void onMessage(JSONObject json, IOAcknowledge ack) {
Log.d("We received a message: " , json.toString());
}
with my socket server is
SocketIO socket = new SocketIO("http://127.0.0.1:3001/news");
2) is support to all machine? or browser?i'm newbie on web socket programing, please help me
thanks for your answer...
Yes, you can build the server in any language you want, but web sockets are not a simple socket, they are sockets that "follow" a specific protocol, so make sure you read the specification of this protocol and implement it on your server so any browser will be able to stablish a connection to it.
And you can also write a client in any language, but if you're not using javascript then I don't see why you would use web sockets instead of using just a simple socket. But if you want to, just take a look at what data a browser sends to a web socket server and emulate it from your own client.
To elaborate on Delta's answer, "websockets" is a variation of the HTTP protocol where the client sends an HTTP request message, the server sends an HTTP response, and then the client and server use the still-open TCP/IP connection to do "other things". The request and response contain special headers which allow the client and server to agree to use the connection in this way.
In order for this to work, the client and server both need to understand at least a subset of the HTTP protocol in order to do the initial "handshake". Hence you can't simply use a websocket client to talk to a plain socket server ... or vice versa. (If you try to do that, both ends will see unexpected stuff / protocol errors. And the websocket end should promptly close its end of the TCP/IP connection.)
Having said that, a websocket client and a websocket server can be implemented in just about any modern programming language. (And the same goes for plain socket clients and servers.)
Sockets are an operating system thing. You can use them with any language providing the relevant interface or glue code to the operating system calls implementing them (e.g. on Linux: socket(2), connect(2), accept(2), listen(2), poll(2), recv(2) etc...)
But if you are newbie about sockets, I strongly suggest reading a good network programming book.
I am looking into trying to do UDP/TCP hole punching using a servlet running on Google's AppEngine.
I would be using primarily the Java EE library. But I don't quite see how to forward a network connection request from the client to the other client who is acting as the P2P "host".
Is there something I'm missing in the ServletRequest/ServletResponse classes?
Don't think you're going to be able to handle UDP. However, for TCP, if you override the service method in the servlet and handle the "CONNECT" verb, you can then read from and write to the input and output streams. From the client side, you should be able to utilize this through a HttpURLConnection or something like Apache HTTP Client.