Link to home
Start Free TrialLog in
Avatar of gplana
gplanaFlag for Spain

asked on

Java TCP sockets

Dear Expert:

I would like to create a server process in Java which accepts an arbitrary number of client connections. These clients are connecting and disconnecting dynamically (on real time).

I would like to know how can I do for sending a broadcast message to every connected client.

Thank you.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You should be able to reuse http://technojeeves.com/joomla/index.php/free/84-server-by-david-flanagan and its Set<Connection> to send to all clients

perhaps this could help:
http://www.calsoftlabs.com/whitepapers/java-networking.html


Simple Example that explains multicasting

The Following source code is an simple example for broadcasting. It broadcast date and time to its multiple clients. ( from java 1.1 developers handbook )
Server Program


import java.net.*; 
import java.io.*; 
import java.util.*; 
public class BroadcastServer
{ 
public static final int PORT = 1200; 
public static void main(String args[]) 
throws Exception{ MulticastSocket socket; 
DatagramPacket packet; InetAddress address; 
address = InetAddress.getByName(); 
socket = new MulticastSocket(); 
// join a Multicast group and send the 
group salutations socket.joinGroup(address); 
byte[] data = null; 
for(;;)
{ 
 Thread.sleep(1000); 
 System.out.println("Sending "); 
 String str = (new Date()).toString(); 
 data = str.getBytes(); 
 packet = new DatagramPacket 
 (data,str.length(),address,PORT);
 // Sends the packet socket.send(packet); 
 } 
 // 
 for 
 } 
 // main 
 } // class BroadcastServer

Client program

 import java.net.*;
 import java.io.*;

 public class BroadcastClient{
 public static final int PORT = 1200; 
 public static void main(String args[]) 
 sthrows Exception{

 MulticastSocket socket;
 DatagramPacket packet;
 InetAddress address; 
                                
 address = InetAddress.getByName(args[0]);        
 socket = new MulticastSocket(BroadcastServer.PORT);

 //join a Multicast group and send the 
 group salutations

 socket.joinGroup(address);
 byte[] data = null;
 packet = new DatagramPacket(data,data.length);

 for(;;)
 {                                
   // receive the packets 
   socket.receive(packet); 
   String str = new String(packet.getData());
   System.out.println(" Time signal received from"+
   packet.getAddress() + " Time is : " +str);
 }  // for 

 }  // main     
         
}  // class Broadcast

Open in new window

Avatar of gplana

ASKER

Thank you both for your help.

However, what I need is a server program which accepts TCP connections and sends some data to every connected (by TCP) client.
I'm thinking in maintaining an array of active connections, but I don't know if it's possible to detect when a connection is closed so I can eliminate from array.

Any other help ?


there is a number of suhggestions here how to determine if connection is closed:
http://stackoverflow.com/questions/969866/java-detect-lost-connection
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gplana

ASKER

Excellent. Thank you very much.
:)