LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 12-10-2008, 02:27 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 373

Rep: Reputation: 30
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
jay73
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;

try{

InetAddress address = InetAddress.getByName("xxx.xxx.xxx.xxx") <--- 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 {
try{
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
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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
arunmathew1984
Member
 
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:
http://www.google.co.in/search?q=jav...ient=firefox-a

Linux Archive

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


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 07:06 PM.

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