覆盖启动函数中的Java套接字编程

时间:2017-11-18 03:32:55

标签: java network-programming

我是使用JavaFx及其功能的新手。我正在创建一个由本地连接播放的2人棋盘游戏。我知道在JavaFX中你不需要一个主类,因为start函数的@Override基本上是你的主要功能。现在我尝试在start函数中创建连接,但每次尝试时都会继续吐出java.net.BindException和java.lang.RuntimeException。我查看了我的端口,它没有被使用。我注释掉代码的连接部分,应用程序能够运行。您是否无法连接启动功能?

客户代码:

 @Override
   public void start(Stage primaryStage) throws Exception, ClassNotFoundException, IOException,UnknownHostException {
       Scene scene1 = new Scene(createContent());
       primaryStage.setTitle("Client Side");
       primaryStage.setScene(scene1);
       primaryStage.show();

       socket = new Socket("127.0.0.1",1337);

       boolean end = false;
       boolean turn = true;
       Serializable ObjectInputStream;
        ObjectInputStream I = new ObjectInputStream(socket.getInputStream());
        Serializable ObjectOutputStream;
        ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream());
        Scanner scan = new Scanner(socket.getInputStream());
        PrintStream p = new PrintStream(socket.getOutputStream());
       do
       {


        while(turn == true)
           {   
               temp1 = scan.nextInt();
               temp2 = scan.nextInt();
               Checker check = (Checker) I.readObject();
               check.relocate(temp1, temp2);
               turn = false;
           }
           while(turn == false)
           {

               p.println(sendX);
               p.println(sendY);
               o.writeObject(sendO);
               turn = true;


           }


       }while(end == false);

   }

服务器代码正好相反:

   @Override
   public void start(Stage primaryStage) throws Exception, 
   ClassNotFoundException, IOException,UnknownHostException {
   Scene scene = new Scene(createContent());
   primaryStage.setTitle("Server Side");
   primaryStage.setScene(scene);
   primaryStage.show();

   serverSocket = new ServerSocket(1337);
   Socket socket = serverSocket.accept();
   boolean end = false;
   boolean turn = true;
   Serializable ObjectInputStream;
   Serializable ObjectOutputStream;
    ObjectInputStream I = new ObjectInputStream(socket.getInputStream());
    ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream());
    Scanner scan = new Scanner(socket.getInputStream());
    PrintStream p = new PrintStream(socket.getOutputStream());
   do
   {


    while(turn == true)
       {   
        p.println(sendX);
        p.println(sendY);
        o.writeObject(sendO);
        turn = false;
       }
       while(turn == false)
       {


        temp1 = scan.nextInt();
        temp2 = scan.nextInt();
        Checker check = (Checker) I.readObject();
        check.relocate(temp1, temp2);
        turn = true;

       }


   }while(end == false); 

   }

2 个答案:

答案 0 :(得分:0)

  

使用Try catch Block

 try {
       serverSocket = new ServerSocket(1337);
       Socket socket = serverSocket.accept();
    }
    catch (IOException e) {
       System.out.println(e);
    }   

   boolean end = false;
   boolean turn = true;
   Serializable ObjectInputStream;
   Serializable ObjectOutputStream;
   ObjectInputStream I = new ObjectInputStream(socket.getInputStream());
   ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream());
   Scanner scan = new Scanner(socket.getInputStream());
   PrintStream p = new PrintStream(socket.getOutputStream());
  do
  {


     while(turn == true)
   {   
    p.println(sendX);
    p.println(sendY);
    o.writeObject(sendO);
    turn = false;
   }
   while(turn == false)
   {


    temp1 = scan.nextInt();
    temp2 = scan.nextInt();
    Checker check = (Checker) I.readObject();
    check.relocate(temp1, temp2);
    turn = true;

   }


   }while(end == false); 

   }  

答案 1 :(得分:0)

  

您可以尝试将另一个进程绑定到同一个端口。

每当我遇到 JVM_BIND 错误时,来自Windows Sysinternals的

TCPView (仅限Windows)是我最喜欢的应用程序。它显示哪些进程正在侦听哪个端口。它还提供了一个方便的上下文菜单来杀死进程或关闭阻碍连接的连接。