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

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

服务端代码

#include 
#include 
#include 
#include 
#include 
#include 

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 \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 
#include 
#include 
#include 
#include 
#include 

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

    if (argc != 3) {
        printf("Usage: %s  \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

  • 我的个人主页:http://www.techping.cn/
  • 我的个人站点博客:http://www.techping.cn/blog/wordpress/
  • 我的CSDN博客:http://blog.csdn.net/techping
  • 我的:http://www.jianshu.com/users/b2a36e431d5e/timeline
  • 我的GitHub:https://github.com/techping
    欢迎相互follow~

你可能感兴趣的:(Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例)