helpermethod
helpermethod

Reputation: 62175

In Java, does something comparable to Python's socketserver exists?

I'm implementing a simple TCP client and TCP server in Java at the moment, and when searching for examples, I've stumbled across this nice Python framework:

http://docs.python.org/library/socketserver.html

EDIT

I'm looking for a solution where you can create a TCP-Server by doing a call like this:

TCPServer server = new TCPServer(port, RequestHandler);
server.serveForever();

So, I have a multihreaded server out of the box, where I only have to implement the RequestHandler (which could be some sort of interface requiring a handle method).

Is there something similar in Java? It seems to make implementing network servers very easy and straightforward.

Upvotes: 4

Views: 222

Answers (4)

eee
eee

Reputation: 1053

I've found something similar to what above Python code can do (as in your post) right here: http://javawork.org/examples/

 final SocketListener listener = new SocketListener( 6060 );
 listener.start();

Upvotes: 1

vbence
vbence

Reputation: 20333

There are good examples for Java how you can create multithreaded servers. I can't imagine what a third party API can do to make this simpler/easier.

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76898

Not in the core language, no. You have to do the normal things you would do in most languages where you set up the listener, accept connections, check for input, etc.

I'm sure somewhere someone has built a generic "framework" for this, but in all honestly, it's about 40 lines of code and there's umteen tutorials out there that show it.

The most analogous approach is using the java NIO classes (1.4 and above) and using the Selector to accept and poll for new connections and input on connected sockets.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533520

Do you mean like ServerSocket in java? http://www.google.co.uk/search?q=java+serversocket+tutorials

If it appears complicated there is likely to be a simpler way to do it. I suggest you ask a more specific question if you have one.

Upvotes: 0

Related Questions