Java中的客户端 - 服务器连接错误

时间:2014-05-13 17:23:07

标签: java client-server computer-science

我在CS课程中为我的学期项目写了一个游戏,但我不能定期做客户服务器部分。程序启动,它询问客户端或服务器和IP地址。当我输入IP时,发生了错误。

我在Eclipse中看到的错误:

线程中的异常" main" java.net.UnknownHostException:88.255.242.252:80

at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at MemoryGUI.<init>(MemoryGUI.java:84)
at MemoryGUI.main(MemoryGUI.java:178)

public class MemoryGUI extends Frame实现了ActionListener {

Socket socket;
ObjectOutputStream toClient;
public static String secim;
CellButtonHandler cbh = new CellButtonHandler();
Memory memory = new Memory();
JFrame Frame = new JFrame("Memory Game");
JFrame Framefirst = new JFrame("Memory Game");
JMenuBar TopMenu = new JMenuBar(); 
JMenu Oper = new JMenu(" Operations "); 
JMenuItem MNINewGame = new JMenuItem("Restart");
JMenuItem MNISolveGame = new JMenuItem("Solve Game");
JMenuItem MNICloseGame = new JMenuItem("Close Game");
JMenu MnStatus = new JMenu(" Status ");
JMenuItem MNITime = new JMenuItem("Passed Time");
JMenuItem MNIPerformance = new JMenuItem("Performance");
JMenu MnHelp = new JMenu(" Help ");
JMenuItem MNIHowToPlay = new JMenuItem("How to Play");
int SuccessStatusActive = 1; 

public MemoryGUI() throws UnknownHostException, IOException {


    Frame.setSize(960, 720); 
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setResizable(false);
    Frame.setVisible(true);
    Frame.setLocationRelativeTo(null);
    Frame.setJMenuBar(TopMenu);
    TopMenu.add(Oper);
    TopMenu.add(MnStatus);
    TopMenu.add(MnHelp);
    Oper.add(MNINewGame);
    Oper.add(MNISolveGame);
    Oper.add(MNICloseGame);
    MnStatus.add(MNITime);
    MnStatus.add(MNIPerformance);
    MnHelp.add(MNIHowToPlay);
    Frame.add(cbh.Cells);
    MNISolveGame.addActionListener(this);
    MNINewGame.addActionListener(this);
    MNICloseGame.addActionListener(this);
    MNITime.addActionListener(this);
    MNIPerformance.addActionListener(this);
    MNIHowToPlay.addActionListener(this);

    int choice = JOptionPane.showOptionDialog(null, //Component parentComponent
            "Server or Client?", //Object message,
            "Choose an option", //String title
            JOptionPane.YES_NO_OPTION, //int optionType
            JOptionPane.INFORMATION_MESSAGE, //int messageType
            null, //Icon icon,
            new String[]{"Client", "Server"}, //Object[] options,
            "Server");//Object initialValue 
    if (choice == 1) {//Server
        new Thread(new MemoryGUI.PanelServer()).start();
    } else { // Client is choosen

        String IP = JOptionPane.showInputDialog("Enter Server IP Address","127.0.0.1");
        socket = new Socket(IP,8080);
        new Thread(new MemoryGUI.PanelClient()).start();
    }
}

 class PanelServer implements Runnable {
        public void run() {
            try {
                ServerSocket serv = new ServerSocket(8080);
                socket = serv.accept();
                toClient = new ObjectOutputStream(socket.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(MemoryGUI.PanelServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    class PanelClient implements Runnable{
        public void run() {
            if (socket != null) {
                try {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    //Scanner reader = new Scanner(socket.getInputStream());
                    while (true) {
                       Button b=(Button)ois.readObject();
                       add(b);
                       repaint();
                    }
                } catch (Exception ex) {}
            }
        }
 }

public static void main(String[] args) throws UnknownHostException, IOException {

    MemoryGUI mem_gui = new MemoryGUI();
}

}

1 个答案:

答案 0 :(得分:0)

此地址(88.255.242.252:80)似乎包含一个端口。您可能需要在:上拆分输入。左边是你的地址,右边是你的端口。

String addrAndPort = "88.255.242.252:80";

String[] arr = addrAndPort.split(":");
String addr = (arr.length > 0) ? arr[0] : "";
int port = (arr.length > 1) ? Integer.parseInt(arr[1]) : 8080;
System.out.printf("The address is %s and the port is %d", addr, port);

当我运行以上内容时,我得到了输出

The address is 88.255.242.252 and the port is 80