客户端打开后,服务器端立即关闭

时间:2016-10-26 13:05:50

标签: java server

直到现在,我的服务器 - 客户端应用程序运行正常。然后当我决定运行它时(首先是服务器然后是客户端),服务器窗口在打开客户端窗口一秒后关闭。现在,只有客户端窗口保持打开状态。但是,javaw.exe进程(其中有2个,服务器和客户端)未停止。他们迷上了我在游戏中使用的端口号。因此,我再次运行时会收到JVM_Bind异常。

我终止了这两个进程并再次运行了应用程序,但出现了同样的问题。我甚至改变了端口号(当然在两个项目上),但它给了我相同的结果。

服务器和客户端会像在任何多人游戏中一样自动发送和接收内容。

客户方:

private void tick() {
    String info = handler.getPlayer(0).getX() + " " + handler.getPlayer(0).getY() + " " + handler.hb2.width + " " + handler.getPlayer(0).h1.x + " " + handler.getPlayer(0).h1.y + " " + handler.getPlayer(0).h2.x + " " + handler.getPlayer(0).h2.y + handler.getPlayer(0).h1.punch + " " + handler.getPlayer(0).h2.punch;
    sendMessage(info);
    String infoR = "";
    try {
        infoR = br.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scanner s = new Scanner(infoR);
    int a = 0;
    while(s.hasNext()) {
        String poop = s.next();
        a++;
        try {
            switch(a) {
            case 1:
                handler.getPlayer(1).x = Integer.parseInt(poop);
                break;
            case 2:
                handler.getPlayer(1).y  = Integer.parseInt(poop);
                break;
            case 3:
                handler.hb1.width = Integer.parseInt(poop);
                break;
            case 4:
                handler.getPlayer(1).h1.x = Integer.parseInt(poop);
                break;
            case 5:
                handler.getPlayer(1).h1.y = Integer.parseInt(poop);
                break;
            case 6:
                handler.getPlayer(1).h2.x = Integer.parseInt(poop);
                break;
            case 7:
                handler.getPlayer(1).h2.y = Integer.parseInt(poop);
                break;
            case 8:
                handler.getPlayer(1).h1.punch = Boolean.getBoolean(poop);
                break;
            case 9:
                handler.getPlayer(1).h2.punch = Boolean.getBoolean(poop);
                break;
            }
        } catch(NumberFormatException e) {
            frame.setVisible(false);
            poop += " " + s.next(); 
            if(poop.equals("you lose")) new ResultDisplay("red");
            handler.hb1.resultDisplayed = true;
            stop();
        }
    }
    handler.tick();
}

public void sendMessage(String message) {
    pw.println(message);
    pw.flush();
}

private void init() {
    try {
        sock = new Socket("127.0.127.1", 1111);
        ostream = sock.getOutputStream();
        pw = new PrintWriter(ostream);
        istream = sock.getInputStream();;
        br = new BufferedReader(new InputStreamReader(istream));
    } catch (IOException e) {
        e.printStackTrace();
    }
    // more game initialization code follows
首先调用

init(),然后每秒调用tick()方法60次(理想情况下)。

服务器端:

    private void tick() {
    String info = handler.getPlayer(1).getX() + " " + handler.getPlayer(1).getY() + " " + handler.hb1.width + " " + handler.getPlayer(1).h1.x + " " + handler.getPlayer(1).h1.y + " " + handler.getPlayer(1).h2.x + " " + handler.getPlayer(1).h2.y + " " + handler.getPlayer(1).h1.punch + " " + handler.getPlayer(1).h2.punch;
    String infoR = "";
    try {
        infoR = br.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scanner s = new Scanner(infoR);
    int a = 0;
    while(s.hasNext()) {
        a++;
        String poop = s.next();
        try {
            switch(a) {
            case 1:
                handler.getPlayer(0).x = Integer.parseInt(poop);
                break;
            case 2:
                handler.getPlayer(0).y  = Integer.parseInt(poop);
                break;
            case 3:
                handler.hb2.width = Integer.parseInt(poop);
                break;
            case 4:
                handler.getPlayer(0).h1.x = Integer.parseInt(poop);
                break;
            case 5:
                handler.getPlayer(0).h1.y = Integer.parseInt(poop);
                break;
            case 6:
                handler.getPlayer(0).h2.x = Integer.parseInt(poop);
                break;
            case 7:
                handler.getPlayer(0).h2.y = Integer.parseInt(poop);
                break;
            case 8:
                handler.getPlayer(0).h1.punch = Boolean.getBoolean(poop);
                break;
            case 9:
                handler.getPlayer(0).h2.punch = Boolean.getBoolean(poop);
                break;
            }
        }catch(NumberFormatException e) {
            frame.setVisible(false);
            poop += " " + s.next(); 
            if(poop.equals("you lose")) new ResultDisplay("green");
            handler.hb2.resultDisplayed = true;
            stop();
        }
    }
    sendMessage(info);
    handler.tick();
}

public void sendMessage(String message) {
    pw.println(message);
    pw.flush();
}

private void init() {
    try {
        sersock = new ServerSocket(1111);
        sock = sersock.accept();
        br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        pw = new PrintWriter(sock.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    // more game initialization code follows

主游戏线程调用init()tick()都由两个项目中的主游戏线程调用

1 个答案:

答案 0 :(得分:1)

在尝试并抓住服务器上的开关时,您可以通过将框架的可见性实际设置为 <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100" patternTransform="translate(0,0)"> <image xlink:href="layout/logo.png" x="0" y="0" width="100" height="100" transform="translate(0,0)" /> </pattern> 来处理异常,并且不打印异常,从而解释了为什么没有得到异常例外。

false

当然你抓住它是有原因的,但也许你应该打印出它抛出的异常而不是隐藏框架?它可能会扔掉你不想扔的东西。