简体   繁体   English

Java Socket仍然是Java中TCP / IP编程的首选?

[英]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). 我需要通过TCP / IP与基本消息/响应协议的服务器进行交互(因此对于每个请求,我将收到一个定义的响应)。

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? 在JVM生态系统中,我认为Java Socket是15年前使用的工具,但我想知道现在JDK中是否有更合适的东西? For example, with Java Sockets one still needs to manually timeout a request if no answer is received, which feels really old fashioned. 例如,对于Java套接字,如果没有收到回答,则仍然需要手动超时请求,这感觉非常老式。

So is there anything new in the JDK or JVM universe? 那么JDK或JVM领域有什么新东西吗?

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. 从nio看SocketChannel ,或者从nio2看更好的AsynchronousSocketChannel 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 . 还要考虑一下像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. 您可以尝试在TCP / IP之上工作的RMI,但使用一组方便的API隐藏所有硬件。

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. 嗯,确实有很多其他技术可供使用,例如JMS有各种各样的实现,它们开箱即用。

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. 在套接字之上,您可以选择HTTPUrlConnection,它实现了大部分HTTP协议。 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/ 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 您可以自由移动上面的一个抽象级别,并使用现成的REST库,例如: 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: 上面的示例连接到服务器,配置HTTP查询字符串,执行请求(超时,编码,引擎盖下的所有混乱),最后以几行代码获取Json格式的响应:

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. 如今,使用REST协议可以获得大量的Web服务,这是一种基于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. 如果您有机会,我建议您使用REST,因为您可以轻松找到可用的客户端和服务器端实现,并且您也不需要在命令协议层上重新发明轮子。

On client side, unirest is quite convenient. 在客户端,unirest非常方便。 On the server side, we have had really great experience in the 1.2.xx series of Play! 在服务器端,我们在1.2.xx系列Play中拥有非常棒的体验! framework. 框架。 But there are thousands of these things out there, just search for "REST". 但是那里有成千上万的东西,只是搜索“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 ." 看看Netty项目 ,“ Netty是一个NIO客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化并简化了TCP和UDP套接字服务器等网络编程 。”

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. 被Twitter和许多大公司用于技术行业。

This is a nice presentation from Norman Maurer. 这是Norman Maurer的精彩演讲

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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