公司的server后台部署在某一个地方,接入的是用户的APP,而该地方的网络信号较差,导致了server后台在执行一段时间后用户无法接入,那边的同事反馈使用netstat查看系统。存在较多的TCP连接。

1. 问题分析

  首先在公司内部測试server上部署,使用LoadRunner做压力測试,能正常执行,然后那边的同事反馈该地方信号较差。考虑到接入的问题。有可能接入进程的FD资源耗尽,导致accept失败。推论的根据是对于TCP连接来说,假设client那边因为一些异常情况导致断网而未能向server发起FIN关闭消息,服务端这边若没有设置存活检測的话。该连接会存在(存活时间暂未測)。

2. 实验測试

  这里简单地写了一个服务端的程序。主要功能是回应,即接受一个报文(格式:2Byte报文长度+报文内容),然后原封不动将报文内容发回client。

  1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <sys/epoll.h>
5 #include <unistd.h>
6 #include <pthread.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <arpa/inet.h>
10
11 int g_epfd;
12
13 int InitServer( unsigned short port )
14 {
15 int nServerFd = socket( AF_INET, SOCK_STREAM, 0 );
16
17 struct sockaddr_in addr;
18 memset( &addr, 0, sizeof(addr) );
19
20 addr.sin_family = AF_INET;
21 addr.sin_port = htons( port );
22 addr.sin_addr.s_addr = 0;
23
24 if ( bind( nServerFd, (struct sockaddr *)&addr, sizeof(addr) ) <0 )
25 {
26 printf("bind error\n");
27 exit(-1);
28 }
29
30 if ( listen( nServerFd, 128 ) < 0 )
31 {
32 printf("listen error\n");
33 exit(-1);
34 }
35
36 return nServerFd;
37 }
38
39 int AddFd( int epfd, int nFd , int nOneShot)
40 {
41 struct epoll_event event;
42 memset( &event, 0, sizeof( event) );
43
44 event.data.fd = nFd;
45 event.events |= EPOLLIN | EPOLLRDHUP | EPOLLET;
46
47 if ( nOneShot ) event.events |= EPOLLONESHOT;
48
49 return epoll_ctl( epfd, EPOLL_CTL_ADD, nFd, &event );
50 }
51
52 int ResetOneShot( int epfd, int nFd )
53 {
54 struct epoll_event event;
55 memset( &event, 0, sizeof(event) );
56
57 event.data.fd = nFd;
58 event.events |= EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
59
60 return epoll_ctl( epfd, EPOLL_CTL_MOD, nFd, &event);
61 }
62
63 void * ReadFromClient( void * arg )
64 {
65 int nClientFd = (int)arg;
66 unsigned char buf[1024];
67 const int nBufSize = sizeof( buf );
68 int nRead;
69 int nTotal;
70 int nDataLen;
71
72 printf("ReadFromClient Enter\n");
73
74 if ( (nRead = read( nClientFd, buf, 2 )) != 2 )
75 {
76 printf("Read Data Len error\n");
77 pthread_exit(NULL);
78 }
79
80 nDataLen = *(unsigned short *)buf;
81 printf("nDataLen [%d]\n", nDataLen);
82 nDataLen = buf[0]*256 + buf[1];
83 printf("nDataLen [%d]\n", nDataLen);
84
85 nRead = 0;
86 nTotal = 0;
87 while( 1 )
88 {
89 nRead = read( nClientFd, buf + nRead, nBufSize );
90 if ( nRead < 0 )
91 {
92 printf("Read Data error\n");
93 pthread_exit( NULL );
94 }
95 nTotal += nRead;
96 if ( nTotal >= nDataLen )
97 {
98 break;
99 }
100 }
101 printf("nTotal [%d]\n", nTotal);
102
103 sleep(5);
104
105 int nWrite = write( nClientFd, buf, nTotal );
106 printf("nWrite[%d]\n", nWrite);
107
108 printf("Not Write ResetOneShot [%d]\n", ResetOneShot(g_epfd, nClientFd));
109
110 return NULL;
111 }
112
113 int main(int argc, char const *argv[])
114 {
115 int i;
116 int nClientFd;
117 pthread_t tid;
118 struct epoll_event events[1024];
119
120 int nServerFd = InitServer( 7777 );
121 if ( nServerFd < 0 )
122 {
123 perror( "nServerFd" );
124 exit(-1);
125 }
126
127 int epfd = epoll_create( 1024 );
128
129 g_epfd = epfd;
130
131 int nReadyNums;
132
133 if ( AddFd( epfd, nServerFd, 0 ) < 0 )
134 {
135 printf("AddFd error\n");
136 exit(-1);
137 }
138
139 while( 1 )
140 {
141 nReadyNums = epoll_wait( epfd, events, 1024, -1 );
142
143 if ( nReadyNums < 0 )
144 {
145 printf("epoll_wait error\n");
146 exit(-1);
147 }
148
149 for ( i = 0; i < nReadyNums; ++i)
150 {
151 if ( events[i].data.fd == nServerFd )
152 {
153 nClientFd = accept( nServerFd, NULL, NULL );
154
155 AddFd( epfd, nClientFd, 1 );
156
157 }else if ( events[i].events & EPOLLIN )
158 {
159 // Can be implemented by threadpool
160 //Read data from client
161 pthread_create( &tid, NULL, ReadFromClient, (void *)(events[i].data.fd) );
162
163 }else if ( events[i].events & EPOLLRDHUP )
164 {
165 //Close By Peer
166 printf("Close By Peer\n");
167 close( events[i].data.fd );
168 }else
169 {
170 printf("Some thing happened\n");
171 }
172
173 }
174 }
175
176 return 0;
177 }

測试内容:

注:clientIP: 192.168.10.108  serverIP&Port: 192.168.10.110:7777

a. client发送一个报文至服务端,然后断网。

(这里对程序做了点修改。这次实验凝视了write响应,防止write影响測试,后面一个实验会使用write)。

http://bjjzdx.tumblr.com
http://bjbdzbyz.tumblr.com
http://shbdzbyz.tumblr.com
http://pp1fe.tumblr.com
http://mo3ay.tumblr.com
http://p155f.tumblr.com
http://ff0xy.tumblr.com
http://l0q6c.tumblr.com
http://qnkso.tumblr.com
http://yubml.tumblr.com
http://ejubd.tumblr.com
http://cjb2t.tumblr.com
http://jgaey.tumblr.com
http://l89mp.tumblr.com
http://k1q63.tumblr.com
http://l2bwz.tumblr.com
http://u5zq9.tumblr.com
http://iq4an.tumblr.com
http://f2wla.tumblr.com
http://u40ng.tumblr.com
http://h4jfn.tumblr.com
http://u6cuu.tumblr.com
http://kztnc.tumblr.com
http://w9c84.tumblr.com
http://hpw9q.tumblr.com
http://hj4wp.tumblr.com
http://y9mok.tumblr.com
http://vev1n.tumblr.com
http://ev3db.tumblr.com
http://w7raj.tumblr.com
http://it5bt.tumblr.com
http://qjelk.tumblr.com
http://hyjlf.tumblr.com
http://bp4xn.tumblr.com

server后台TCP连接存活问题的更多相关文章

  1. 服务器后台TCP连接存活问题

    0. 背景 公司的服务器后台部署在某一个地方,接入的是用户的APP,而该地方的网络信号较差,导致了服务器后台在运行一段时间后用户无法接入,那边的同事反馈使用netstat查看系统,存在较多的TCP连接 ...

  2. ESP8266 station模式下建立client、server TCP连接

    程序实现内容: 1.在station模式下,ESP8266作为client.server进行TCP连接2.实现数据的发送.接收(同时回传)实现思路:TCP网络通信分层为:应用层.网络层.数据链路层.物 ...

  3. TCP连接状态详解及TIME_WAIT过多的解决方法

    上图对排除和定位网络或系统故障时大有帮助,但是怎样牢牢地将这张图刻在脑中呢?那么你就一定要对这张图的每一个状态,及转换的过程有深刻地认识,不能只停留在一知半解之中.下面对这张图的11种状态详细解释一下 ...

  4. close_wait状态和time_wait状态(TCP连接)

    1.CLOSE_WAIT的简单解决方案 不久前,我的Socket Client程序遇到了一个非常尴尬的错误.它本来应该在一个socket长连接上持续不断地向服务器发送数据,如果socket连接断开,那 ...

  5. 高并发TCP连接数目问题

    linux可通过五元组唯一确定一个链接:源IP,源端口,目的IP,目的端口,传输层协议.而一个端口不允许被两个及以上进程占用(一个进程可同时占用多个端口),据此是否可以推测一台linux服务器最多可以 ...

  6. TCP连接的状态与关闭方式及其对Server与Client的影响

    TCP连接的状态与关闭方式及其对Server与Client的影响 1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用. ...

  7. TCP连接的状态与关闭方式,及其对Server与Client的影响

    1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用.特定数据包以及超时等,具体状态如下所示: CLOSED:初始状态, ...

  8. TCP连接——爱的传声筒

    TCP连接——爱的传声筒 TCP通信最重要的特征是:有序(ordering)和可靠(reliable).有序是通过将文本流分段并编号实现的.可靠是通过ACK回复和重复发送(retransmission ...

  9. TCP连接与关闭

    1.建立连接协议(三次握手) (1)客户端发送一个带SYN标志的TCP报文到服务器.这是三次握手过程中的报文1. (2) 服务器端回应客户端的,这是三次握手中的第2个报文,这个报文同时带ACK标志和S ...

随机推荐

  1. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

  2. 数据库相关--mysql8.0设置root登录密码

    系统:win7  64位 MySQL    8.0 .11 1.启动服务: $ net start mysql 进入MySQL(mysql -hlocalhost -uroot): 2.选择mysql ...

  3. Print Article HDU - 3507 -斜率优化DP

    思路 : 1,用一个单调队列来维护解集. 2,假设队列中从头到尾已经有元素a b c.那么当d要入队的时候,我们维护队列的下凸性质, 即如果g[d,c]<g[c,b],那么就将c点删除.直到找到 ...

  4. python urllib 库

    urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...

  5. 使用Actuator监控

    Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能. springboot使用actuator的方式非常简单,只需要在项目中加入依赖spring ...

  6. padding和margin设置成百分比

    Margin和Padding是我们在网页设计经常使用到的CSS样式,他们分别是间距和填充,一个作用于盒子外面,一个作用于盒子里面,默认的情况下,这些属性的值都会被计算在盒子的面积里面,在网页开发中的流 ...

  7. MongoError: Cannot update '__v' and '__v' at the same time,错误解决办法

    1.讲查询的结果,原封不动的插入到另一个表中,结果报错了:MongoError: Cannot update '__v' and '__v' at the same time,起初认为是mongodb ...

  8. 分金币 [CQOI 2011] [BZOJ 3293]

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...

  9. .net 4.0 中的特性总结(一):dynamic

    在新版本的C#中,dynamic关键词是一个很重要的新特性,现在你可以创建动态对象并在运行时再决定它的类型.而且.net 4.0为CLR加入了一组为动态语言服务的运行时环境,称为DLR(Dynamic ...

  10. 深入理解JVM(2)——揭开HotSpot对象创建的奥秘

    一.对象创建的过程:当虚拟机遇到一条含有New的指令时,会进行一系列对象创建的操作. 检查常量池中是否含有带创建对象所属类的符号引用 a) 如果含有的话,直接进行下一步 b) 如果常量池中没有这个符号 ...