• Post Reply Bookmark Topic Watch Topic
  • New Topic

Send message to all clients from server  RSS feed

 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title says

Client:



Server:


Thanks in Advance.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:Send message to all clients from server


I assume that when you say "all clients" that you mean "one client". Your server code accepts a connection, does something, and writes some data to the client. After that, it closes the client connection (which means that the client can't get anything else).

Also, it is the same thread that is doing the accept() from the server socket, and writing to the client.... so... at no time will you ever have more than one client connected at the same time.

Henry
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have more than one Client , So i need to send to all Clients at once
Because right now i have to write and send but i just want to send once to all clients and not each by one.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:I have more than one Client , So i need to send to all Clients at once
Because right now i have to write and send but i just want to send once to all clients and not each by one.


Don't know what to tell you. You are trying to solve the problem of writing to more than one client at once... but you first need to have more than one client connected simultaneously first, before you even have a problem to solve.

Perhaps the question should be... how to design your application to allow more than one client to connect at the same time?

Henry
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unclear what your environment is, but anyway.

Look at java.net.MulticastSocket - used for IP multicast packets (see DatagramPacket etc.)

See this Wikipedia article for extended discussion.

Bill
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, let's assume that the OP fixes the issue, and the server actually allows more than one simultaneous connection.

William Brogden wrote:Unclear what your environment is, but anyway.

Look at java.net.MulticastSocket - used for IP multicast packets (see DatagramPacket etc.)

See this Wikipedia article for extended discussion.


Switching from TCP to Multicast is not as simple as it sounds. It changes a lot about the transport, which may need to be addressed. Specifically ...

It switches from TCP to UDP, which means that it is now an unreliable connection. If the OP want the connection to be reliable, he will have to implement his own reliable layer with gap detection and gap fill capability.

It switches from being receiver paced to being source paced. TCP can place back pressure on the source to prevent it from overwhelming the receiver. With UDP, there is no back pressure. If the source writes too fast, and a component can't handle it, the offending packet is merely discarded.

It limits the range of communication. TCP can pretty much go anywhere. Multicast can only go where it is routed. And most configurations, especially ones for the home, do not configure multicast routing out of the LAN environment.

It is also connection-less. The source can't tell how many receivers (or if any receivers) are interested in the multicast group.


So... I recommend that the OP review the pros and cons before switching to multicast -- it may be easier to just write the same message once to every client.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!