简体   繁体   中英

Can one machine act as client and server at same time using socket in Java?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM