首先我们先了解一下什么是半双工通信?

半双工数据传输允许数据在两个方向上传输,但是在某一时刻,只允许数据在一个方向上传输,它实际上是一种切换方向的单工通信。

TCP服务端代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h> 
#include <netinet/in.h> 
#include <stdint.h>     
#include <sys/socket.h>  
#include <sys/types.h> 
#include <unistd.h>  

#define PORT 8100 

/* 打印错误信息 */
void error()
{
    perror("Socket Creation Failed");
    exit(EXIT_FAILURE);
}

int main()
{
    uint32_t sockfd,conn;  
    char server_msg[10000],client_msg[10000];  
    struct sockaddr_in server_addr,client_addr;  

    /* 创建流式套接字 */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        error();  
    }

    /* 服务端绑定地址和端口 */
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;   
    server_addr.sin_port = htons(PORT); 
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    printf("Server is running...\n");

    /* 绑定socket到服务端 */
    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        error();  /// If binding is unsuccessful
    }

    /* 监听客户端,监听的数量上限,我们这里设置为5 */
    listen(sockfd, 5);

    printf("Server is listening...\n");

    /* 当服务端发现有新的连接请求,服务端创建一个socket描述符响应accept,用来发送和接收数据 */
    conn = accept(sockfd, (struct sockaddr *)NULL, NULL);

    printf("Server is connected...\n");

    while (1)
    {
        bzero(&server_msg, sizeof(server_msg));
        bzero(&client_msg, sizeof(client_msg));

        /* 发送消息 */
        printf("\nEnter message here: ");
        fgets(server_msg, 10000, stdin);
        send(conn, server_msg, strlen(server_msg) + 1, 0);

        /* 接收消息 */
        recv(conn, client_msg, sizeof(client_msg), 0);
        printf("\nClient Message: %s\n", client_msg);
    }

    /* 关闭socket */
    close(sockfd);
    printf("Server is offline...\n");
    return 0;
}

服务端运行状态:

基于TCP的半双工网络编程实践-LMLPHP

TCP客户端代码:

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>
#include <netdb.h> 
#include <netinet/in.h>  
#include <stdint.h>       
#include <sys/socket.h> 
#include <sys/types.h>  
#include <unistd.h>  

#define PORT 8100  

void error()
{
    perror("Socket Creation Failed");
    exit(EXIT_FAILURE);
}

int main()
{
    uint32_t sockfd;
    struct sockaddr_in server_addr; 
    char serverResponse[10000],clientResponse[10000]; 

    /* 客户端创建socket */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        error();
    }

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    printf("Client is running...\n");

    /* 连接服务端 */
    connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));

    printf("Client is connected...\n");

    while (1)
    {
        bzero(&serverResponse, sizeof(serverResponse));
        bzero(&clientResponse, sizeof(clientResponse));

        /* 接收消息 */
        recv(sockfd, serverResponse, sizeof(serverResponse), 0);
        printf("\nServer message: %s \n", serverResponse);

        /* 发送消息 */
        printf("\nEnter message here: ");
        fgets(clientResponse, 10000, stdin);
        send(sockfd, clientResponse, strlen(clientResponse) + 1, 0);
    }

    /* 关闭socket */
    close(sockfd);
    printf("Client is offline...\n");
    return 0;
}

客户端运行状态:

基于TCP的半双工网络编程实践-LMLPHP

【欢迎关注编码小哥,学习更多实用的编程方法】

01-18 11:30