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.
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.
My service already uses Websockets to communicate with an HTML5 in-browser client. The client is served by the same server from a normal http request.
Now I would like to offer the same service/app but out of the browser, and I would like to offer it over TCP sockets.
The RPCs/action object I am using are going to be the same, the serialization is going to be the same, the logic is the same. I just want to use TCP socket instead of WebSocket.
I would like to keep the code together under the same "project folder", starting all at once when I deploy the playframework server (basically on start I want to start listening to WebSockets, TCP sockets and http requests), and have everything in the same package on deploy.
I know that:
It is not necessary, since WebSocket can be used in not-in-browser apps, but consider this an exercise or a curiosity question.
playframework is built on top of netty, and I used netty before to do some TCP services (nothing big and nothing prod ready though ... so not an expert). So they should work together right?
What I was thinking to do:
Have an akka actor listen for new socket connections.
Wrap the connections (WS or TCP sockets) into a ClientConnectionManager instance
Pass it to the actors that takes care of the connections/rpc logic.
Other leads I considered: Reimplementing the playframework Controller class.
Or is there an already implemented solution for this?
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 have not worked with HTTP post/get before, my up coming project in my office is based on http post/get in java. Its basically client - server based application. the client will post some info and I need to get that info and process the string and vice-verse. this project has to be developed on J2SE. You can assume this some thing like a JMS queue message processing stuff. I googled for the info but most of the information was for web application, mine should work like a message queue. Can someone explain me how to do this or point me where I can get some useful info.
Thanks
Arun
Well, if you don't need to specifically use strict HTTP, and you need to just use Java SE (and not Java EE, which rules out Servlets, JSPs, JMS, etc), then you need to probably investigate ServerSocket and Socket classes.
Server
Your server would need to listen on a TCP port (say, port 8080) - usually you would pick a port number between 1025 and 65,535, however if you are attempting to use an already defined service that has a default port, then use that. Note however, that on unix, in order to listen on any port below 1024, I believe you need to be root. Traditionally, port 80 is used for HTTP.
To listen on this port, you would need something like this in your code:
ServerSocket srvSocket = new ServerSocket(8080);
Socket socket = srvSocket.accept();
This pretty much the most basic code that would cause your application to wait until something connected to port 8080. Once connected, you could obtain both an InputStream and OutputStream for your connected client, by interrogating the returned socket object, allowing you to read content from the client, and inserting these requests in a queue. This queue could be then processed by some other Thread.
Client
In order for your client to connect to the server, you would need to use something based on the following example:
Socket connection = new Socket("server.domain.com", 8080);
OutputStream output = connection.getOutputStream();
You would then write your request to the server into the OutputStream (and read from the InputStream returned from getInputStream() if you expected a response)
The code supplied is pretty basic, but it should give you a rough idea of how to proceed. You can even use this method if you wanted to use real HTTP, however it might be a better idea to use some premade library if that was the case (although its probable that you're not going to require all functionality defined in the HTTP spec itself).
Anyway, I hope that provides you a good starting point from which to build.
Jetty is a popular web server, designed to easily be embedded in an application.
Its HTTP server component can run inside your application and respond to requests by dispatching to your custom code.
Jetty also features an HTTP client that you can use on the client side to send requests.
This is a rather big topic and I won't be able to post a complete guide, but Jetty's documentation is generally of very high quality and should be a good starting point.
I suggest you start with learning the basics of HTTP protocol. This article is a good starter. After you understood the basics follow the this article on how to programatically communicate (read/write) with HTTP servers. After that Google is your friend.
If you weren't restricted to J2SE, you could use Servlets for managing the POST/GET methods of HTTP. Evaluate if it is possible, otherwise you'd be reinventing the wheel
I also have a mainly SE background. On the client side, writing get/post is pretty easy. Or you can Google to find source code. I found that using REST was straightforward and understandable. On the server side, there are many options and I have very limited experience. I wrote the server using standard JEE6 and it wasn't too painful, but sounds like that is not an option for you.