0

I have a network consists of 5 machines. One machine called leader (server), other called followers (clients). I use ServerSocket in leader side (to make it server) and use Socket in follower side to make them clients. This structure works perfectly.

Now, I want to add new change to above network. I would like to allow follower (clients) to send a message to others followers. How can I achieve that?

Is it possible to achieve that by making each follower (client) has Socket (connect with leader) and ServerSocket (connect to followers)?.

I look forward to hearing from you.

user3298986
  • 49
  • 1
  • 6
  • possible duplicate of [Is it possible to run a socket server and socket client on the same machine?](http://stackoverflow.com/questions/10069059/is-it-possible-to-run-a-socket-server-and-socket-client-on-the-same-machine) – Raedwald May 07 '14 at 11:49
  • Sure you can http://stackoverflow.com/questions/9580571/running-a-client-server-prog-on-a-single-system – Lav May 07 '14 at 12:33
  • I did not mean how to run the clent and the server in one machine. What I meant is that how to make client act as client as well as server. – user3298986 May 09 '14 at 16:03

2 Answers2

1

Leader can have the list of followers(Each follower can have an unique id). If a follower needs to pass message to another follower, he needs to pass the data to the Leader, and leader in turn can publish it to the intended follower or the leader can broadcast it to all followers. Leader can run different threads for each connection it has received and maintain a client pool for broadcasting.

  • However, the main aim of my application that I need to reduce the load on Leader, therefore if I lets the communication between the follower manage by the leader, I add extra load on the leader which is not acceptable. Therefore I need to make the communication achieve by follower directly. – user3298986 May 08 '14 at 12:12
  • Then the solution provided by Aniket is the best choice. The follower can have a server socket on a different port. Once a message needs to be broadcasted the server/ Leader can connect to its follower. Moreover the leader can publish the list of hosts and ports of the followers, so the followers can directly interact without the load on the Leader. – user3547829 May 14 '14 at 10:04
0
Can one machine act as client and server at same time using socket in Java?

Socket = IP + port. As long as both sockets on same machine(localhost) have different ports it is possible.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289