上面的问题本人已经解决如下:
主要思想是:
1.客户端启动搜索对话框时向局域网内发一个广播信息并建立一个零时搜索服务端。
2.服务器接收到广播信息时分离出这个信息中的客户端ip地址建立链接并把本服务器的相关信息发给客户端。
3.客户端的零时搜索服务端接受到信息处理并显示到界面。
4.按“加入”、“取消”或窗口关闭按钮时关闭零时搜索服务端。
主要代码如下:
///////////////////////////////////////////////////////////////////////////////////////////////
package paoYao;
import java.io.*;
import java.net.*;
//该线程用于接受新用户的搜索消息并发送给用户当前服务器的信息。
public class ServerToSearch extends Thread{
public boolean isStop;
private Node[] node=null;
private String ip;
ServerToSearch(Node[] node){
this.node=node;
try{
InetAddress ipInet=InetAddress.getLocalHost();
String[] iptmp=ipInet.toString().split("/");
ip=iptmp[1];
}
catch(UnknownHostException e){
}
}
public void run(){
while(!isStop){
try{
InetAddress group1= InetAddress.getByName("228.5.6.7");
MulticastSocket s1 = new MulticastSocket(Setting.port);
s1.joinGroup(group1);
byte[] buf = new byte[1000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
s1.receive(recv);
String recvd=new String(recv.getData());
s1.leaveGroup(group1);
if(recvd.startsWith("SEARCHTHPAOYAO")){
String[] tmp=recvd.split("\n");
String[] tm=tmp[0].split("#");
StringBuffer userlst=new StringBuffer("");
for(int j=0;j<4&&node[j]!=null;j++){
userlst.append(node[j].usersit);
userlst.append("#");
userlst.append(node[j].username);
userlst.append("\n");
}
Socket socket=new Socket(tm[1],5858);
ObjectOutputStream output=new ObjectOutputStream(socket.getOutputStream());
output.flush();
output.writeObject(ip);
output.flush();
output.writeObject(Setting.server);
output.flush();
output.writeObject(userlst.toString());
output.flush();
output.close();
socket.close();
}
}
catch(UnknownHostException e){
}
catch(IOException e){
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
package paoYao;
import java.io.*;
import java.net.*;
//该线程用于接受服务器回应搜索信息时传送回的相应服务器信息,并调用SearchNetworkDialog类的addItem()方法把相应信息添加到界面。
public class SearchServer extends Thread{
public ServerSocket server;
private SearchNetworkDialog search;
SearchServer(SearchNetworkDialog search){
this.search=search;
try{
server=new ServerSocket(5858,255);
}
catch(Exception e){
}
}
public void run(){
while(!server.isClosed()){
try{
Socket socket=server.accept();//侦听并接受到此套接字的连接。
ObjectInputStream input=new ObjectInputStream(socket.getInputStream());
String serverip=(String)input.readObject();
String servername=(String)input.readObject();
String userlst=(String)input.readObject();
search.addItem(serverip,servername,userlst);
input.close();
socket.close();
}
catch(Exception e){
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
package paoYao;
import javax.swing.*;
import java.io.IOException;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class SearchNetworkDialog extends JDialog implements ActionListener{
private int sign=0;
private PaoYao main=null;
private InetAddress ip;
private String ipAll;
// private String hostname;
private GridBagConstraints c;
private String[] serverIp=new String[256];
private JRadioButton[][] sit=new JRadioButton[256][4];
private ButtonGroup sitgroup=new ButtonGroup();
private JLabel[] sernam=new JLabel[256];
private JLabel[] serno=new JLabel[256];
private JButton jLno;
private JButton jLserver;
private JButton jLeast;
private JButton jLsouth;
private JButton jLwest;
private JButton jLnorth;
private JPanel jPMain;
private JPanel jPm;
private JPanel jPSM;
private JPanel jPSouth;
private JButton jBConfirm;
private JButton jBCancel;
private JButton jBRenovate;
private JScrollPane jSMain;
private JTextField jTFStatus;
private SearchServer searchServer;
SearchNetworkDialog(PaoYao main){
this.main=main;
initDialog();
try{
ip=InetAddress.getLocalHost();
}
catch(UnknownHostException e){
}
String[] ipsh=ip.toString().split("/");
ipAll=ipsh[1];
sendToNet();
searchServer=new SearchServer(this);
searchServer.start();
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent we){
closing();
}
}
);
}
public void closing(){
try{
searchServer.server.close();
}
catch(IOException e){
}
this.dispose();
}
public void sendToNet(){
try{
String msg = "SEARCHTHPAOYAO#"+ipAll+"\n";
InetAddress group1 = InetAddress.getByName("228.5.6.7");
MulticastSocket s1 = new MulticastSocket(Setting.port);
// s.setLoopbackMode(true);
s1.joinGroup(group1);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group1, Setting.port);
s1.send(hi);
s1.leaveGroup(group1);
}
catch(UnknownHostException e){
}
catch(IOException e){
}
}
public void initDialog(){
setSize(new Dimension(405,300));
setTitle("搜索网络");
jLno=new JButton("序号");
jLserver=new JButton("服务器名称");
jLeast=new JButton("东庄");
jLsouth=new JButton("南庄");
jLwest=new JButton("西庄");
jLnorth=new JButton("北庄");
jPm=new JPanel(new BorderLayout());
jPMain=new JPanel(new GridBagLayout());
c=new GridBagConstraints();
c.gridx=0;
c.gridy=0;
jPMain.add(jLno,c);
c=new GridBagConstraints();
c.gridx=1;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
jPMain.add(jLserver,c);
c=new GridBagConstraints();
c.gridx=2;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
jPMain.add(jLeast,c);
c=new GridBagConstraints();
c.gridx=3;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
jPMain.add(jLsouth,c);
c=new GridBagConstraints();
c.gridx=4;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
jPMain.add(jLwest,c);
c=new GridBagConstraints();
c.gridx=5;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
jPMain.add(jLnorth,c);
jPm.add(jPMain,BorderLayout.NORTH);
jPSM=new JPanel();
jPSouth=new JPanel();
jTFStatus=new JTextField();
jTFStatus.setEditable(false);
jBConfirm=new JButton("加入");
jBCancel=new JButton("取消");
jBRenovate=new JButton("刷新");
jBConfirm.addActionListener(this);
jBCancel.addActionListener(this);
jBRenovate.addActionListener(this);
jPSouth.setLayout(new GridBagLayout());
c=new GridBagConstraints();
c.insets=new Insets(10,0,10,10);//创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象。
c.gridx=0;
c.gridy=0;
jPSouth.add(jBConfirm,c);
c=new GridBagConstraints();
c.insets=new Insets(10,10,10,10);//创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象。
c.gridx=1;
c.gridy=0;
jPSouth.add(jBCancel,c);
c=new GridBagConstraints();
c.insets=new Insets(10,10,10,0);//创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象。
c.gridx=2;
c.gridy=0;
jPSouth.add(jBRenovate,c);
jSMain=new JScrollPane(jPm);
jSMain.revalidate();//支持推迟的自动布局。
jPSM.setLayout(new BorderLayout());
jPSM.add(jPSouth,BorderLayout.CENTER);
jPSM.add(jTFStatus,BorderLayout.SOUTH);
this.setLayout(new BorderLayout());
this.add(jSMain,BorderLayout.CENTER);
this.add(jPSM,BorderLayout.SOUTH);
setStatus("没有搜索到可用服务器!");
//设置对话框居中
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width-500)/2,(int)(screenSize.height-400)/2);
this.setResizable(false);
this.setVisible(true);
}
public void setStatus(String str){
jTFStatus.setText(str);
}
public void addItem(String hostAddr,String servernm,String userInfo){
this.serverIp[sign]=hostAddr;
String[] str=userInfo.split("\n");
for(int i=0;i<str.length;i++){
String[] sr=str[i].split("#");
if(sr[0].equals("East")){
this.sit[sign][0]=new JRadioButton(sr[1]);
// sitgroup.add(sit[sign][0]);
sit[sign][0].setEnabled(false);
}
else if(sr[0].equals("South")){
this.sit[sign][1]=new JRadioButton(sr[1]);
// sitgroup.add(sit[sign][1]);
sit[sign][1].setEnabled(false);
}
else if(sr[0].equals("West")){
this.sit[sign][2]=new JRadioButton(sr[1]);
// sitgroup.add(sit[sign][2]);
sit[sign][2].setEnabled(false);
}
else if(sr[0].equals("North")){
this.sit[sign][3]=new JRadioButton(sr[1]);
// sitgroup.add(sit[sign][3]);
sit[sign][3].setEnabled(false);
}
}
for(int i=0;i<4;i++){
if(sit[sign][i]==null){
this.sit[sign][i]=new JRadioButton("无人");
sitgroup.add(sit[sign][i]);
}
}
serno[sign]=new JLabel(String.valueOf(sign));
sernam[sign]=new JLabel(servernm);
c=new GridBagConstraints();
c.gridx=0;
c.gridy=sign+1;
jPMain.add(serno[sign],c);
c=new GridBagConstraints();
c.gridx=1;
c.gridy=sign+1;
c.fill=GridBagConstraints.BOTH;
jPMain.add(sernam[sign],c);
c=new GridBagConstraints();
c.gridx=2;
c.gridy=sign+1;
c.fill=GridBagConstraints.BOTH;
jPMain.add(sit[sign][0],c);
c=new GridBagConstraints();
c.gridx=3;
c.gridy=sign+1;
c.fill=GridBagConstraints.BOTH;
jPMain.add(sit[sign][1],c);
c=new GridBagConstraints();
c.gridx=4;
c.gridy=sign+1;
c.fill=GridBagConstraints.BOTH;
jPMain.add(sit[sign][2],c);
c=new GridBagConstraints();
c.gridx=5;
c.gridy=sign+1;
c.fill=GridBagConstraints.BOTH;
jPMain.add(sit[sign][3],c);
jPMain.updateUI();
sign++;
setStatus("搜索到"+sign+"个可用服务器!");
}
public void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if(obj==jBConfirm){
for(int i=0;i<sign;i++){
for(int j=0;j<4;j++){
if(sit[i][j].isSelected()){
String tempsit=new String();
if(j==0){
tempsit="East";
}
else if(j==1){
tempsit="South";
}
else if(j==2){
tempsit="West";
}
else if(j==3){
tempsit="North";
}
// System.out.print(tempsit);
// System.out.print(serverIp[i]);
new ClientNewGameDialog(main,tempsit,serverIp[i]);
closing();
}
}
}
}
else if(obj==jBCancel){
closing();
}
else if(obj==jBRenovate){
this.sign=0;
sendToNet();
jPMain.updateUI();
}
}
}