summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/socket-api/wasm-src/timeout_server.c
blob: c71cf4553dc3edd94e940b374171926e7bb3a295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef __wasi__
#include <wasi_socket_ext.h>
#endif

int
main(int argc, char *argv[])
{
    int socket_fd;
    int client_socket_fd;
    struct sockaddr_in addr = { 0 };
    int addrlen = sizeof(addr);
    int bool_opt = 1;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(1234);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Create socket failed");
        return EXIT_FAILURE;
    }

    if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &bool_opt,
                   sizeof(bool_opt))
        == -1) {
        perror("Failed setting SO_REUSEADDR");
        goto fail;
    }

    if (bind(socket_fd, (struct sockaddr *)&addr, addrlen) == -1) {
        perror("Bind socket failed");
        goto fail;
    }

    if (listen(socket_fd, 1) == -1) {
        perror("Listen failed");
        goto fail;
    }

    if ((client_socket_fd =
             accept(socket_fd, (struct sockaddr *)&addr, (socklen_t *)&addrlen))
        == -1) {
        perror("Accept failed");
        goto fail;
    }

    printf("Client connected, sleeping for 10s\n");
    sleep(10);

    printf("Shuting down\n");
    shutdown(client_socket_fd, SHUT_RDWR);
    close(client_socket_fd);
    shutdown(socket_fd, SHUT_RDWR);
    close(socket_fd);
    return EXIT_SUCCESS;

fail:
    close(socket_fd);
    return EXIT_FAILURE;
}