美文网首页
linux c设置socket超时时间

linux c设置socket超时时间

作者: 一路向后 | 来源:发表于2021-11-01 20:44 被阅读0次

1.源码实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define SERVER_PORT 8008
#define MESSAGE "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhello world"

int main()
{
    struct sockaddr_in srvaddr;
    struct timeval timeout = {10, 0};
    int locfd;
    int sockopt = 1;
    int res;

    /*创建一个套接字*/
    locfd = socket(AF_INET, SOCK_STREAM, 0);
    if(locfd < 0)
    {
        printf("create socket error!\n");
        return -1;
    }

    printf("socket ready!\n");

    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(SERVER_PORT);
    srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    setsockopt(locfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int));

    /*bind, 将网络地址与端口绑定*/
    res = bind(locfd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
    if(res < 0)
    {
        printf("bind error!\n");
        close(locfd);
        return -1;
    }

    printf("bind ready!\n");

    /*listen, 监听端口*/
    listen(locfd, 10);

    printf("等待来自客户端的连接......\n");

    while(1)
    {
        struct sockaddr_in cliaddr;
        socklen_t len = sizeof(cliaddr);
        int clifd;

        timeout.tv_sec = 0;

        if(setsockopt(locfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) == -1)
        {
            printf("set setsockopt recv time out error!\n");
            close(locfd);
            return -1;
        }

        /*bind, 将网络地址与端口绑定*/
        clifd = accept(locfd, (struct sockaddr *)&cliaddr, &len);
        if(clifd < 0)
        {
            printf("accept error! %s\n", strerror(errno));
            close(locfd);
            return -1;
        }

        /*输出客户机的信息*/
        char *ip = inet_ntoa(cliaddr.sin_addr);

        printf("客户机: %s连接到本服务器成功!\n", ip);

        timeout.tv_sec = 10;

        if(setsockopt(clifd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) == -1)
        {
            printf("set setsockopt send time out error!\n");
            close(locfd);
            return -1;
        }

        if(setsockopt(clifd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) == -1)
        {
            printf("set setsockopt recv time out error!\n");
            close(locfd);
            return -1;
        }

        /*输出客户机请求的信息*/
        char buff[1024] = {0};
        int size = read(clifd, buff, sizeof(buff));

        printf("Request information: ");

        printf("%s\n", buff);

        printf("%d bytes\n", size);

        write(clifd, MESSAGE, strlen(MESSAGE));

        close(clifd);
    }

    close(locfd);

    return 0;
}

2.编译源码

$ gcc -o httpd httpd.c -std=c89

3.运行及其结果

$ ./httpd
$ telnet 127.0.0.1 8008
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HTTP/1.1 200 OK
Content-Length: 11

hello worldConnection closed by foreign host.

相关文章

  • linux c设置socket超时时间

    1.源码实现 2.编译源码 3.运行及其结果

  • python3 socket 中设置超时的几种常用方法

    一,默认超时时间设置 socket.setdefaulttimeout(time) 参数time为超时时间,必须放...

  • Socket超时时间设置

    你知道在 Java 中怎么对 Socket 设置超时时间吗?他们的区别是什么?想一想和女朋友打电话的场景就知道了,...

  • httpclient参数配置

    序 这里简单解释一下httpclient一些关键参数的配置 超时时间 这里设置了socket_timeout以及c...

  • 网络编程

    Linux Socket编程(不限Linux) C/C++ socket编程教程:1天玩转socket通信技术 一...

  • 发包程序小记

    1、说说发包程序里面的timeout 设置因为socket receive函数,如果没有设置超时时间就会按照默认时...

  • httpclient 超时时间

    超时相关配置 HttpClient中可设置三个超时:RequestTimeout(连接池获取到连接的超时时间)、C...

  • 从linux源码看socket(tcp)的timeout

    # 从linux源码看socket(tcp)的timeout ## 前言 网络编程中超时时间是一个重要但又容易被忽...

  • java socket参数选项

    java socket参数选项主要用于控制socket通信过程。如超时,保持连接,缓存大小设置等。 TCP_NOD...

  • feign client设置超时时间

    feign client需要根据业务特点设置不同的超时时间 设置全局超时时间 设置具体服务的超时时间 参考 Spr...

网友评论

      本文标题:linux c设置socket超时时间

      本文链接:https://www.haomeiwen.com/subject/csdtzltx.html