I am starting on a network-programming assignment I have. The instructions are as follows:
a. Create
nTCP clients and servers (together, the client and server on the
same host form a peer), wherenis an input parameter.b. Assign a virtual host name for each peer, e.g, Peer1, Peer2, etc.
c. Create an additional server that will act as the central server for
all peers.d. Have all
npeers connect to the central server, sending it a
“Hello” message.e. Every second, have the central server send to a random selection of
n/2peers the name of another, random peer.f. The peers which have been contacted by the central server then
connect to the peer they have been given, and send it a random
number, which is echoed back.e.g. for 2 peers:
Peer1: sending “Hello” to Central.
Central: received “Hello” from Peer 1.
Peer2: sending “Hello” to Central.
Central: received “Hello” from Peer 2.
Central: sending “Peer2” to Peer1.
Peer1: received “Peer2” from Central.
Peer1: sending “5890245” to
Peer2.Peer2: received “5890245” from Peer1.
Peer2: sending “5890245”
to Peer1.Peer1: received “5890245” from Peer2.
. . .
I am confused by the “virtual hostname” instruction in step b. What does it mean?
For the server, in Python I can do this:
hostname = #????
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind((hostname, 50008))
mySocket.listen(1)
If hostname is ‘localhost’, or ”, it will only listen to connections from this machine. But if I try to give a name like ‘Peer1’, I get:
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
presumably because it tries to connect to the host named “Peer1”, which doesn’t exist.
So how can I send one of my peers the “name” of another, and have it connect, if they’re all on my machine? My thoughts are that I’d just have to give them a port number as well, and have them all be “localhost”, but that doesn’t seem to be what is asked for.
If you have a language-agnostic, or other-language-specific answer, that would be just as appreciated! Thanks!
The assignment is telling you to do the following:
Create a group of client/server pairs. Each pair represents a single Peer, where the number of Peers is specified by the user. Assign a unique name (aka the virtual hostname) to each Peer that you create.
Then have each Peer’s server socket open a listening port, and its client socket connect to the central server. One connected, the client socket sends its assigned Peer name to the server, so the server can keep a list of all available Peers.
Then every second, the central server grabs a random selection of half of the connctions from its list and, for each connection it pulls out, writes the name of a random Peer to that conecton.
On the receiving side, when a Peer’s client socket receives a name from the central server, create a separate connection to the server socket that is running on the specified Peer. That means the central server has to keep track of which IP belongs to which Peer (which it can grab when a Peer connects to it), and include that IP in the message that it sends out, so that the receiver knows where to connect to.
Once that separate connection has been established, write a random number to it and wait for it to be echoed back.
When a Peer’s server socket receives a message, echo it back to the client it came from.