tcp数据发送过快的处理

I also faced the same problem few weeks ago when implementing a VoIP server. After spending several days I could come up with a solution. As many others mentioned, there is no any direct system call to do the job. Instead,

  1. You can check if we have received the ACK after sending a packet with TCP_INFO option.
  2. If we haven't received the ACK, wait for few milliseconds and check again.

This may continue until a time out reaches. You have to implement it as a wrapper function to send() call. You will need tcp_info struct from <netinet/tcp.h>. It is the data structure for holding information about your tcp connection.

Here is the pseudo code

int blockingSend(const char *msg, int msglen, int timeout) { std::lock_guard<std::mutex> lock(write_mutx); int sent = send(sock_fd, msg, msglen, 0); tcp_info info; auto expireAt = chrono::system_clock::now() + chrono::milliseconds(timeout); do { this_thread::sleep_for(milliseconds(50)); getsockopt(sock_fd,SOL_TCP, TCP_INFO, (void *) &info, sizeof(info)); //wait till all packets acknowledged or time expires } while (info.tcpi_unacked > 0 && expireAt > system_clock::now()); if(info.tcpi_unacked>0) { cerr << "no of unacked packets :" << info.tcpi_unacked << endl; return -1; } return sent; }

Here tcpi_unacked member holds the number of packets unacknowledged of your connection. If you read it soon after the send() call, it will contain number of unacked packets which is equal to number of packets sent. With time, number of unacked packets will be decremented to zero. Therefore you need to periodically check the value of tcpi_unacked till it reaches zero. If the connection is half opened, you will never receive ACKs while causing a endless loop. For such scenarios you may need to add a timeout mechanism as implemented above.

Even though this question has been asked long ago, this answer may help some one who has faced the same problem. I must mention that there could be more accurate solutions to this problem than this solution. Since I am a newbie to system programming and C/C++ this is what I could come up with.

转载于:https://www.cnblogs.com/bornfish/p/5244936.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值