Solved

Java TCP sockets

Posted on 2011-09-05
8
495 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:gplana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36483310
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36483318

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

0
 
LVL 15

Author Comment

by:gplana
ID: 36483498
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 ?
0
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
LVL 47

Expert Comment

by:for_yan
ID: 36483521


there is a number of suhggestions here how to determine if connection is closed:
http://stackoverflow.com/questions/969866/java-detect-lost-connection
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 250 total points
ID: 36483532
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 36483604
>>
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.
>>

See the comment i posted. There's *already* a collection of active connections, which you can iterate
0
 
LVL 15

Author Closing Comment

by:gplana
ID: 36483702
Excellent. Thank you very much.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36483838
:)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question