close_wait的影响和消除

本文深入解析TCP连接中close_wait状态的原理,详细解释为什么会出现该状态,以及可能导致的原因,如服务端数据未处理完成或未调用close(socket)。同时提供了解决close_wait状态的方法,包括改善服务端代码和使用keepalive保活机制来检测和处理半关闭连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

close_wait

close_wait是TCP结束连接四次握手时被动关闭的一端出现的状态。如下图所示:
这里写图片描述

为什么会出现保持在close_wait呢?很明显是被动关闭的一方未发FIN导致其一直处于CLOSE_WAIT。
那么为什么被动关闭的一方未发FIN了?很可能是被动关闭的一方还用数据没有发完,导致FIN没发。或者是
服务端没有调用TCP的close(socket)。

close_wait的影响

Close_Wait会占用一个连接,数量过多,就使可用的连接减少,还会并占用系统非换页内存。 而linux可用连接也是有限制的。如果不加处理,会极大的影响服务器的性能。

close_wait的解决方法

一是对服务端代码进行改善,查看服务端代码在处理完毕客户端请求,并在客户端数据处理完毕后,有没有进行使用close(socket)操作.
二是使用keepalive保活机制来对这种半关闭连接进行检测
int keepAlive = 1;//开启keepalive属性
int keepIdle = 60;//如果60秒内没有任何数据往来,则进行探测
int keepInterval = 6;//探测时发包的时间间隔为6秒
int keepCount = 3;//探测尝试的次数,如果第一次探测包就收到相应,以后的2次就不再发
//keepalive
setsockopt(m_iSock,SOL_SOCKET, SO_KEEPALIVE, (void*)&keepAlive, sizeof(int));
setsockopt(m_iSock,SOL_TCP,TCP_KEEPIDLE,(void*)&keepIdle, sizeof(int));
setsockopt(m_iSock,SOL_TCP, TCP_KEEPINTVL,(void*)&keepInterval,sizeof(int));
setsockopt(m_iSock,SOL_TCP, TCP_KEEPCNT,(void*)&keepCount,sizeof(int));

static int media_get_socket(void* handle) { MediaIOPriv* priv = handle; if (!priv || priv->socket <= 0) return -EINVAL; return priv->socket; } /**************************************************************************** * Public Functions ****************************************************************************/ int media_process_command(const char* target, const char* cmd, const char* arg, char* res, int res_len) { return media_proxy(MEDIA_ID_GRAPH, NULL, target, cmd, arg, 0, res, res_len); } void media_graph_dump(const char* options) { media_proxy(MEDIA_ID_GRAPH, NULL, NULL, "dump", options, 0, NULL, 0); } void* media_player_open(const char* params) { return media_open(MEDIA_ID_PLAYER, params); } int media_player_close(void* handle, int pending_stop) { return media_close(handle, pending_stop); } int media_player_set_event_callback(void* handle, void* cookie, media_event_callback event_cb) { return media_set_event_cb(handle, cookie, event_cb); } int media_player_prepare(void* handle, const char* url, const char* options) { return media_prepare(handle, url, options); } int media_player_reset(void* handle) { if (!handle) return -EINVAL; media_close_socket(handle); return media_proxy_once(handle, NULL, "reset", NULL, 0, NULL, 0); } ssize_t media_player_write_data(void* handle, const void* data, size_t len) { return media_process_data(handle, true, (void*)data, len); } int media_player_get_sockaddr(void* handle, struct sockaddr_storage* addr) { return media_get_sockaddr(handle, addr); } void media_player_close_socket(void* handle) { media_close_socket(handle); } int media_player_get_socket(void* handle) { return media_get_socket(handle); } int media_player_start(void* handle) { if (!handle) return -EINVAL; return media_proxy_once(handle, NULL, "start", NULL, 0, NULL, 0); } int media_player_stop(void* handle) { if (!handle) return -EINVAL; media_close_socket(handle); return media_proxy_once(handle, NULL, "stop", NULL, 0, NULL, 0); } int media_player_pause(void* handle) { return media_proxy_once(handle, NULL, "pause", NULL, 0, NULL, 0); } int media_player_seek(void* handle, unsigned int msec) { char tmp[32]; snprintf(tmp, sizeof(tmp), "%u", msec); return media_proxy_once(handle, NULL, "seek", tmp, 0, NULL, 0); } int media_player_set_looping(void* handle, int loop) { char tmp[32]; snprintf(tmp, sizeof(tmp), "%d", loop); return media_proxy_once(handle, NULL, "set_loop", tmp, 0, NULL, 0); } int media_player_is_playing(void* handle) { char tmp[32]; int ret; ret = media_proxy_once(handle, NULL, "get_playing", NULL, 0, tmp, sizeof(tmp)); return ret < 0 ? ret : !!atoi(tmp); } int media_player_get_position(void* handle, unsigned int* msec) { char tmp[32]; int ret; if (!msec) return -EINVAL; ret = media_proxy_once(handle, NULL, "get_position", NULL, 0, tmp, sizeof(tmp)); if (ret >= 0) *msec = strtoul(tmp, NULL, 0); return ret; } int media_player_get_duration(void* handle, unsigned int* msec) { char tmp[32]; int ret; if (!msec) return -EINVAL; ret = media_proxy_once(handle, NULL, "get_duration", NULL, 0, tmp, sizeof(tmp)); if (ret >= 0) *msec = strtoul(tmp, NULL, 0); return ret; } int media_player_get_latency(void* handle, unsigned int* latency) { char tmp[32]; int ret; if (!latency) return -EINVAL; ret = media_proxy_once(handle, NULL, "get_latency", NULL, 0, tmp, sizeof(tmp)); if (ret >= 0) *latency = strtoul(tmp, NULL, 0); return ret; } int media_player_set_volume(void* handle, float volume) { char tmp[32]; snprintf(tmp, sizeof(tmp), "%f", volume); return media_proxy_once(handle, NULL, "volume", tmp, 0, NULL, 0); } int media_player_get_volume(void* handle, float* volume) { char tmp[32]; int ret; if (!volume) return -EINVAL; ret = media_proxy_once(handle, NULL, "get_volume", NULL, 0, tmp, sizeof(tmp)); if (ret >= 0) { sscanf(tmp, "vol:%f", volume); ret = 0; } return ret; } int media_player_set_property(void* handle, const char* target, const char* key, const char* value) { return media_proxy_once(handle, target, key, value, 0, NULL, 0); } int media_player_get_property(void* handle, const char* target, const char* key, char* value, int value_len) { return media_proxy_once(handle, target, key, NULL, 0, value, value_len); } void* media_recorder_open(const char* params) { return media_open(MEDIA_ID_RECORDER, params); } int media_recorder_close(void* handle) { return media_close(handle, 0); } int media_recorder_set_event_callback(void* handle, void* cookie, media_event_callback event_cb) { return media_set_event_cb(handle, cookie, event_cb); } int media_recorder_prepare(void* handle, const char* url, const char* options) { return media_prepare(handle, url, options); } int media_recorder_reset(void* handle) { if (!handle) return -EINVAL; media_close_socket(handle); return media_proxy_once(handle, NULL, "reset", NULL, 0, NULL, 0); } ssize_t media_recorder_read_data(void* handle, void* data, size_t len) { return media_process_data(handle, false, data, len); } int media_recorder_get_sockaddr(void* handle, struct sockaddr_storage* addr) { return media_get_sockaddr(handle, addr); } int media_recorder_get_socket(void* handle) { return media_get_socket(handle); } void media_recorder_close_socket(void* handle) { media_close_socket(handle); } int media_recorder_start(void* handle) { return media_proxy_once(handle, NULL, "start", NULL, 0, NULL, 0); } int media_recorder_pause(void* handle) { return media_proxy_once(handle, NULL, "pause", NULL, 0, NULL, 0); } int media_recorder_stop(void* handle) { if (!handle) return -EINVAL; media_close_socket(handle); return media_proxy_once(handle, NULL, "stop", NULL, 0, NULL, 0); } int media_recorder_set_property(void* handle, const char* target, const char* key, const char* value) { return media_proxy_once(handle, target, key, value, 0, NULL, 0); } int media_recorder_get_property(void* handle, const char* target, const char* key, char* value, int value_len) { return media_proxy_once(handle, target, key, NULL, 0, value, value_len); } int media_recorder_take_picture(char* params, char* filename, size_t number) { int ret = 0; MediaIOPriv* priv; if (!number || number > INT_MAX) return -EINVAL; priv = calloc(1, sizeof(MediaIOPriv)); if (!priv) return -ENOMEM; sem_init(&priv->sem, 0, 0); priv->cookie = media_recorder_start_picture(params, filename, number, media_recorder_take_picture_cb, priv); if (!priv->cookie) { free(priv); return -EINVAL; } sem_wait(&priv->sem); sem_destroy(&priv->sem); ret = priv->result; free(priv); return ret; } void* media_recorder_start_picture(char* params, char* filename, size_t number, media_event_callback event_cb, void* cookie) { char option[32]; void* handle = NULL; int ret; if (!number || number > INT_MAX) return NULL; handle = media_recorder_open(params); if (!handle) return NULL; ret = media_recorder_set_event_callback(handle, cookie, event_cb); if (ret < 0) goto error; snprintf(option, sizeof(option), "total_number=%zu", number); ret = media_recorder_prepare(handle, filename, option); if (ret < 0) goto error; ret = media_recorder_start(handle); if (ret < 0) goto error; return handle; error: media_recorder_close(handle); return NULL; } int media_recorder_finish_picture(void* handle) { return media_recorder_close(handle); }
最新发布
07-11
//IIC所有操作函数 void MAX30102_IIC_Init(void); //初始化IIC的IO口 void MAX30102_IIC_Start(void); //发送IIC开始信号 void MAX30102_IIC_Stop(void); //发送IIC停止信号 void MAX30102_IIC_Send_Byte(u8 txd); //IIC发送一个字节 u8 MAX30102_IIC_Read_Byte(unsigned char ack);//IIC读取一个字节 u8 MAX30102_IIC_Wait_Ack(void); //IIC等待ACK信号 void MAX30102_IIC_Ack(void); //IIC发送ACK信号 void MAX30102_IIC_NAck(void); //IIC不发送ACK信号 void MAX30102_IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data); void MAX30102_IIC_Read_One_Byte(u8 daddr,u8 addr,u8* data); void MAX30102_IIC_WriteBytes(u8 WriteAddr,u8* data,u8 dataLength); void MAX30102_IIC_ReadBytes(u8 deviceAddr, u8 writeAddr,u8* data,u8 dataLength); //MAX30102所有操作函数 void MAX30102_Init(void); void MAX30102_Reset(void); u8 M30102_Bus_Write(u8 Register_Address, u8 Word_Data); u8 max30102_Bus_Read(u8 Register_Address); void max30102_FIFO_ReadWords(u8 Register_Address,u16 Word_Data[][2],u8 count); void max30102_FIFO_ReadBytes(u8 Register_Address,u8* Data); void maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data); void maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data); void maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led); //心率血氧算法所有函数 void maxim_heart_rate_and_oxygen_saturation(uint32_t *pun_ir_buffer , int32_t n_ir_buffer_length, uint32_t *pun_red_buffer , int32_t *pn_spo2, int8_t *pch_spo2_valid , int32_t *pn_heart_rate , int8_t *pch_hr_valid); void maxim_find_peaks( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num ); void maxim_peaks_above_min_height( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height ); void maxim_remove_close_peaks( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance ); void maxim_sort_ascend( int32_t *pn_x, int32_t n_size ); void maxim_sort_indices_descend( int32_t *pn_x, int32_t *pn_indx, int32_t n_size); // void max301
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值