1

我想确保我理解我们拥有 example.exe 的概念,并且我们将在同一台计算机上运行它 5 次或更多次

example.exe 将创建一个 pear(具有发送和接收套接字),发送套接字在固定端口上发送,接收套接字在同一端口上接收

我的问题是,当运行 example.exe 5 次时,将在具有相同固定端口的同一“机器”上创建 5 个对等点!!!怎么来的!我尝试了代码,它在机器上运行,这意味着相同的 IP 地址!

这是一些代码

接收插座

sockaddr_in RecvAddr;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(PORT);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

cout << RecvAddr.sin_addr.s_addr << endl;


int broadcastValue = 1;
if (setsockopt(receivingSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&broadcastValue, sizeof(broadcastValue)) == SOCKET_ERROR)
    exit(EXIT_FAILURE);

int iResult = 0;
iResult = bind(receivingSocket, (SOCKADDR *)& RecvAddr, sizeof (RecvAddr));
if (iResult != 0) {
    exit(EXIT_FAILURE);

发送套接字

sockaddr_in destinationAddress;
destinationAddress.sin_family = AF_INET;
destinationAddress.sin_addr.s_addr = INADDR_BROADCAST;
destinationAddress.sin_port = htons((unsigned short)PORT);

if (sendto(sendingSocket, message.c_str(), message.length(), 0, (struct sockaddr*) &destinationAddress, sizeof(destinationAddress)) == SOCKET_ERROR)
{

    exit(EXIT_FAILURE);
}
4

0 回答 0