I am trying to implement comet grizzly on my glassfish server v3.
I am trying to connect web server from desktop application using http url object.
I am creating ObjectInputStreamer and ObjectOutputStreamer at both client and web server.
In webserver servlet I am creating ObjectOutputStream to write response back to client.
And this output streamer I am attaching to handling of comet so that I could push data to client without request on same response channel afterwards.
and on client I am not closing the InputStreamer so that I can read the response pushed by webserver using comet.
But on writing data on output stream from webserver it is not giving any exception but still I am not able to read at client end which gives EOFException on reading from opened Input Stream.
Thanks,
Ali
Comet is a technique used to notify a client of changes on the server over an http internet connection. A good place to start learning comet are the examples here. Using the java.io.ObjectInput/OutputStream is for reading in files, passing serialized java objects, etc. They use the Object Serialization Stream Protocol, which won't work with comet.
Related
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 have a complete implementation of a protocol where four messages are exchanged between the client (a native Android application) and the server (a standalone Java server) in the following way using a persistent connection through Java sockets:
(client->server): message1
(server->client); message2
(client->server): message3
(server->client): message4
Between sending each message, both client and server have to do heavy mathematical (cryptographic) operations (pairing-based computations over elliptic curves).
This project works properly in my local development machine using sockets and mantaining opened this socket from message1 to the message4 between the Android app and the Java server. Now, I need to do the same with Google AppEngine, but since it does not allow opening sockets, I do not know how can I do it. I already checked the Channel and XMPP APIs, but I do not know whether my use-case applies to that APIs. Is it the right method using Channel and XMPP APIs from AppEngine? Is it possible to emulate the functionality implemented in my local machine through sockets on AppEngine?
Thank you for your response.
Google App Engine doesn't support persistent connections.
You will need to significantly re-design your protocol to run over HTTP.
As an example, message1 can be sent over an HTTP request, message2 can be returned with the matching HTTP response. At that point, your socket connection ends.
You'll have to issue a second HTTP request to open a new socket with message3, and you can return message4 with the second HTTP response.
You can "connect" the first and second HTTP request by using an HTTP session. A session is basically an id with extra data stored on the server side. You'd create the session in the first HTTP request, and pass it as a parameter to the second HTTP request. The server can look up the session id and the associated data when processing the second request.
You can find more info about sessions on SO: How to use session on Google app engine
The XMPP API will not help you, it's for communicating between the GAE server-side code and other XMPP clients that use HTTP as a communcation protocol.
The Channel API can be used to send data from the server->client, but it's actually implemented as an HTTP long poll. Multiple long HTTP requests are used, and you are not guaranteed to have a single socket that stays open; multiple sockets are opened and closed in the process. It will be more complicated that what I described above, and more expensive.
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 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).
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.