multiple TCP connections

J

Jack

Hi c++ guru's
I need your help. I have 4 machines and I need them to be able to
communicate to each other via peer to peer architecture. I know that
when we have two machines one machine acts as a client and the other
one as a server. the server machine just listens for incoming
connections. My question is if we have 4 machines do I need to create
3 seperate connections codes and connection sockets on each machine.
for instance if we have two machines, the client has a code similar to
this:
socketChannel.sin_family = AF_INET;
socketChannel.sin_port = htons(PORT);
inet_pton(AF_INET, serverIP, &socketChannel.sin_addr); //PUT THE IP
ADDRESS OF THE SERVER TO CONNECT
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);

If I have 4 machines, will I need 3 more of these ie. 3 instances of
socketChannel, and sd plus another one of when each machine is a
server (in peer to peer, a machine is both a client and a server) and
basically 4 copies of this same code?

Thanks
 
V

Victor Bazarov

Jack said:
Hi c++ guru's
I need your help. I have 4 machines and I need them to be able to
communicate to each other via peer to peer architecture. [..]

It looks like you might want to have a list (or other container) of
those connections, instead of hard-coding a certain number of them.
I would probably use a 'vector', and while performance of appending
is not necessarily best, and removal (especially from the middle) is
definitely worse than, say, of 'list', you have the ability to index
within your collection. Another possibility is a 'map', where you
can store the connection information based on a name, an index, or
any other key you can sort on.

You need to do more designing before we can help you with the
language part, methinks.

V
 
B

bjeremy

Jack said:
Hi c++ guru's
I need your help. I have 4 machines and I need them to be able to
communicate to each other via peer to peer architecture. I know that
when we have two machines one machine acts as a client and the other
one as a server. the server machine just listens for incoming
connections. My question is if we have 4 machines do I need to create
3 seperate connections codes and connection sockets on each machine.
for instance if we have two machines, the client has a code similar to
this:
socketChannel.sin_family = AF_INET;
socketChannel.sin_port = htons(PORT);
inet_pton(AF_INET, serverIP, &socketChannel.sin_addr); //PUT THE IP
ADDRESS OF THE SERVER TO CONNECT
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);

If I have 4 machines, will I need 3 more of these ie. 3 instances of
socketChannel, and sd plus another one of when each machine is a
server (in peer to peer, a machine is both a client and a server) and
basically 4 copies of this same code?

Thanks

Well... if you want to do peer to peer connections, then each box
would need to have a server connection on it since each node can
potentially be a server.. so yes.. you would need 4 ServerSocket (or
whatever you name them) objects... but still you only need 1
ServerSocket class...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top