I write a tcp server and a tcp client,the client only send data to server and print how many bytes it writed,the server only accept the connection,and then I use netstat to show the socket's Recv-Q and Send-Q,and I found that Recv-Q+send-Q > write bytes. How does it happend?
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;
}
this is the resulte:
standard output netstat result
Aucun commentaire:
Enregistrer un commentaire