简体   繁体   中英

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

I need to interact with a server over TCP/IP with a basic message/response protocol (so for each request I shall receive a defined response).

In the JVM ecosystem, I think Java Socket was the tool to use 15 years ago, but I'm wondering if there is anything more suitable nowadays in the JDK? For example, with Java Sockets one still needs to manually timeout a request if no answer is received, which feels really old fashioned.

So is there anything new in the JDK or JVM universe?

No, there are much better option nowadays which allow you to implement your client/server asynchronously without additional threading.

Look at SocketChannel from nio or even better AsynchronousSocketChannel from nio2. Check this tutorial

Especially the latter option will allow you to just start the connection listener and register callback which will be called whenever new connection is requested, data arrived, data was written etc.

Also consider looking at some high level solutions like Netty . It will take care of the network core, distribute load evenly to executors. Additionally it provides clears separation of the network layer and processing layer. For the processing layer it will provide you with lot of reusable codecs for common protocols.

You can try RMI which works on top of TCP/IP but hides all the hardwork with a convenient set of APIs.

Follow this tutorial post

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

Sockets are low-level building blocks of network communications, like wires in the electricity network of your house. Yes, they're old fashioned, yes, we likely don't want to see them, but they're there and they will stay there for a good reason.

On top of Sockets, you can eg pick the HTTPUrlConnection, which implements most of the HTTP protocol. Still, setting timeout policies are in your hands, which I find quite useful, and extremelly painful at the same time.

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

You are free to move one abstraction level above, and use a ready-made REST library, such as this: http://unirest.io/java.html

The example above connects to a server, configures a HTTP query string, perform the request (timeout, encodings, all the mess under the hood), and finally get the response in Json format in a few lines:

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

Nowadays a vast amount of web services are available using REST protocol, which is a simple command-response over HTTP. If you have a chance, I'd suggest using REST, as you can easily find available client and server side implementations, and you don't need to reinvent the wheel on the command-protocol layer either.

On client side, unirest is quite convenient. On the server side, we have had really great experience in the 1.2.xx series of Play! framework. But there are thousands of these things out there, just search for "REST".

Take a look to Netty Project , " Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server ."

This framework give us a lot of capabilities that simplify the programming process, allowing a big scalability.

Is used by Twitter and a lot of big companies in the tecnology industry.

This is a nice presentation from Norman Maurer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM