Peer to Peer Questions
Posted: Thu May 24, 2012 6:13 pm
The Internet Chat.rev example program has a server and client
stack. Multiple client stacks can be opened, and all messages are relayed
by the server to all clients. I'm using this as a starting point for a
peer to peer chat project. So in my effort I have a couple of questions.
Maybe someone can point me in the right direction.
It seems creating sockets to send and receive are very different. For instance:
To receive: accept connections on port 1987 with message "chatConnected"
To send, open socket "localhost:1987" with message "chatConnected"
I tried using both commands, one after another, thinking that the port would then
be available for both sending and receiving. This just locked up my stack.
So you can't send and receive at the same time on the same port. Okay.
But in terms of setting up peer to peer communication without going through
an intermediary sever, here's my problem. Two computers could communicate
as long as the send and receive ports were reversed. But add in a few more
computers, and there's no way of knowing which way the ports need to be set.
So my thinking is that it would be best to use only one port, set to receive.
Upon sending, the receive connection would be closed, and then reopened
as a send port. Then after the message is sent, it would immediately be
put back into the receive state. This seems like a good solution. Right?
But what happens if there is a collision? Say two, three or four computers
are trying to send to the same machine at the same time. Or, when one
machine is receiving, and at the same time another tries to send to it.
stack. Multiple client stacks can be opened, and all messages are relayed
by the server to all clients. I'm using this as a starting point for a
peer to peer chat project. So in my effort I have a couple of questions.
Maybe someone can point me in the right direction.
It seems creating sockets to send and receive are very different. For instance:
To receive: accept connections on port 1987 with message "chatConnected"
To send, open socket "localhost:1987" with message "chatConnected"
I tried using both commands, one after another, thinking that the port would then
be available for both sending and receiving. This just locked up my stack.
So you can't send and receive at the same time on the same port. Okay.
But in terms of setting up peer to peer communication without going through
an intermediary sever, here's my problem. Two computers could communicate
as long as the send and receive ports were reversed. But add in a few more
computers, and there's no way of knowing which way the ports need to be set.
So my thinking is that it would be best to use only one port, set to receive.
Upon sending, the receive connection would be closed, and then reopened
as a send port. Then after the message is sent, it would immediately be
put back into the receive state. This seems like a good solution. Right?
But what happens if there is a collision? Say two, three or four computers
are trying to send to the same machine at the same time. Or, when one
machine is receiving, and at the same time another tries to send to it.