Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-10-2008, 02:27 PM   #1
Registered: Nov 2006
Distribution: xubuntu
Posts: 444

Rep: Reputation: 31
How to broadcast with Java Socket

Hi there,

Does anybody know how can i broadcast a message with a Java Sockets implementation (tcp).

Thank you.
Old 12-11-2008, 01:00 PM   #2
LQ Guru
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
See the InetAddress API in the javadocs.

Basically, you would create a new Socket:

Socket socket;
OutputStream out;


InetAddress address = InetAddress.getByName("") <--- IP address as String
int port = xxxx; <--- client port
socket = new Socket(address, port);
out = socket.getOutputStream(); <---- obtain outputstream for writing to client(s)
String msg = "..."; <--- message to client(s)
byte [] output = msg.getBytes(); <---- convert msg to bytes; optionally specify a charset as an argument
out.write(output); <--------- write output to client(s)
out.flush(); <--- flush output so we are sure that it has been sent (failure to do so can cause client to hang)
} catch (IOException iox){

}finally {
if (out != null) <-- first check whether we have an outputstream at all
out.close(); <---- if so, close outputstream
}catch (IOException iox){

Last edited by jay73; 12-11-2008 at 01:15 PM.
Old 12-11-2008, 04:11 PM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,703

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
I'm afraid this sample code doesn't address the question.

Broadcasting can't be done with TCP Sockets which only support point to point (unicast) connected transmission.

To broadcast, you need to use a UDP socket (DatagramSocket).

You might also want to use multicasting (MulticastSocket).
Old 12-14-2008, 01:44 AM   #4
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
This looks like a homework question. Did you look up what you wanted on Google?

I did a simple search on your behalf, and came up with tons of good results:

Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:25 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiclient Java Socket Question Chryzmo Programming 1 04-05-2008 12:00 PM
broadcast (udp) socket - multi processes emarri Programming 1 09-25-2006 06:06 AM
Multiple connections on one socket? (Java) smoothdogg00 Programming 2 04-09-2006 09:15 PM
connection between java socket and unix socket sathishkumar Programming 1 01-10-2006 03:21 PM
Why Does a Broadcast on a Raw Socket get echoed back to me? btn_paragon Programming 5 09-09-2005 07:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration