• Post Reply Bookmark Topic Watch Topic
  • New Topic

MulticastSocket problem finding ...

 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all excuse my for my bad english.
My question is this i have a little chat program that works fine in a local net but when i and a friend tested it over the internet. He couldent find me and i couldent find him. The litle program works and no exceptions are thrown and there are no firewalls or proxys between us. so i was wondering how this class D multicast adressing relly works ? well here is the code
Thanks ! Rille
// this is the reciver class
/*
* Mottagare.java
*
*/
mport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

class Mottagare implements Runnable{
Thread aktivitet = new Thread(this);
MulticastSocket so;
JTextArea txt;


public Mottagare(MulticastSocket sock, JTextArea txtAr) {
so = sock;
txt = txtAr;
aktivitet.start();

}

public void run(){
byte [] data = new byte[1024];
while(true){
try{
DatagramPacket packet = new DatagramPacket(data, data.length);

so.receive(packet);
String medd = new String(data, 0, packet.getLength());
txt.append(medd + "\n");
}
catch(IOException e){
break;
}

}
}
}

and this is the real worker class
/*
* Chat.java
*
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Chat extends JFrame implements ActionListener {
String namn;
InetAddress iadr;
int port;
MulticastSocket so;
JTextArea txt = new JTextArea();
JScrollPane sp = new JScrollPane(txt);
JTextField skriv = new JTextField();
JButton sluta = new JButton("Koppla ner");

public Chat(String anvandarnamn, String gruppAdr, int portNr)throws IOException {
namn = anvandarnamn;
iadr = InetAddress.getByName(gruppAdr);
port = portNr;

so = new MulticastSocket(port);
so.joinGroup(iadr);
new Mottagare(so, txt);
sendMedd("UPPKOPPLAD");

setTitle("Chat " + namn);
txt.setEditable(false);
getContentPane().add(sluta, BorderLayout.NORTH);
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(skriv, BorderLayout.SOUTH);
sluta.addActionListener(this);
skriv.addActionListener(this);
setSize(400, 250);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void sendMedd(String a){
byte [] data = (namn + ": "+ a).getBytes();

DatagramPacket packet = new DatagramPacket(data, data.length, iadr, port);
try{
so.send(packet);
}
catch(IOException e){System.out.println(e);}
}

public void actionPerformed(ActionEvent e){
if(e.getSource() == skriv){
sendMedd(skriv.getText());
skriv.setText("");
}
else if(e.getSource() == sluta){
sendMedd("NEDKOPPLAD");
try{
so.leaveGroup(iadr);
}
catch(IOException ei){System.out.println(ei);}
so.close();
dispose();
System.exit(0);
}
}

public static void main(String args[]) throws IOException{
String namn = "Anonym";
if(args.length > 0){
namn = args[0];
}
new Chat(namn, "230.230.230.231", 9090);
}

}
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a wizard in this area, so take this with some caution, but I think most internet routers are configured not to route multicast packets. It's quite a can of worms; it could easily be abused, and you could easily claim incredible amounts of bandwidth downstream and there isn't the infrastructure to cope with that (or invoice it ).
It's a problem that is being worked on in a lot of places, though. Multicasting is the holy grail of internet broadcasting; audio and video streams are now streamed individually for every client, which is of course plain silly.
But for the moment you'll have to be content with your intranet, I think.
- Peter
 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for the reply .. well if one think of it it makes sense if all was broadcasting over the internet we had to rename the internet to slowynet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!