linux系统编程(十③)RK3568 socket之 TCP 服务器的实现【更新客户端断开重连依旧可以收发】

这里对上一篇:https://blog.csdn.net/u012507643/article/details/153483336?spm=1011.2124.3001.6209

服务器编程再客户端断开重连后不能响应的解决办法

直接上程序【这里解决的思路还是和tcp客户端连接服务器的方式一样,用MSG_PEEK去看下recv】

复制代码
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/tcp.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>




int main(int argc, char *argv[])
{
    int fd, client_fd;
    struct sockaddr_in s;
    struct sockaddr_in c;
    int len = sizeof(c), rlen = 0;
    char rx_buf[1024] = {0};

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
    {
        printf("create socekt failed\n");
        return 1;
    }

    s.sin_family = AF_INET;
    s.sin_port = htons(8089);
    //s.sin_addr.s_addr = INADDR_ANY;
    inet_pton(AF_INET, "192.168.31.96", &s.sin_addr);
    bind(fd, (const struct sockaddr *)&s, sizeof(s));

    printf("start listen\n");
    if (listen(fd, 10) < 0) {
        printf("listen failed\n");
        return 1;
    }

    printf("server ip:192.168.31.96,port:8089\n");

    /* 返回连接上来的套接字 */
    client_fd = accept(fd, (struct sockaddr *)&c, &len);
    if (client_fd < 0)
    {
        printf("sccept failed\n");
        return 1;
    }
    printf("new client %d connected\n", client_fd);
    while(1)
    {
        int ret = recv(client_fd, rx_buf, sizeof(rx_buf), MSG_PEEK);
        if ((ret > 0) || (ret == -1 && errno == EAGAIN))
        {
            rlen = recvfrom(client_fd, rx_buf, sizeof(rx_buf), MSG_DONTWAIT, (struct sockaddr *)&c, &len);
            if (rlen > 0)
            {
                printf("recv[%s:%d] data:%s,len=%d\n", inet_ntoa(c.sin_addr), ntohs(c.sin_port), rx_buf, rlen);
                send(client_fd, rx_buf, rlen, MSG_DONTWAIT);
                memset(rx_buf, 0, sizeof(rx_buf));
            }
        } else {
            printf("clinet %d disconnectted\n", client_fd);
            close(client_fd);
            len = sizeof(c);
            /* 返回连接上来的套接字 */
            client_fd = accept(fd, (struct sockaddr *)&c, &len);
            if (client_fd < 0)
            {
                printf("sccept failed\n");
                return 1;
            } else {
                printf("new clinet fd=%d connectted\n", client_fd);
            }
        }
    }

    close(fd);
    close(client_fd);
    return 0;
}
相关推荐
hz5678926 分钟前
涉密视频会议设备配置指南:终端、音视频采集与配套设施选型
服务器·网络·数据库·安全·实时音视频·信息与通信·智能硬件
广州浮点FLOATLIC31 分钟前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
优橙教育35 分钟前
零基础学AI应用开发要多久?3个月能到什么水平
服务器·开发语言·网络·php
BLUcoding1 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK1 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2081 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)1 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6251 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
zhengqweasd1 小时前
机房托管服务器怎么选硬盘,机械盘和 SSD 托管场景差异
运维·服务器·github
xh didida3 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++