<返回更多

通过exec命令打开一个TCP连接

2021-06-21    追光的Rocky
加入收藏

下面是一个简单的echo服务程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024
#define on_error(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); exit(1); }

int main(int argc, char *argv[]) {
    if (argc < 2) on_error("Usage: %s [port]n", argv[0]);
    
    int port = atoi(argv[1]);

    int server_fd, client_fd, err;
    struct sockaddr_in server, client;
    char buf[BUFFER_SIZE];

    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd < 0) on_error("Could not create socketn");

    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);

    int opt_val = 1;
    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);

    err = bind(server_fd, (struct sockaddr *) &server, sizeof(server));
    if (err < 0) on_error("Could not bind socket %dn", err);

    err = listen(server_fd, 128);
    if (err < 0) on_error("Could not listen on socketn");

    printf("Server is listening on %dn", port);
    printf("Server IP address is: %sn", inet_ntoa(server.sin_addr));
    printf("Server port is: %dn", (int) ntohs(server.sin_port));

    while (1) {
        socklen_t client_len = sizeof(client);
        client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len); 
        printf("get a connection %dn", client_fd);
        printf("Client IP address is: %sn", inet_ntoa(client.sin_addr));
        printf("Client port is: %dn", (int) ntohs(client.sin_port));

        if (client_fd < 0) on_error("Could not establish new connectionn");

        while (1) {
            int read = recv(client_fd, buf, BUFFER_SIZE, 0);

            if (!read) break; // done reading
            if (read < 0) on_error("Client read failedn");

            printf("server received %d bytes: %s", read, buf);

            err = send(client_fd, buf, read, 0);
            if (err < 0) on_error("Client write failedn");
        }

    }

    return 0;
}

编译之后运行:

[root@localhost opt]# ./a.out 12345
Server is listening on 12345
Server IP address is: 0.0.0.0
Server port is: 12345

然后一般通过telnet的方式来连接测试:

$ telnet localhost 12345

还有一种测试方式,就是通过exec命令来打开一个tcp连接:

[root@localhost dev]# exec 6<>/dev/tcp/localhost/12345

执行上面的命令后,echo服务程序的日志输出:

get a connection 4
Client IP address is: 127.0.0.1
Client port is: 46476

通过netstat命令查看TCP连接情况:

[root@localhost ~]# netstat -aonp|grep 12345
tcp        0      0 0.0.0.0:12345           0.0.0.0:*               LISTEN      2511/./a.out         off (0.00/0/0)
tcp        0      0 127.0.0.1:46476         127.0.0.1:12345         ESTABLISHED 1335/-bash           off (0.00/0/0)
tcp        0      0 127.0.0.1:12345         127.0.0.1:46476         ESTABLISHED 2511/./a.out         off (0.00/0/0)

可以看到连接已经建立。

通过下面的命令,进行写数据:

[root@localhost dev]# echo "Write this to the socket" >&6

echo服务程序打印日志如下:

server received 25 bytes: Write this to the socket
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>