Solved

Java TCP sockets

Posted on 2011-09-05
8
485 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 50
servlet  URL Rewriting 1 36
Apps blocked by Java 9 79
fibonacci ten numbers 4 38
Creating an OSPF network that automatically (dynamically) reroutes network traffic over other connections to prevent network downtime.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now