Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例

Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例

服务端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    int serv_sock, clnt_sock;

    struct sockaddr_in serv_addr, clnt_addr;
    socklen_t clnt_addr_size;

    if (argc != 2) {
        printf("Usage: %s <port>\n", argv[0]);
        exit(1);
    }

    serv_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (serv_sock < 0) {
        printf("sock() error\n");
        exit(1);
    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));

    int optval = 1;
    // setting address and port reusable
    setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

    if (bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("bind() error\n");
        exit(1);
    }

    if (listen(serv_sock, 5) < 0) {
        printf("listen() error\n");
        exit(1);
    }

    clnt_addr_size = sizeof(clnt_addr);
    clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr,
               &clnt_addr_size);

    if (clnt_sock < 0) {
        printf("accept() error\n");
        exit(1);
    }

    char buf[1024];
    printf("connected with ip: %s and port: %d\n",
           inet_ntop(AF_INET, &clnt_addr.sin_addr, buf, 1024),
           ntohs(clnt_addr.sin_port));

    while (1) {
        memset(buf, 0, sizeof(buf));
        ssize_t size = read(clnt_sock, buf, sizeof(buf) - 1);
        if (size < 0) {
            printf("read() error\n");
            break;
        } else {
            printf("client#: %s\n", buf);
        }
    }
    close(clnt_sock);
    close(serv_sock);
    return 0;
}

客户端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    int clnt_sock;
    struct sockaddr_in serv_addr;
    int str_len;

    if (argc != 3) {
        printf("Usage: %s <IP> <port>\n", argv[0]);
        exit(1);
    }

    clnt_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (clnt_sock < 0) {
        printf("sock() error\n");
        exit(1);
    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons(atoi(argv[2]));

    if (connect(clnt_sock,
            (struct sockaddr*)&serv_addr,
            sizeof(serv_addr)) == -1) {
        printf("connect() error\n");
        exit(1);
    }

    char buf[1024];

    while (1) {
        memset(buf, 0, sizeof(buf));
        printf("client enter: ");
        fflush(stdout);
        ssize_t size = read(0, buf, sizeof(buf) - 1);
        if (size < 0) {
            printf("read() error\n");
            break;
        } else {
            buf[size - 1] = '\0';
        }
        write(clnt_sock, buf, strlen(buf));
    }
    close(clnt_sock);
    return 0;
}

使用方法

编译:

$ gcc hello_server.c -o hello_server
$ gcc hello_client.c -o hello_client

打开终端:

$ ./hello_server 8888

打开另一个终端:

$ ./hello_client 127.0.0.1 8888

运行结果

客户端:

$ ./hello_client 127.0.0.1 8888
client please enter: 123
client please enter: 456
client please enter: 

服务端:

$ ./hello_server 8888
connected with ip: 127.0.0.1 and port: xxxx
client#: 123
client#: 456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值