Communication among multiple instances of a program in peer to peer chat - C language

StackOverflow https://stackoverflow.com/questions/22547402

  •  18-06-2023
  •  | 
  •  

Question

I'm writing a peer to peer chat program. In this program, client and server functionality are written in one unique file. First, I would like to ask whether the mechanism in my program is correct or not.

  1. I fork() two processes, one for client functionality and one for server functionality
  2. In server process, I initialize sockets, do bind(), listen(). Then I use select() to handle for multi-connections from clients. In client process, I initialize socket, do connect().
  3. When the first peer is created, the client process doesn't have any server to connect with, so it just waits until other peers appear. In this case, I just initialize a socket and wait.
  4. When next peers are created, their client processes initialize sockets, do connect() and send information to server process of the first peer to ask for joining.
  5. In server process, I receive information after select() returns and send a feedback to the client process of sender.

Second, for example, my program is called "p2p". I start an instance(from my program) in one terminal, let say, P2P1 by running "p2p Name_of_group Port_Number_for_Server_Side". This is the first peer of the network, it defines a name of a group, a port to listen. Now I run another instance (in another terminal), let say P2P2 by running "p2p Name_of_group Port_Number_for_Server_Side". This second peer states the name of a group it would like to join and a port to listen for its server process.

A client process of P2P2 connects to a server process of P2P1 by sending its information to the server process P2P1. However, in this case, the client process of P2P2 doesn't know the value of socket which is initialized to open a port in server process of P2P1. So my question is how to get this information from P2P1 when the P2P2 wants to communicate with P2P1 using send() and recv()? Thanks very much for your reply.

Was it helpful?

Solution

First question: I would prefer to use one single process instead of two since data exchange would be simpler. Then choosing between a single thread solution (select()ing with no timeout from network and from user input in a loop) or a two threads solution I would probably stay for 2 threads, which is a little bit more complicated but more responsive to the user.

The rest of mechanism seems correct but may vary according to the implementation required by yor second question

Second question: I think that you always need to pass the hostname:port of at least one active client as argument.

I would develop this solution: if hostname:port are not passed then the client just does nothing (it's probably the first node of a network); if such info is present, then as first thing the client should try to connect to the given address.

The first contact should be followed by a special command that makes the contacted server to share info of every other active node in the network. In your case I would start P2P2 as "p2p Name_of_group Port_Number_for_Server_Side Port_Number_of_P2P1". Then

  • P2P2 connects to P2P1 and ask for other nodes
  • P2P1 answers that is is the only node
  • now both P2P1 and P2P2 knows that the network is made of two nodes

Now you can start a P2P3 as "p2p Name_of_group Port_Number_for_Server_Side Port_Number_of_P2P1" or "p2p Name_of_group Port_Number_for_Server_Side Port_Number_of_P2P2". Let's assume you make it connect to P2P2

  • P2P3 connects to P2P2 and asks for other active nodes
  • P2P2 replies address info about P2P1
  • P2P3 connects to P2P1 and do not ask for anything (or you can be redundant if you wish)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top