插座连接()与bind()的 [英] socket connect() vs bind()

查看:93
本文介绍了插座连接()与bind()的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者连接()绑定()系统调用'准'的套接字文件描述符到一个地址(典型的IP /端口组合)。他们的原型是这样的: -

Both connect() and bind() system calls 'associate' the socket file descriptor to an address (typically an ip/port combination). Their prototypes are like:-

int connect(int sockfd, const struct sockaddr *addr,
               socklen_t addrlen);

int bind(int sockfd, const struct sockaddr *addr,
            socklen_t addrlen);

什么是2调用之间准确的区别?当一个人应该使用连接()绑定()

具体而言,在一些示例服务器的客户端codeS,发现客户端使用连接()和服务器使用的绑定() 电话。原因并不完全清楚,我。

Specifically, in some sample server client codes, found that client is using connect() and server is using the bind() call. Reason was not fully clear to me.

推荐答案

若要更好地理解,让我们找出确切结合并连接进入画面,

To make understanding better , lets find out where exactly bind and connect comes into picture,

继两个呼叫的定位,如通过Sourav澄清,

Further to positioning of two calls , as clarified by Sourav,

bind()的套接字与本地地址相关联[这就是为什么服务器端绑定,以便客户端可以使用该地址连接到服务器。]
connect()可用于连接到远程[服务器]地址,这就是为什么是客户端,连接[为已读:连接到服务器]用于

bind() associates the socket with its local address [that's why server side binds, so that clients can use that address to connect to server.] connect() is used to connect to a remote [server] address, that's why is client side, connect [read as: connect to server] is used.

我们不能互换使用它们(即使我们有客户端相同的机器上机/服务器),因为具体的角色和相应的实施。

We cannot use them interchangeably (even when we have client/server on same machine) because of specific roles and corresponding implementation.

我将进一步建议这些电话TCP / IP握手相关。

I will further recommend to correlate these calls TCP/IP handshake .

那么,谁将会在这里发送SYN,这将是连接()。而结合()被用于定义通信结束点

So , who will send SYN here , it will be connect() . While bind() is used for defining the communication end point.

希望这有助于!

这篇关于插座连接()与bind()的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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