English 中文(简体)
POSIX 袖珍 演习
原标题:POSIX Sockets bind() Exercise

I am trying to solve the following introductory POSIX Sockets exercise:

笔录

(我假定“制度”是指本组织。)

我不理解的是,如果连接(如果我不正确的话)以上述方式履行约束义务,那么(如果我不正确的话,我就认为)要求是必需的。 而且,为了向要求完成这种行为的受约束者提供袖珍地址,还需要什么价值——第6号——补充和第6号——?

问题回答

您是正确的,无需打bind。 该系统将在<条码>联接/代码>时选择源地址和源港。 页: 1

这是一项工作。 它只是检验你是否知道如何使用<条码>bind。

I do not understand why a bind() call is necessary when connect() performs the bind in the aforementioned way (correct me if I am incorrect).

你是正确的。

Also, what values of sin6_addr and sin6_port are necessary for the socket address provided to the bind() call to accomplish this behavior?

INADDR_ANY for IPv4. 我将把IPv6作为读者的一种做法。

You should also specify zero as the port number, then the system will choose.

EDIT:这与所要求的情况相反。 我认为,问题在于用户<>选择IP地址和港口。 由于欧洲司法论坛指出了这一点。


This should be what you re looking for.

这应当是最有意义的部分:

clnt_addr.sin_family = AF_INET;
clnt_addr.sin_addr.s_addr = INADDR_ANY;
clnt_addr.sin_port = htons(33333);
if (bind(sockfd, (struct sockaddr *) &clnt_addr,
          sizeof(clnt_addr)) < 0) 
          error("ERROR on binding");

举例来说,在连接之前,单列单只与预期港口(33333)有关,但在你看来,你可以将上述代码第二行改为:

clnt_addr.sin_addr.s_addr = inet_addr("192.168.0.1");

to have the socket bound also on a specific IP address. What matters here is that binding the socket before the connect() sets your endpoint of the TCP/IP communication to use the desired IP address/port. Without the bind() your socket would have been unbounded: the kernel would have chosen the source IP address for you and would have assigned a random source port to your socket.





相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

Listen to a port that is in use [duplicate]

Possible Duplicate: Get connecting IP from specified ports that using by other program. If a port is used by a program, is there any way I can listen that port and get the connected IP on that ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。