Rehab Reda
Rehab Reda

Reputation: 193

UDB peer to peer on the same machine

I want to make sure that I understand the concept we have example.exe and we will run it 5 times or more on the same computer

example.exe will create a pear (which has a sending and receiving socket) and the sending socket sends on a fixed port and the receiving socket receive on the same port

my question is that when running example.exe 5 times 5 peers will be created on the same "machine" with the same fixed port !!! how that comes ! and i tried the code and it is working on the machine which means the same Ip address!

here are some code

Receiving Socket

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);

Sending Socket

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);
}

Upvotes: 1

Views: 79

Answers (0)

Related Questions