Linux C Socket处理

2013-09-19 11:52  1015人阅读  评论 (0)
Tags: csocket

相关结构

struct sockaddr_in {
        __uint8_t       sin_len;
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        char            sin_zero[8];
};
struct sockaddr {
        __uint8_t       sa_len;         /* total length */
        sa_family_t     sa_family;      /* [XSI] address family */
        char            sa_data[14];    /* [XSI] addr value (actually larger) */
};

服务器

#require <stdio.h>
#require <string.h>
#require <sys/socket.h>
#require <netinet/in.h>
#require <arpa/inet.h>

// 最大数据大小
#define MAXDATASIZE 1024
// 服务器监听端口
#define SERVERPORT 8000
// 最大连接数
#define MAXCONN_NUM 10

int main() {
    // 申请socket
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "socket error\n");
        return 1;
    }

    // 服务器地址
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(struct sockaddr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVERPORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    // 将server绑定到socket
    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        fprintf(stderr, "bind error\n");
        return 1;
    }

    // 监听端口
    if (listen(sockfd, MAXCONN_NUM) == -1) {
        fprintf(stderr, "listen error\n");
        return 1;
    }

    // 循环接收客户端请求
    while (1) {
        // 初始化客户端地址
        int new_fd;
        struct sockaddr_in client_addr;
        memset(&client_addr, 0, sizeof(struct sockaddr));
        int sin_size = sizeof(struct sockaddr_in);

        // 等待客户端连接
        if ((new_fd= accept(sockfd, (struct sockaddr *)&client_addr, &sin_size)) == -1) {
            fprintf(stderr, "accept error\n");
            continue;
        }

        // 客户端连接成功
        printf("new connection: %s:%u\n", inet_ntoa(client_addr.sin_addr), client_addr.sin_port);

        // 读取数据
        int numbytes;
        char buf[MAXDATASIZE];
        if ((numbytes = recv(new_fd, buf, MAXDATASIZE, 0)) == -1) {
            fprintf(stderr, "recv error\n");
            continue;
        }

        // 打印数据
        if (numbytes) {
            buf[numbytes] = '\0';
            printf("recv: %s\n", buf);
            sleep(3);
        }

        // 发送数据
        printf("send: dotcoo\n");
        if (send(new_fd, "dotcoo", 7, 0) == -1) {
            fprintf(stderr, "send error\n");
            continue;
        }

        // 关闭连接
        printf("close connection: %s\n", inet_ntoa(client_addr.sin_addr));
        close(new_fd);
    }

    return 0;
}

客户端

#require <stdio.h>
#require <string.h>
#require <sys/socket.h>
#require <netinet/in.h>


// 最大数据大小
#define MAXDATASIZE 1024
// 服务器地址
#define SERVERIP "127.0.0.1"
// 服务器端口
#define SERVERPORT 8000

int main() {
    // 申请socket
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "socket error\n");
        return 1;
    }

    // 服务器地址
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(struct sockaddr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVERPORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVERIP);

    // 连接到服务器
    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        fprintf(stderr, "connect error\n");
        return 1;
    }

    // 发送数据
    printf("send: server hello\n");
    if (send(sockfd, "server hello", 13, 0) == -1) {
        fprintf(stderr, "send error\n");
        return 1;
    }

    // 读取数据
    int numbytes;
    char buf[MAXDATASIZE];
    if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
        fprintf(stderr, "recv error\n");
        return 1;
    }

    // 打印数据
    if (numbytes) {
        buf[numbytes] = '\0';
        printf("recv: %s\n", buf);
    }

    // 关闭连接
    close(sockfd);
    return 0;
}