Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am a newbie in java.
I am looking to broadcast a meesage in java from the client to all the listening servers.I dont want to broadcast to any particular machine hence i am 192.168.1.255 as the ip address in the code but still doesnot send the packets to the listening servers.However when i use a specific ip adress it just works fine.Code is as follows:

Java
import java.io.*; 
import java.net.*; 

public class client {

	public static void main(String args[]) throws Exception    
	{      		
		while(true)
		{
		
		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
		DatagramSocket clientSocket = new DatagramSocket(); 
		clientSocket.setBroadcast(true);
		
		InetAddress IPAddress = InetAddress.getByName("192.168.1.255"); 
		byte[] sendData = new byte[50];   
		byte[] receiveData = new byte[50];  
		String sentence = inFromUser.readLine();   
		sendData = sentence.getBytes();    
		DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,new InetSocketAddress("192.168.1.255",9876)); 
		clientSocket.send(sendPacket); 
		DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);   
		clientSocket.receive(receivePacket); 
		String modifiedSentence = new String(receivePacket.getData());  
		System.out.println("FROM SERVER:" + modifiedSentence);   
		clientSocket.close();    
		}
	} 

}


Any help would really be appreciated.

Thanks in advance
Posted
Updated 4-Nov-14 21:59pm
v2

1 solution

Your broadcast address needs to take account of your subnet mask as described in http://en.wikipedia.org/wiki/Broadcast_address[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900