Solved

Java TCP sockets

Posted on 2011-09-05
8
483 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility


there is a number of suhggestions here how to determine if connection is closed:
http://stackoverflow.com/questions/969866/java-detect-lost-connection
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 250 total points
Comment Utility
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
Comment Utility
>>
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
Comment Utility
Excellent. Thank you very much.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
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 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:
This video teaches viewers about errors in exception handling.

763 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