Is there anything comparable to Python socketserver in Java?

I am currently implementing a simple TCP client and TCP server in Java, and when searching for examples, I came across this beautiful Python structure:

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

EDIT

I am looking for a solution where you can create a TCP server by making this call:

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

So, I have a multi-page server out of the box where I only need to implement the RequestHandler (which may be some kind of interface that requires the handle method).

Is there something similar in Java? It seems that making network servers very easy and straightforward.

+4
source share
4 answers

Not in the base language, no. You have to do the usual things that you would do in most languages, where you set up your listener, accepted connections, checked for input, etc.

I’m sure that somewhere, someone created a common “infrastructure” for this, but honestly, this is about 40 lines of code, and there are twelve tutorials that show this.

The most similar approach is to use Java NIO classes (1.4 and above) and use Selector to accept and poll new connections and enter into connected sockets.

+3
source

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

  final SocketListener listener = new SocketListener( 6060 ); listener.start(); 
+1
source

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

If it turns out to be difficult, there will most likely be an easier way to do this. I suggest you ask a more specific question if you have one.

0
source

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 it simpler / easier.

0
source

Source: https://habr.com/ru/post/1343604/


All Articles