一、问题引入

阅读UNIX网络编程 卷1:套接字联网API 第3版的前4个章节,觉得有必要对书籍上的源码案例进行复现,并推敲TCP的C/S通信过程。

二、解决过程

2-1 server

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>

#define PORT 8887
#define QUEUE 20
#define BUFFER_SIZE 1024

int main()
{
    // 定义sockfd, AF_INET:IPv4协议,SOCK_STREAM:字节流套接字
    int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    // 定义sockaddr_in
    struct sockaddr_in server_sockaddr;
    server_sockaddr.sin_family = AF_INET;
    server_sockaddr.sin_port = htons(PORT);
    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    // bind,成功返回0,出错返回-1
    if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)) == -1)
    {
        perror("bind failure");
        exit(1);
    }

    // listen,成功返回0,出错返回-1
    if (listen(server_sockfd, QUEUE) == -1)
    {
        perror("listen failure");
        exit(1);
    }

    // 客户端套接字
    char buffer[BUFFER_SIZE];
    struct sockaddr_in client_addr;
    socklen_t length = sizeof(client_addr);

    // 成功返回非负描述字,出错返回-1
    // accept()被阻塞,等待客户端连接,一旦客户端和服务器完成TCP三次握手,accept()返回一个已连接描述值
    int conn = accept(server_sockfd, (struct sockaddr *)&client_addr, &length);
    if (conn < 0)
    {
        perror("connect failure");
        exit(1);
    }

    printf("**************************n");
    printf("accept successful !!!n");
    char ipbuf[64] = {0};
    printf("client || ip: %s, port: %dn",
           inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, ipbuf, sizeof(ipbuf)),
           ntohs(client_addr.sin_port));
    printf("**************************n");
    while (1)
    {
        memset(buffer, 0, sizeof(buffer));
        // 接收
        int len = recv(conn, buffer, sizeof(buffer), 0);
        if (strcmp(buffer, "exit") == 0 || strcmp(buffer, "q") == 0)
            break;
        if(len > 0)
        {
            printf("Recv:%s (%d Byte)n", buffer, strlen(buffer));
        }
    }
    close(conn);
    close(server_sockfd);
    return 0;
}

2-2 client

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>

#define PORT 8887
#define BUFFER_SIZE 1024

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    /// 连接服务器,成功返回0,错误返回-1
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }
    printf("**************************n");
    printf("connect successful !!!n");
    char ipbuf[64] = {0};
    printf("server || ip: %s, port: %dn",
           inet_ntop(AF_INET, &servaddr.sin_addr.s_addr, ipbuf, sizeof(ipbuf)),
           ntohs(servaddr.sin_port));
    printf("now you can send message to servern");
    printf("**************************n");
    char sendbuf[BUFFER_SIZE];
    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
    {
        if(sendbuf[strlen(sendbuf)-1] == 'n')
            sendbuf[strlen(sendbuf)-1] = '';
        // 发送
        send(sockfd, sendbuf, strlen(sendbuf), 0);
        if (strcmp(sendbuf, "exit") == 0 || strcmp(sendbuf, "q") == 0)
            break;
        memset(sendbuf, 0, sizeof(sendbuf));
    }
    close(sockfd);
    return 0;
}

2-3 编译和运行测试

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/caojun97/p/17356425.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!