简体   繁体   English

如何在Java中进行TCP通信中的Acknowlegement

[英]How to get Acknowlegement in TCP communication in Java

I have written a socket program in Java. 我用Java编写了一个socket程序。 Both server and client can sent/receive data to each other. 服务器和客户端都可以相互发送/接收数据。 But I found that if client sends data to server using TCP then internally TCP sends acknowledgement to the client once the data is received by the server. 但我发现如果客户端使用TCP将数据发送到服务器,则内部TCP会在服务器收到数据后向客户端发送确认。 I want to detect or handle that acknowledgement. 我想检测或处理该确认。 How can I read or write data in TCP so that I can handle TCP acknowledgement. 如何在TCP中读取或写入数据,以便我可以处理TCP确认。 Thanks. 谢谢。

This is simply not possible, even if you were programming in C directly against the native OS sockets API. 即使您使用C直接针对本机OS套接字API进行编程,这根本不可能实现。 One of the points of the sockets API is that it abstracts this away for you. 套接字API的一个要点是它为您抽象出来。

The sending and receiving of data at the TCP layer doesn't necessarily correlate with your Java calls to send or receive data. 在TCP层发送和接收数据不一定与发送或接收数据的Java调用相关。 The data you send in one Java call may be broken into several pieces which may be buffered, sent and received independently, or even received out of order. 您在一次Java调用中发送的数据可能会被分成几个部分,这些部分可能被独立缓冲,发送和接收,甚至无序接收。

See here for more discussion about this. 有关此内容 ,请参阅此处

Any data sent over a TCP socket is acknowledged in both directions. 通过TCP套接字发送的任何数据都在两个方向上得到确认。 Data sent from client to server is the same as data sent from server to client as far as TCP wire communications and application signaling. 从TCP服务器发送到服务器的数据与从TCP服务器发送到客户端的数据相同,直到TCP有线通信和应用程序信令。 As @Eric mentions, there is no way to get at that signaling. 正如@Eric所提到的,没有办法得到那个信号。

It may be that you are talking about timing out while waiting for the response from the server. 在等待服务器的响应时,您可能正在谈论超时。 That you'd like to detect if a response is taking too long. 您想要检测响应是否花费太长时间。 Is it possible that the client's message is larger than the server's response so the buffering is getting in the way of the response but not the initial request? 是否有可能客户端的消息大于服务器的响应,因此缓冲会妨碍响应而不是初始请求? Have you tried to use non-blocking sockets? 您是否尝试使用非阻塞套接字?

You might want to take a look at the NIO code if you have not already done so. 如果你还没有这样做,你可能想看一下NIO代码 It has a number of classes that give you more fine grained control over socket communications. 它有许多类,可以让您对套接字通信进行更细粒度的控制。

This is not possible in pure Java since Java's network API all handles socket, which hides all the TCP details. 这在纯Java中是不可能的,因为Java的网络API都处理套接字,它隐藏了所有TCP细节。

You need a protocol that can handle IP-layer data so you can get TCP headers. 您需要一个可以处理IP层数据的协议,以便您可以获得TCP标头。 DLPI is the most popular API to do this, DLPI是最受欢迎的API,

http://www.opengroup.org/onlinepubs/9638599/chap1.htm http://www.opengroup.org/onlinepubs/9638599/chap1.htm

Unfortunately, there is not Java implementation of such network. 不幸的是,没有这种网络的Java实现。 You have to use native code through JNI to do this. 您必须通过JNI使用本机代码才能执行此操作。

I want to detect or handle that acknowledgement. 我想检测或处理该确认。

There is no API for receiving or detecting the ACKs at any level above the protocol stack. 在协议栈之上的任何级别都没有用于接收或检测ACK的API。

Rethink your requirement. 重新考虑你的要求。 Knowing that the data has got to the server isn't any use to an application. 知道数据已经到达服务器对应用程序没有任何用处。 What you want to know is that the peer application has received it, in which case you have to get the peer application to acknowledge at the application protocol level. 您想知道的是对等应用程序已收到它,在这种情况下,您必须让对等应用程序在应用程序协议级别确认。

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

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