TCP Socket demo

socket_server.c

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

#define IPV4_TCP_SOCKET_PORT    3456

int main(int argc, char* argv[]) {
    int serv_fd, conn_fd;
    struct sockaddr_in server_addr, client_addr;
    memset(&server_addr, 0, sizeof(server_addr));

    /* 获取用于接受连接的server socket */
    serv_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (serv_fd < 0) {
        perror("socket create failed");
        exit(EXIT_FAILURE);
    }

    /* 设置服务器地址和端口 */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(IPV4_TCP_SOCKET_PORT);
    // server_addr.sin_addr.s_addr = inet_addr("192.168.1.1");
    server_addr.sin_addr.s_addr = INADDR_ANY;

    /* 将socket与服务器地址绑定 */
    if (bind(serv_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
        perror("socket bind failed");
        exit(EXIT_FAILURE);
    }

    /* 监听连接 */
    if (listen(serv_fd, 1) < 0) {
        perror("socket listen failed");
        exit(EXIT_FAILURE);
    }

    socklen_t client_addr_len = sizeof(client_addr);

    /* 接受连接 */
    if ((conn_fd = accept(serv_fd, (struct sockaddr *)&client_addr, &client_addr_len)) < 0) {
        perror("socket accept failed");
        exit(EXIT_FAILURE);
    }

    char buf[30];

    if (recv(conn_fd, buf, sizeof(buf), 0) < 0) {
        perror("socket recv failed");
        exit(EXIT_FAILURE);
    }

    printf("server recv data: [%s]\n", buf);

    if (send(conn_fd, buf, sizeof(buf), 0) < 0) {
        perror("socket send failed");
        exit(EXIT_FAILURE);
    }

    close(conn_fd);
    close(serv_fd);

    printf("server ended successfully\n");
    exit(EXIT_SUCCESS);
}

 

socket_client.c

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

#define IPV4_TCP_SOCKET_PORT    3456

int main(int argc, char* argv[]) {
    int client_fd, conn_fd;
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(IPV4_TCP_SOCKET_PORT);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    client_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (client_fd < 0) {
        perror("socket create failed.");
        exit(EXIT_FAILURE);
    }

    if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
        perror("socket connect failed");
        exit(EXIT_FAILURE);
    }

    char buf[] = "hello socket";

    if (send(client_fd, buf, sizeof(buf), 0) < 0) {
        perror("socket send failed");
        exit(EXIT_FAILURE);
    }

    if (recv(client_fd, buf, sizeof(buf), 0) < 0) {
        perror("socket recv failed");
        exit(EXIT_FAILURE);
    }

    printf("client recv response: [%s]\n", buf);

    /* 关闭socket */
    close(client_fd);

    printf("client ended successfully\n");
    exit(EXIT_SUCCESS);
}

编译:

gcc socket_server.c -o server
gcc socket_client.c -o client

 

posted @ 2021-02-10 15:42  代萌  阅读(393)  评论(0编辑  收藏  举报