Im making a chat application where one client will send a message to ALL other clients who are currently online through server.
The problem is that I'm unable to make such a loop which can infinitely send n receive messages using server.
Secondly can't we just use PrintWriter and BufferedReader, why to use DataInputStream :S
Here is the loop i was trying to make:

for( ; ; )		
			{
				str = in.readLine();
				if(str == "") break;
				jTextArea1.append("\n"  +  str);
			 
			} // this is for Receiving the data frm server

I dont know how to send that, may be like that

out.println(jTextField1);

Im totally stuck in it..hope to get some help
Thanks.

This can be done by splitting up send & receive as seperate methods.And by using GroupAddress,
Below link & code might be usefull.

-------------------------------
http://java.sun.com/docs/books/tutorial/networking/datagrams/broadcasting.html
-------------------------------

void BroadcastSend()
		{
			int iBrcstPort=1600;
			String sBroadcast="";
			byte b[]=new byte[1000];
			try
			{
				sBroadcast=ta2.getText();  //GetText from JTextArea
				sBroadcast=sBroadcast.trim();

				mulSoc=new MulticastSocket();
				InetAddress BrcstAddress = InetAddress.getByName("230.0.0.1");
				mulSoc.joinGroup(BrcstAddress);

				b=sBroadcast.getBytes();
				DatagramPacket pack = new DatagramPacket(b,b.length,BrcstAddress,iBrcstPort);
				mulSoc.send(pack);
				
				//mulSoc.leaveGroup(BrcstAddress);
				//mulSoc.close();
			}
			catch (Exception e)
			{
				System.out.println("Excep BroadcastSend: "+e);
			}
		}

		void BroadcastReceive()
		{
			int iBrcstPort=1600;
			String sBroadcast="",str="";
			byte b[]=new byte[1000];
			try
			{
				mulSoc=new MulticastSocket(iBrcstPort);
				InetAddress BrcstAddress = InetAddress.getByName("230.0.0.1");
				mulSoc.joinGroup(BrcstAddress);

				while(true)
				{
					DatagramPacket pack = new DatagramPacket(b,b.length);
					mulSoc.receive(pack);

					str=new String(pack.getData(),0,pack.getLength());
					System.out.println("Broadcast From Server : "+str);
				}				
				mulSoc.leaveGroup(BrcstAddress);
				mulSoc.close();
			}
			catch (Exception e)
			{
				System.out.println("Excep BroadcastReceive: "+e);
			}
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.