简体   繁体   中英

Client-Server Connection Error in Java

I wrote a game for my term project in CS course but I couldn't do client-server part regularly. The program starts, it asks client or server and IP address. When I input the IP, error has occurred.

Error which I saw in Eclipse:

Exception in thread "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 implements 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();
}

}

This address (88.255.242.252:80) seems to include a port. You probably need to split the input on : . The left is your address, the right is your port.

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);

When I run the above I get the output

The address is 88.255.242.252 and the port is 80

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM