Вот вопрос
Есть сервер что посылает по UDP рекламу по Multicast
Есть Gui на клиенте что ловит рекламу и отображает её.
Но хотя ю вижу что реклама приходит на клиент (отпечатываю в консуле) Gui полностью умерает. Вот код
Код: Выделить всё
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import javax.swing.*;
import javax.swing.border.Border;
import Util.Controller;
import Common.ChatUser;
public class Chat extends JFrame
{
private Container surface;
private JTextArea taOutpu;
private JTextField tfInput;
private DefaultListModel model;
private JList lstClients;
private JButton btnSend;
private JLabel lblCommers;
private String toAppend;
private String currentPlayerName;
public Chat(String PlayerName)
{
this.currentPlayerName =PlayerName;
BorderLayout bl = new BorderLayout();
surface = this.getContentPane();
surface.setLayout(bl);
model = new DefaultListModel();
lstClients =new JList(model);
lstClients.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
model.addElement(new ChatUser("\\\\_otctpe/\\|-0_//","192.168.2.100"));
model.addElement(new ChatUser("test","192.168.2.100"));
btnSend = new JButton("Send");
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ChatUser cu = (ChatUser)lstClients.getSelectedValue();
try {
Socket socket = new Socket(cu.getIpAddress(), 3000);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
appendText("You say: "+tfInput.getText()+"\n");
out.writeObject(currentPlayerName+" say: "+tfInput.getText()+"\n");
out.close();
socket.close();
} catch (Exception e1) {
System.out.println(e1);
}
}});
taOutpu = new JTextArea();
taOutpu.setLineWrap(true);
taOutpu.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
tfInput = new JTextField();
lblCommers = new JLabel();
JPanel pnlBottom = new JPanel(new BorderLayout());
pnlBottom.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pnlBottom.add(BorderLayout.CENTER,tfInput);
pnlBottom.add(BorderLayout.EAST,btnSend);
surface.add(BorderLayout.SOUTH,pnlBottom);
surface.add(BorderLayout.CENTER,taOutpu);
surface.add(BorderLayout.EAST,lstClients);
surface.add(BorderLayout.NORTH,lblCommers);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(543,340);
this.setLocation(WindowPositioner.Center(this));
this.setResizable(false);
this.setVisible(true);
//start multicast sniffer
MulticastSniffer mcs = new MulticastSniffer(this);
mcs.start();
}
public static void main(String [] s)
{
new Chat("Michael");
}
public synchronized void appendText(String toAppend)
{
taOutpu.append(toAppend);
this.toAppend=null;
}
public void setAppendText(String toAppend)
{
this.toAppend = toAppend;
}
}
class MulticastSniffer extends Thread
{
MulticastSocket ms;
Chat chat;
//const'
public MulticastSniffer(Chat chat)
{
this.chat = chat;
try {
ms = new MulticastSocket(3000);
ms.joinGroup(InetAddress.getByName("230.0.0.1"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
while(true)
{
Runnable adder = new Runnable()
{
public void run()
{
byte[] buffer = new byte[8192];
while (true)
{
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
dp.setLength(buffer.length);
try {
ms.receive(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println(s);
chat.appendText(s);
}
}
};
try{
SwingUtilities.invokeAndWait(adder);
}
catch(Exception e){
System.out.println(e.getMessage());
}
} // end while
}//end run
}