Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

multicast client and server work only on the same PC

843790Aug 12 2006 — edited Aug 14 2006
Hi,
i have written a small client -Incoming.java- and a small server -Outcoming.java- multicast program. You run Outcoming FIRST in a console and write messages, which appear on the console on which you have started Incoming SECOND. The problem is that these work only if you run both of them on the same PC. In a sample client-server program from a Sun tutorial -"QuoteServer"- i noticed, by using tcpdump, that a couple of IGMP packets leave the PC and then packets leave with destination a multicast server. However in my program only the IGMP packets leave the PC; no contact to the multicast server.

What's the problem?
Thanks

Here are the files:

Incoming.java:
import java.io.*;
import java.net.*;

public class Incoming extends Thread
{
	int port;	//4446	
	String multicastServer;	//230.0.0.1
	MulticastSocket in;
	byte[] inBuffer;
	String username;
	String msg;
	InetAddress multicastserverIP;
	
	public Incoming()
	{	
		port=4446;
		msg="";
		try {
			in=new MulticastSocket(port);
		}
		catch (IOException e)
		{
			System.out.println("Failed to create multicast socket");
		}
		
	}
	
	
	
	public void run() 
	{
		try {
			multicastserverIP=InetAddress.getByName("230.0.0.1");
		}
		catch (UnknownHostException e)	
		{	
			
		}
		
		try {
			in.joinGroup(multicastserverIP);
		}
		catch (IOException e)
		{
			System.out.println("Failed to join group");
		}
		
		while (!msg.trim().equalsIgnoreCase("quit"))
		{
			readSocket();
			printText();
		}
		
	}
	
	
	
	private void readSocket() 
	{
		inBuffer=new byte[2048];
		DatagramPacket rcv=new DatagramPacket(inBuffer, inBuffer.length);
		
		try	{
			in.receive(rcv);
		}
		catch(IOException e)
		{
			System.err.println("Failed to receive incoming message");
		}
	}
	
	private String getUsername()
	{
		username=new String(inBuffer).trim().split("#!#")[0];
		return username;
	}
	
	private String getMessage()
	{
		msg=new String(inBuffer).trim().split("#!#")[1];
		return msg;
	}
	
	private void printText()
	{
		System.out.print(getUsername()+">");
		System.out.println(getMessage());
	}
	
	public static void main(String[] args) throws IOException, UnknownHostException
	{
		new Incoming().run();
	}
}
Outcoming.java:
import java.io.*;
import java.net.*;

public class Outcoming extends Thread 
{
	int port;	//4446	
	String multicastServer;	//230.0.0.1
	MulticastSocket out;
	byte[] outBuffer;
	String username;
	String msg;
	InetAddress multicastserverIP;
	
	public Outcoming() throws IOException, UnknownHostException
	{		
		port=4446;
		username="kostas";
		out=new MulticastSocket(4445);
//		out.joinGroup(InetAddress.getByName(multicastServer));		
	}/////////////////////Outcoming	
	
	
	
	public void run()
	{
		try {
			multicastserverIP=InetAddress.getByName("230.0.0.1");
		}
		catch (UnknownHostException e)	
		{	
			System.out.println("Cannot resolve ip address");
		}
		
		try {
			out.joinGroup(multicastserverIP);
		}
		catch (IOException e)
		{
			System.out.println("Failed to join group");
		}
		
		boolean readMore=true;
		String line="";	
		
		while (readMore)
		{
			System.out.print(">");  
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

			try	{
				line=br.readLine().trim();
			}
			catch (IOException e)	{
				e.printStackTrace();
			}
			
			if (line.equalsIgnoreCase("quit"))	
			{
				readMore=false;
			}
			
			createAndSendPacket(line);
		}
	}/////////////////////run

	
	
	private void createAndSendPacket(String msg)
	{
		try	{
			msg=username+"#!#"+msg;
			byte[] buf=msg.getBytes();
			InetAddress multicastserverIP=InetAddress.getByName(multicastServer);
			DatagramPacket dp=new DatagramPacket(buf, buf.length, multicastserverIP , port);
//			DatagramPacket dp=new DatagramPacket(buf, buf.length);
			out.send(dp);
			System.err.println("Packet sent");
		}
		catch (UnknownHostException e)
		{
			System.out.println("createAndSendPacket: UnknownHostException");
		}
		catch (IOException e)
		{
			System.out.println("createAndSendPacket: IOException");
		}
	}/////////////////////createAndSendPacket
	
	public static void main(String[] args) throws IOException, UnknownHostException
	{
		new Outcoming().run();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 11 2006
Added on Aug 12 2006
5 comments
421 views