Is Java Socket still the first choice for programming TCP / IP in Java?

I need to interact with the server via TCP / IP with the basic message / response protocol (so for each request I get a specific response).

In the JVM ecosystem, I believe Java Socket was a tool to use 15 years ago, but I wonder if there is anything more suitable now in the JDK? For example, with Java Sockets, you still need to manually program the request if you do not receive a response that seems really old-fashioned.

So, is there something new in the JDK or JVM universe?

+5
source share
4 answers

No, now there is a much better option that allows you to implement your client / server asynchronously without additional streaming.

Take a look at SocketChannel from nio or even better AsynchronousSocketChannel from nio2. Check tutorial

In particular, the latter option will allow you only to start the connection listener and register a callback that will be called whenever a new connection is requested, data arrives, data is recorded, etc.

Also consider some high-level solutions, such as Netty . He will take care of the network core, will evenly distribute the load on the performers. In addition, it provides a clean separation of the network layer and the processing layer. For the processing layer, it will provide you with many reusable codecs for common protocols.

+4
source

You can try RMI, which runs on top of TCP / IP, but hides all labor costs with a convenient set of APIs.

Follow this post guide

+2
source

Well, there really are many other technologies to use, for example JMS has various implementations that work out of the box.

Sockets are low-level building blocks of network communications, such as wires in the electrical network of your home. Yes, they are old-fashioned, yes, we probably do not want to see them, but they are there, and they will stay there for a good reason.

At the top of the sockets, for example, select HTTPUrlConnection, which implements most of the HTTP protocol. However, setting timeout policies is in your hands, which I find very useful and extremely painful at the same time.

http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

You can move one level of abstraction higher and use a ready-made REST library, such as: http://unirest.io/java.html p>

The above example connects to the server, configures the HTTP request string, executes the request (timeout, encodings, all the clutter under the hood) and finally receives a Json response in several lines:

Unirest.post("http://httpbin.org/post") .queryString("name", "Mark") .field("last", "Polo") .asJson(); 

Currently, a huge number of web services are available using the REST protocol, which is a simple HTTP command response. If you have a chance, I would suggest using REST, as you can easily find the available implementations on the client side and on the server, and you do not need to reinvent the wheel at the command protocol level.

On the client side, unirest is pretty convenient. On the server side, we had a really great experience in the 1.2.xx game series! framework But there are thousands of these things, just find "REST".

+1
source

Take a look at the Netty Project , "Netty is a NIO client server platform that allows you to quickly and easily develop network applications such as protocol servers and clients. It greatly simplifies and simplifies network programming such as TCP and UDP socket servers."

This structure gives us many features that simplify the programming process, allowing greater scalability.

Used by Twitter and many large technology companies.

This is a good presentation from Norman Maurer.

+1
source

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


All Articles