Recv-Q+Send-Q>写入字节
Posted
技术标签:
【中文标题】Recv-Q+Send-Q>写入字节【英文标题】:Recv-Q+Send-Q>write bytes 【发布时间】:2016-03-28 02:57:12 【问题描述】:我写了一个tcp服务器和一个tcp客户端,客户端只向服务器发送数据并打印它写入了多少字节,服务器只接受连接,然后我使用netstat显示套接字的Recv-Q和Send- Q,我发现 Recv-Q+send-Q > 写入字节。它是怎么发生的?
client code:
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <stdarg.h>
#include <errno.h>
void error(int status, int err, const char *fmt, ...)
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (err != 0)
fprintf(stderr, "errno = %d, errmsg = %s\n", err, strerror(err));
if (status != 0)
exit(err);
int set_address(const char *host, const char *serv, const char *protocol, sockaddr_in *addr)
struct hostent *host_info;
struct servent *serv_info;
memset(addr, 0, sizeof(sockaddr_in));
addr->sin_family = AF_INET;
if (host != NULL)
if (inet_aton(host, &addr->sin_addr) != 0)
host_info = gethostbyname(host);
if (host_info == NULL)
error(1, h_errno, "set_address failed, gethostbyname(%s) failed, errno = %d, errmsg = %s\n", host, h_errno, hstrerror(h_errno));
else
inet_aton(host_info->h_addr_list[0], &addr->sin_addr);
else
addr->sin_addr.s_addr = INADDR_ANY;
if (serv != NULL)
char * end_pos;
addr->sin_port = htons(strtol(serv, &end_pos, 10));
if (*end_pos != '\0')
serv_info = getservbyname(serv, protocol);
if (serv_info == NULL)
error(1, h_errno, "set_address failed, getservbyname(%s, %s) faield\n", serv, protocol);
else
addr->sin_port = serv_info->s_port;
int tcp_client(const char *host, const char *serv)
struct sockaddr_in server;
int fd;
const int on = 1;
set_address(host, serv, "tcp", &server);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
error(1, errno, "create socket failed\n");
if (connect(fd, (struct sockaddr *)&server, sizeof(server)) != 0)
error(1, errno, "connect to server failed\n");
return fd;
int main (int argc, char **argv)
if (argc != 3)
printf("usage: %s <host> <port>\n", basename(argv[0]));
exit(1);
char buf[1024];
int fd;
int total_write_bytes = 0, cur_write_bytes = 0;
fd = tcp_client(argv[1], argv[2]);
while (true)
cur_write_bytes = write(fd, buf, sizeof(buf));
if (cur_write_bytes <= 0)
break;
total_write_bytes += cur_write_bytes;
printf("total_write_bytes = %d, curr_write_bytes = %d\n", total_write_bytes, cur_write_bytes);
return 0;
server code:
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <stdarg.h>
#include <errno.h>
void error(int status, int err, const char *fmt, ...)
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (err != 0)
fprintf(stderr, "errno = %d, errmsg = %s\n", err, strerror(err));
if (status != 0)
exit(err);
int set_address(const char *host, const char *serv, const char *protocol, sockaddr_in *addr)
struct hostent *host_info;
struct servent *serv_info;
memset(addr, 0, sizeof(sockaddr_in));
addr->sin_family = AF_INET;
if (host != NULL)
if (inet_aton(host, &addr->sin_addr) != 0)
host_info = gethostbyname(host);
if (host_info == NULL)
error(1, h_errno, "set_address failed, gethostbyname(%s) failed, errno = %d, errmsg = %s\n", host, h_errno, hstrerror(h_errno));
else
inet_aton(host_info->h_addr_list[0], &addr->sin_addr);
else
addr->sin_addr.s_addr = INADDR_ANY;
if (serv != NULL)
char * end_pos;
addr->sin_port = htons(strtol(serv, &end_pos, 10));
if (*end_pos != '\0')
serv_info = getservbyname(serv, protocol);
if (serv_info == NULL)
error(1, h_errno, "set_address failed, getservbyname(%s, %s) faield\n", serv, protocol);
else
addr->sin_port = serv_info->s_port;
int tcp_server(const char *host, const char *serv)
struct sockaddr_in local;
int fd;
const int on = 1;
set_address(host, serv, "tcp", &local);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
error(1, errno, "create socket failed");
if (bind(fd, (sockaddr *)&local, sizeof(local)) != 0)
error(1, errno, "bind to port %s:%s failed\n", host, serv);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
error(1, errno, "set SO_REUSEADDR failed\n");
if (listen(fd, 100) != 0)
error(1, errno, "listen on %s:%s failed\n", host, serv);
return fd;
int main (int argc, char **argv)
if (argc != 3)
printf("usage: %s <host> <port>\n", basename(argv[0]));
exit(1);
int listen_fd, fd;
struct sockaddr_in addr;
socklen_t len;
listen_fd = tcp_server(argv[1], argv[2]);
fd = accept(listen_fd, (struct sockaddr *)&addr, &len);
if (fd < 0)
error(1, errno, "accept an connection failed\n");
while (true)
sleep(5);
return 0;
这是结果:
标准输出结果: standard output
netstat 结果: netstat result
tcpdump 结果:
14:17:16.633105 IP localhost.54393 > localhost.personal-agent: S 4282375064:4282375064(0) win 32767 14:17:16.633106 IP localhost.personal-agent > localhost.54393: S 4268411460:4268411460(0) ack 4282375065 win 32767 14:17:16.633115 IP localhost.54393 > localhost.personal-agent: 。确认 1 赢 8192 14:17:16.633127 IP localhost.54393 > localhost.personal-agent: P 1:1025(1024) ack 1 win 8192 14:17:16.633130 IP localhost.personal-agent > localhost.54393:.确认 1025 赢 8704 14:17:16.633156 IP localhost.54393 > localhost.personal-agent: P 1025:2049(1024) ack 1 win 8192 14:17:16.633159 IP localhost.personal-agent > localhost.54393:.确认 2049 赢 8704 14:17:16.633167 IP localhost.54393 > localhost.personal-agent: P 2049:3073(1024) ack 1 win 8192 14:17:16.633169 IP localhost.personal-agent > localhost.54393:.确认 3073 赢 8704 14:17:16.633176 IP localhost.54393 > localhost.personal-agent: P 3073:4097(1024) ack 1 win 8192 14:17:16.633179 IP localhost.personal-agent > localhost.54393:.确认 4097 赢 8704 14:17:16.633185 IP localhost.54393 > localhost.personal-agent: P 4097:5121(1024) ack 1 win 8192 14:17:16.633188 IP localhost.personal-agent > localhost.54393:.确认 5121 赢 8704 14:17:16.633195 IP localhost.54393 > localhost.personal-agent: P 5121:6145(1024) ack 1 win 8192 14:17:16.633198 IP localhost.personal-agent > localhost.54393:.确认 6145 赢 8704 14:17:16.633204 IP localhost.54393 > localhost.personal-agent: P 6145:7169(1024) ack 1 win 8192 14:17:16.633206 IP localhost.personal-agent > localhost.54393:.确认 7169 赢 8704 14:17:16.633213 IP localhost.54393 > localhost.personal-agent: P 7169:8193(1024) ack 1 win 8192 14:17:16.633215 IP localhost.personal-agent > localhost.54393:.确认 8193 赢 8704 14:17:16.633222 IP localhost.54393 > localhost.personal-agent: P 8193:9217(1024) ack 1 win 8192 14:17:16.633224 IP localhost.personal-agent > localhost.54393:.确认 9217 赢 8704 14:17:16.633230 IP localhost.54393 > localhost.personal-agent: P 9217:10241(1024) ack 1 win 8192 14:17:16.633233 IP localhost.personal-agent > localhost.54393:.确认 10241 赢得 8704 14:17:16.633239 IP localhost.54393 > localhost.personal-agent: P 10241:11265(1024) ack 1 win 8192 14:17:16.633242 IP localhost.personal-agent > localhost.54393:.确认 11265 赢 8448 14:17:16.633249 IP localhost.54393 > localhost.personal-agent: P 11265:12289(1024) ack 1 win 8192 14:17:16.633251 IP localhost.personal-agent > localhost.54393:.确认 12289 赢 8192 14:17:16.633258 IP localhost.54393 > localhost.personal-agent: P 12289:13313(1024) ack 1 win 8192 14:17:16.633261 IP localhost.personal-agent > localhost.54393:.确认 13313 赢 7936 14:17:16.633269 IP localhost.personal-agent > localhost.54393: 。确认 14337 赢 7680 14:17:16.671777 IP localhost.personal-agent > localhost.54393:.确认 31757 赢得 3325 14:17:16.879921 IP localhost.54393 > localhost.personal-agent: P 31757:45057(13300) ack 1 win 8192 14:17:16.959771 IP localhost.personal-agent > localhost.54393: 。确认 45057 赢 0 14:17:17.175771 IP localhost.54393 > localhost.personal-agent: 。确认 1 赢 8192 14:17:17.175786 IP localhost.personal-agent > localhost.54393:.确认 45057 赢 0 14:17:17.607770 IP localhost.54393 > localhost.personal-agent: 。确认 1 赢 8192 14:17:17.607782 IP localhost.personal-agent > localhost.54393:.确认 45057 赢 0 14:17:18.471768 IP localhost.54393 > localhost.personal-agent: 。 ack 1 赢 8192 14:17:18.471775 IP localhost.personal-agent > localhost.54393: 。 ack 45057 赢 0
【问题讨论】:
【参考方案1】:已收到但发送方尚未收到确认的字节在发送队列和接收队列中。它们保留在发送队列中,因为在它们被确认之前,发送者必须准备好重新发送它们。它们一直保留在接收队列中,直到应用程序调用接收函数并将它们从队列中取出。
【讨论】:
我贴出tcpdump结果,所有发送的消息都已确认以上是关于Recv-Q+Send-Q>写入字节的主要内容,如果未能解决你的问题,请参考以下文章