Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java TCP sockets

Posted on 2011-09-05
8
Medium Priority
?
501 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 total points
ID: 36483532
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

963 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