libcurl socket连接

2021-03-10  本文已影响0人  一路向后

1.server部分源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>

int main()
{
    struct sockaddr_in srv_addr;
    struct sockaddr_in cli_addr;
    int srv_fd, cli_fd;
    int srv_len, cli_len;
    int ret;
    fd_set readfds, testfds;

    srv_fd = socket(AF_INET, SOCK_STREAM, 0);
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    srv_addr.sin_port = htons(8080);

    srv_len = sizeof(srv_addr);

    bind(srv_fd, (struct sockaddr *)&srv_addr, srv_len);
    listen(srv_fd, 5);

    FD_ZERO(&readfds);
    FD_SET(srv_fd, &readfds);

    while(1)
    {
        char ch;
        int fd;
        int nread;

        testfds = readfds;

        printf("server waiting\n");

        ret = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
        if(ret < 1)
        {
            perror("server 5\n");
            exit(1);
        }

        for(fd=0; fd<FD_SETSIZE; fd++)
        {
            if(FD_ISSET(fd, &testfds))
            {
                if(fd == srv_fd)
                {
                    cli_len = sizeof(cli_addr);
                    cli_fd = accept(srv_fd, (struct sockaddr *)&cli_addr, &cli_len);
                    FD_SET(cli_fd, &readfds);
                    printf("adding client on fd %d\n", cli_fd);
                }
                else
                {
                    ioctl(fd, FIONREAD, &nread);

                    if(nread == 0)
                    {
                        close(fd);
                        FD_CLR(fd, &readfds);
                        printf("removing client on fd, %d\n", fd);
                    }
                    else
                    {
                        read(fd, &ch, 1);
                        sleep(5);
                        printf("serving client on fd %d\n", fd);
                        ch++;
                        write(fd, &ch, 1);
                    }
                }
            }
        }
    }

    return 0;
}

2.client部分源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

int main()
{
    CURL *curl = NULL;
    CURLcode res = 0;
    curl_socket_t sockfd;
    size_t iolen;
    long sockextr;

    char ch = 'A';
    char s = ' ';

    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1");
        curl_easy_setopt(curl, CURLOPT_PORT, 8080);
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);

        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
        {
            printf("error1: %s\n", strerror(res));
            return -1;
        }

        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
        if(res != CURLE_OK)
        {
            printf("error2: %s\n", strerror(res));
            return -1;
        }

        sockfd = (curl_socket_t)sockextr;

        res = curl_easy_send(curl, &ch, 1, &iolen);
        if(res != CURLE_OK)
        {
            printf("error3: %s\n", strerror(res));
            return -1;
        }

        iolen = 0;

        do {
            res = curl_easy_recv(curl, &s, 1, &iolen);
            if(res != CURLE_OK && res != CURLE_AGAIN)
            {
                printf("error4: %s\n", strerror(res));
                return -1;
            }
        }
        while(res == CURLE_AGAIN);

        printf("the first time: char from server = %c\n", s);

        ch = 'B';

        sleep(5);

        res = curl_easy_send(curl, &ch, 1, &iolen);
        if(res != CURLE_OK)
        {
            printf("error5: %s\n", strerror(res));
            return -1;
        }

        do {
            res = curl_easy_recv(curl, &s, 1, &iolen);
            if(res != CURLE_OK && res != CURLE_AGAIN)
            {
                printf("error6: %s\n", strerror(res));
                return -1;
            }
        }
        while(res == CURLE_AGAIN);

        printf("the second time: char from server = %c\n", s);

        curl_easy_cleanup(curl);
    }

    return 0;
}

3.编译源码

$ gcc -o server server.c
$ gcc -o client client.c -L$HOME/local/lib -lcurl

4.运行程序及其结果

$ ./server
server waiting
adding client on fd 4
server waiting
serving client on fd 4
server waiting
serving client on fd 4
server waiting
removing client on fd, 4
server waiting
$ ./client
the first time: char from server = B
the second time: char from server = C
上一篇 下一篇

猜你喜欢

热点阅读