tags:

views:

186

answers:

3

In an application I need to use Socket connections as well as Http requests/responses in Java, I have done some work with Tomcat but I think it handles only HTTP requests/responses.

I was thinking of using Java Networking(java.net.Socket and java.net.ServerSocket), make a jar and run it on the server to handle the sockets and use Apache to handle the HTTP requests but I don't know how to integrate both of these.

The app will be installed on a home server so even the server part is under my control. Any ideas how to implement this?

+1  A: 

You mean to say you want to accept http request and Server Socket requests in the same JVM so that you can share the same Java objects? Not sure why but you can do that easily. Tomcat is there to start the web application and listen for http requests. You can write a register a listener in web application which will initialize a Server socket at some port. This way its in same JVM so you can share the objects.

Let me know if you were looking for something.

Bhushan
I don't really want to share objects, just serve a file and then close the connection on HTTP and if someone requests connection on a port I want to make a socket connection and keep the client connected.
Kevin Boyd
then what you mean by "how to integrate both of these"
Bhushan
By integrating I mean if I only write a client-server networking app with Socket and ServerSocket and then build the server side of the app I will get a jar, this will be like a stand alone process. Now the question is regarding the HTTP part how do I implement that and how to merge both apps into a single jar.( if necessary)...
Kevin Boyd
Kevin I read your requirements and my solution still holds true as that way you are creating a war which is running the webapp as well as server socket. But just for the sake of creating a single jar you are merging the applications. I would keep the two applications separate but yes the two apps can still use the common code.
Bhushan
Actually as I have commented to AndreaG the HTTP part is the first part of contact thereafter the app moves to socket mode connection to client so should I use a J2EE container(Tomcat) for this or can I get it done buy an Apache web server and just keep a seperate jar for the socket connection? What would be the pros of the first method over the second?
Kevin Boyd
If serving the policy file via http requires some dynamic code then you need Tomcat, i.e. reading request data and based on that serve correct policy file. Socket connection app needs to be started separately by batch file or windows service, etc.
Bhushan
It's always going to be the same Policy file, to all clients, Thanks
Kevin Boyd
+1  A: 

This kind of question (and the further explanation to Bhushan answer) makes me ask back "Why?"

Why do you think you need to mix up socket-based and HTTP communications? This smells quite bad.

Maybe if you detail your requirements we can come up with a cleaner design. Or better, look back at established solutions for such problems.

AndreaG
Clients are Flashlite devices and for the swf's on the device to make a cross domain request needs a policy file on the server, so the swf makes an HTTP request to the server, I need to handle this and serve the policy file. Once policy file is served I need the device to be connected to the server via sockets.
Kevin Boyd
Is this something like that: http://www.adobe.com/devnet/devices/articles/flashlite_sockets.html ? It uses Tornado (http://sourceforge.net/projects/tornadonio/), an Open Source Java NIO Socket Server.
AndreaG
Me again... Other links to OS socket servers and tutorial here (sorry, text in Italian): http://www.actionscript.it/forum/showthread.php?t=825
AndreaG
Felipe Andrade who wrote Tornado has posted that first link but he hasn't posted any Java code, I will search around for that, thanks for the tip though
Kevin Boyd
Sorry to say but the Tornado framework by Felipe is no longer supported.
Kevin Boyd
+1  A: 

You can create a new thread in the Servlet.init() method. In this thread, you run your listening loop for the ServerSocket.

ZZ Coder