accept()创建一个新的套接字是什么意思? [英] What does it mean that accept() creates a new socket?

查看:362
本文介绍了accept()创建一个新的套接字是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基于以下理解

  • 套接字由ip:port定义.服务器和客户端都将拥有自己的套接字
  • 套接字连接由五个组定义,分别是server_ip,server_port,client_ip,client_port,协议
  • 套接字描述符是标识套接字的整数值

我试图实现自己的服务器,其中socket()和accept()返回不同的套接字描述符值(总是这样吗?).我的问题是,为什么说如果服务器中没有打开新端口,accept()会创建一个新的套接字,并且socket()和accept()返回的套接字描述符的ip:port都相同.如果新套接字是由accept()创建的,那么它与socket()创建的套接字有什么不同?

I tried to implement my own server where socket() and accept() returns different socket descriptor value (Is it always the case?). My question is why is it said that accept() creates a new socket if no new port is opened in server and ip:port is same for both the socket descriptors returned by socket() and accept(). If new socket is created by accept() how is it different than the socket created by socket()?

推荐答案

我试图实现自己的服务器,其中socket()accept()返回 不同的套接字描述符值(总是这样吗?).

I tried to implement my own server where socket() and accept() returns different socket descriptor value (Is it always the case?).

是的

我的问题是为什么为什么说accept()如果没有则创建一个新的套接字 服务器中打开了新端口,并且两个套接字的ip:port都相同 socket()accept()返回的描述符.如果是新插座 accept()创建的套接字与accept()创建的套接字有什么不同 socket()?

My question is why is it said that accept() creates a new socket if no new port is opened in server and ip:port is same for both the socket descriptors returned by socket() and accept(). If new socket is created by accept() how is it different than the socket created by socket()?

因为最初的套接字用于等待通信,而第二个套接字用于通信.呼叫socket(+ bind + listen)准备一个通信端点,也称为 socket (或服务器套接字)以接收传入的呼叫.在准备充分的通信点上对accept的呼叫,等待传入呼叫,并在发生这种情况时创建一个通信信道(2个端点+协议),该通信信道由已连接的套接字表示电话.

Because the initial socket is used to wait for communication while the second is used to communicate. A call to socket (+bind+listen) prepare a communication end-point, aka socket (or server socket) to receive incoming calls. A call to accept on a well prepared communication point, waits for an incoming call, and when this happens that creates a communication channel (2 end-points + protocol) represented by the connected socket returned by the call.

C API可能会使您感到困惑,因为它们都被称为套接字,但实际上并没有相同的用途.在其他一些语言/API中,进行了区分.例如,在Java中,有ServerSocket用于等待传入呼叫,而Socket用于通信.

C API may confuse you because both are called socket, but are really not for the same use. In some other languages/API differentiation is made. For example in Java you have ServerSocket that is used to wait for incoming calls, and Socket that are used to communicate.

这篇关于accept()创建一个新的套接字是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆