龙空技术网

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

追光的Rocky 331

前言:

目前朋友们对“如何查看tcp连接”可能比较珍视,小伙伴们都想要分析一些“如何查看tcp连接”的相关内容。那么小编同时在网络上收集了一些关于“如何查看tcp连接””的相关内容,希望各位老铁们能喜欢,各位老铁们快快来了解一下吧!

下面是一个简单的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 socket\n");    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 %d\n", err);    err = listen(server_fd, 128);    if (err < 0) on_error("Could not listen on socket\n");    printf("Server is listening on %d\n", port);    printf("Server IP address is: %s\n", inet_ntoa(server.sin_addr));    printf("Server port is: %d\n", (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 %d\n", client_fd);        printf("Client IP address is: %s\n", inet_ntoa(client.sin_addr));        printf("Client port is: %d\n", (int) ntohs(client.sin_port));        if (client_fd < 0) on_error("Could not establish new connection\n");        while (1) {            int read = recv(client_fd, buf, BUFFER_SIZE, 0);            if (!read) break; // done reading            if (read < 0) on_error("Client read failed\n");            printf("server received %d bytes: %s", read, buf);            err = send(client_fd, buf, read, 0);            if (err < 0) on_error("Client write failed\n");        }    }    return 0;}

编译之后运行:

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

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

$ telnet localhost 12345

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

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

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

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

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

[root@localhost ~]# netstat -aonp|grep 12345tcp        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

标签: #如何查看tcp连接