首页 文章

TCP双向还是全双工?

提问于
浏览
19

Bidirectionalfull-duplex 是不同的概念 . 例如, Ethernet 只是半双工,因为在特定时间,只有一个主机可以通过线路发送数据,并且它不能发送和接收数据 simultaneously .

因此,当我们通过以太网使用TCP时,我认为TCP只是双向或半双工 .

here它说TCP是全双工的 . 为什么?

5 回答

  • 4

    通过阅读您发布的文章,我认为's clear that they'谈论TCP支持全双工通信(强调我的):

    [TCP]是一种全双工协议,这意味着每个TCP连接都支持一对字节流,每个字节流在每个方向上流动 .

  • 3

    它当然是双向的,因为双方都发送/接收数据包 . 当您询问TCP是否为全双工时,您究竟是什么意思?

    发送和接收数据包 at the same time 都与物理组件有关,而TCP是一种协议,用于定义如何构建和处理数据以便到达目的地 .

    NIC(网络接口控制器)负责发送和接收物理数据包,您必须检查有关半/全双工功能的信息 .

    例如,如果无线(802.11)使用相同的天线来发送和接收无线电信号,则它是半双工的 .

  • 15

    这两者都是 . 它是双向的,因为它可以在两个方向上发送数据,并且它是全双工的,因为它可以在API级别同时执行此操作,而无需线路转换 .

    当然,在较低级别,它可能受到可用物理层的限制 .

  • 15

    TCP API是全双工的 . 这意味着TCP API允许同时从连接的两端发送数据 . 让我们看一下来源证明:

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <unistd.h>
    
    
    void do_write(const char* who, int socket) {
        const char hello[] = "hello!";
        if( 0 < write(socket, hello, strlen(hello)) )
            printf( "%s: write done ok\n", who );
        else
            printf( "%s: write error: %s\n", who, strerror(errno) );
    }
    
    void do_read(const char* who, int socket) {
        /* do parental things with this end, like reading the child's message */
        char buf[1024];
        int n = read(socket, buf, sizeof(buf));
        if( 0 < n )
            printf("%s: received '%.*s' %db\n", who, n, buf, n);
        else if( 0 == n )
            printf( "%s: no data available\n", who );
        else
            printf( "%s: read error: %s\n", who, strerror(errno) );
    }
    
    int main() {
        int fd[2];
        static const int parent = 0;
        static const int child = 1;
        pid_t pid;
    
        socketpair(PF_LOCAL, SOCK_STREAM, 0, fd);
    
        pid = fork();
        if (pid == 0) {      /* child process */
            close(fd[parent]);
            do_write("child", fd[child]);
            do_read("child", fd[child]);
            /* sleep(1); */
            do_write("child", fd[child]);
            do_read("child", fd[child]);
        } else {             /* parent process */
            close(fd[child]);
            do_write("parent", fd[parent]);
            do_read("parent", fd[parent]);
            do_write("parent", fd[parent]);
            do_read("parent", fd[parent]);
        }
    
        return 0;
    }
    

    输出(在FreeBSD上)是:

    父母:写完了好
    孩子:写得好
    孩子:收到'你好!' 6B
    孩子:写得好
    父母:收到'你好!你好!' 12B
    父母:写完了好
    孩子:收到'你好!' 6B
    父母:没有可用数据

    因此TCP API是全双工的,并且可以同时从两侧发送数据 . 我认为实现也是全双工,但它需要编写更复杂的测试来识别 . 这当然取决于实现 . 当至少一个传输链路不是全双工时,良好的实现可能不会产生影响 .

  • 2

    是的,TCP连接提供全双工服务 . 让我们理解全双工的含义 . 这意味着同时在两个实体之间交换数据(发送和接收) . 由于TCP是传输层协议,传输层协议提供在不同主机上运行的进程之间的逻辑通信,因此在这方面全双工的含义也是如此 .

    这里 full-duplex 表示 "If there is a TCP connection between Process A on one host and Process B on another host, then application layer data can flow from Process A to Process B at the same time as application layer data flows from Process B to Process A". TCP连接也总是 point-to-point ,即在单个发送者和单个接收者之间 . 请记住,来自进程A的数据尚未通过传输层下面的层,类似地,来自进程B的数据将通过传输层下面的层 .

    资料来源:Kurose,Ross的计算机网络 .

相关问题