今天刚接触java的网络编程。
使用socket和serversocket编写了一个简单的对话工具。
在同一个电脑上调试的时候毫无问题。
但是把客户端放到别的电脑上去,那么就搜不到客户端了
请各位高手帮帮小弟忙,感激不尽!!
以下是我的代码(非常简陋,请见谅)://客户端
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TalkClient extends Frame implements ActionListener{
    
    Label prompt = new Label("请输入交谈内容:");
    Label IP = new Label("本地IP:");
    Panel panel = new Panel();
    Panel panelInfo = new Panel();
    TextField tLine = new TextField(10);//对话输入框
    TextArea tArea = new TextArea(5,10);//显示所有对话
    
    Socket client = null;
    
    //输入输出流
    InputStream input = null;
    OutputStream output = null;
    
    boolean connect = false;//已经成功连接的标志
    
    public TalkClient()
    {
        super("Client客户端");
        setSize(250,250);
        panel.add(prompt);
        panel.add(tLine);
        panelInfo.add(IP);
        add("North",panel);
        add("South",panelInfo);
        add("Center",tArea);
        tLine.addActionListener(this);
        this.addWindowListener(//按退出按钮
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        try{
                            output.write("bye~".getBytes());
                            if(connect)
                            {
                                output.close();
                                input.close();
                                client.close();
                            }
                            System.exit(0);
                        }
                        catch(Exception ex){
                            System.out.println("退出错误:" + ex);
                        }
                    }
                }
                );
        this.show();
        
        connectServer();
        connecting();
        
    }
    
    public void actionPerformed(ActionEvent e)
    {
        try{
            String str = tLine.getText();
            byte []buf = str.getBytes();
            tLine.setText(null);
            output.write(buf);
            tArea.append("我说:" + str);
            tArea.append("\n");
        }
        catch(Exception ex){
            System.out.println("action错误:" + ex);
        }
    }
    private void connectServer()//连接server
    {
        boolean flag = true;
        while(flag)
        {
            try{
                client = null;
                input = null;
                output = null;
                
                tArea.append("正在尝试连接服务器...\n");
                
                InetAddress address = InetAddress.getLocalHost();
                client = new Socket(address.getHostAddress().toString(),6000);//以本地IP创建客户端
                IP.setText("本地IP:" + address.getHostAddress().toString());
                tArea.append("已链接到服务器" + client.getInetAddress().getHostName() + "\n");
                
                flag = false;//成功连接,跳出循环
                connect = true;
                
                input = client.getInputStream();
                output = client.getOutputStream();
            }
            catch(Exception e){
                System.out.println("无法连接服务器:" + e);
                tArea.append("无法连接服务器\n");
                
                int result = JOptionPane.showConfirmDialog(null,"是否需要重新连接?\n(若选择不重新连接,此程序将会关闭)","注意",
                        JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
                
                if(result == JOptionPane.NO_OPTION)
                {
                    flag = false;                    tArea.append("再见");
                    System.exit(0);
                }
            }
        }//end while
    }
    private void connecting()//正在连接状态
    {
        boolean flag = true;
        while(flag)
        {            
            try{
                byte []buf = new byte[256];
                input.read(buf);//从服务器读取信息放进buf中
                String str = new String(buf);
                tArea.append("服务器:" + str);
                tArea.append("\n");
            }
            catch(Exception e){
                tArea.append("连接断开...");
                int result = JOptionPane.showConfirmDialog(null,"是否需要重新连接?\n(若选择不重新连接,此程序将会关闭)","注意",
                        JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
                if(result == JOptionPane.NO_OPTION)
                {
                    flag = false;
                    tArea.append("再见");
                    System.exit(0);
                }
                else if(result == JOptionPane.YES_OPTION)
                {
                    connectServer();//重新连接服务器
                }
            }
        }//end while
    }
}
//服务器
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TalkServer extends Frame implements ActionListener{
    Label prompt = new Label("请输入交谈内容:");
    Label IP = new Label("本地IP:");
    Panel panel = new Panel();
    TextField tLine = new TextField(10);
    TextArea tArea = new TextArea(5,10);
    
    ServerSocket server = null;
    Socket client = null;
    InputStream input = null;
    OutputStream output = null;
    
    public TalkServer()
    {
        super("Server服务器");
        setSize(250,250);
        panel.add(prompt);
        panel.add(tLine);
        panel.add(IP);
        add("North",panel);
        add("Center",tArea);
        tLine.addActionListener(this);
        this.addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                }
                );
        show();
        
        try{
            server = new ServerSocket(6000);//创建server
        }
        catch(Exception e){
            System.out.println("客户端创建失败" + e);
        }
        
        searchClient();
        connecting();
        
    }
    public void actionPerformed(ActionEvent e)
    {
        try{
            String str = tLine.getText();
            byte []buf = str.getBytes();
            tLine.setText(null);
            output.write(buf);
            tArea.append("我说:" + str);
            tArea.append("\n");
        }
        catch(Exception ex){
            System.out.println("action错误:" + ex);
        }
    }
    private void searchClient()//搜索client
    {
        client = null;
        input = null;
        output = null;
        
        try{
            tArea.append("正在搜索用户...\n");
            client = server.accept();
            tArea.append("已链接到客户端" + client.getInetAddress().getHostName() + "\n");
            input = client.getInputStream();
            output = client.getOutputStream();
        }
        catch(Exception e){
            System.out.println("搜索不到用户" + e);
        }
    }
    private void connecting()
    {
        boolean flag = true;
        while(flag)
        {
            try{
                byte []buf = new byte[256];
                input.read(buf);
                String str = new String(buf);
                tArea.append("客户端:" + str);
                tArea.append("\n");
                
                try{
                    client.sendUrgentData(0);
                }catch(Exception e)
                {
                    System.out.println("与用户断开");
                    tArea.append("与用户断开连接...\n");
                    int result = JOptionPane.showConfirmDialog(null,"是否需要重新搜索用户?\n(若选择不重新搜索,此程序将会关闭)","注意",
                            JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
                    
                    if(result == JOptionPane.NO_OPTION)
                    {
                        flag = false;                        tArea.append("再见");
                        System.exit(0);
                    }
                    else if(result == JOptionPane.YES_OPTION)
                    {
                        searchClient();
                    }
                }
            }
            catch(Exception e){
                System.out.println("对话发生错误" + e);
            }
        }//end while
    }
}