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();
}
}