写了一个UDP的聊天系统,但是无法实现广播,请各位帮忙看看
[size=18px]sever端:[/size]package UDPChatRoom;
import java.net.*;
import java.util.*;
public class UDPChatRoomServer {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
HashSet set=new HashSet();
DatagramSocket ds=new DatagramSocket(9000);
while(true){
byte[] bs1=new byte[100];
DatagramPacket letter1=new DatagramPacket(bs1,0,bs1.length);
ds.receive(letter1);
InetAddress ip=letter1.getAddress();
set.add(ip);
int port=letter1.getPort();
byte[] bs2=letter1.getData();
int offset=letter1.getOffset();
int length=letter1.getLength();
String str=new String(bs2,offset,length);
byte[] bs3=str.toUpperCase().getBytes();
for(Object o:set){
System.out.println(o);
DatagramPacket letter2=new DatagramPacket(bs3,0,bs3.length,(InetAddress)o,port);
ds.send(letter2);
}
}
}
}
[size=18px]client端:[/size]package UDPChatRoom;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class UDPChatRoomClient {
JTextField jtf;
JTextArea jta;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UDPChatRoomClient c=new UDPChatRoomClient();
}
public UDPChatRoomClient(){
initGUI();
initNet();
}
public void initGUI(){
JFrame j=new JFrame("Chat");
j.setSize(400,300);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea();
jta.setEditable(false);
JScrollPane jsp=new JScrollPane(jta);
jtf=new JTextField();
j.add(jsp,BorderLayout.CENTER);
j.add(jtf,BorderLayout.SOUTH);
j.setVisible(true);
}
public void initNet(){
try {
final DatagramSocket ds=new DatagramSocket();
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
byte[] bs1=jtf.getText().getBytes();
jtf.setText("");
String sss="192.168.0.12";
InetAddress ip=InetAddress.getByName(sss);
DatagramPacket letter1=new DatagramPacket(bs1,0,bs1.length,ip,9000);
ds.send(letter1);
} catch (
UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (
IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
while(true){
DatagramPacket letter2=new DatagramPacket(new byte[100],0,100);
ds.receive(letter2);
byte[] bs2=letter2.getData();
int length=letter2.getLength();
int offset=letter2.getOffset();
String str=new String(bs2,offset,length);
jta.append(str+"\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我没法实现一个人说话,每个客户端的JTextArea上都显示出来,不知道哪错了,请教大家,先谢谢了
------解决方案--------------------
给你简单改造了一下,你看看
服务端
Java code
package UDPChatRoom;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class UDPChatRoomServer {
public static final int UDP_SERVER_PORT = 9001;
public static final int UDP_BOARD_PORT = 9000;
public static void main(String[] args) throws Exception {
//create boardcast socket and add to group
MulticastSocket boradCast = new MulticastSocket(UDP_BOARD_PORT);
InetAddress boradIP = InetAddress.getByName("224.168.0.114");
boradCast.joinGroup(boradIP);
//create udp server socket to get data from client
DatagramSocket serverSocket = new DatagramSocket(9001);
while (true) {
byte[] bs1 = new byte[100];
DatagramPacket letter1 = new DatagramPacket(bs1, 0, bs1.length);
// receive data by UDP
serverSocket.receive(letter1);
byte[] bs2 = letter1.getData();
int offset = letter1.getOffset();
int length = letter1.getLength();
String str = new String(bs2, offset, length);
byte[] bs3 = str.toUpperCase().getBytes();
DatagramPacket letter2 = new DatagramPacket(bs3, bs3.length,
boradIP, 9000);
// send received data to group
boradCast.send(letter2);
}
}
}