簡體   English   中英

服務器無響應

[英]Server not responding

我正在用Java開發服務器/客戶端應用程序,但是它並不能真正按照我想要的方式工作。

我建立了連接,一切正常,但隨后卻什么也沒做。 我認為接受客戶不是我的主意。

這是我的代碼。

網絡線程:

package libgdx.server;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.*;


public class NetworkingThread extends Thread {
private Socket sock= null;
 private int ID;

public NetworkingThread(Socket sock){
    super("Multiple connection thread!");

    this.sock = sock;

 }

@Override
public void run()  {
   try{
   System.out.println("In the Method run() in the thread!");

       PrintWriter out = new PrintWriter(sock.getOutputStream(),true);

    BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String inputLine,outputLine;
    AlphaProtocol alpha = new AlphaProtocol();
    outputLine = alpha.ProcessInput(null);
    out.println(outputLine);

    out.println("Welcome to the Server! Hope you enjoy your stay.");
    while ((inputLine  = in.readLine()) != null){
        outputLine = alpha.ProcessInput(inputLine);
        out.println(outputLine);
        if (outputLine.equals("Bye!")){
            break;
        }
    }
    out.close();
    in.close();
   sock.close();
   }catch(IOException ioe){
       ioe.printStackTrace();
       System.err.println("Error in the Tread of Connecting and Method Run()");
   }

}

}

這是服務器上接受客戶端並處理它們的基礎:)

服務器上的主類:

package libgdx.server;

import java.io.IOException;
import java.net.ServerSocket;

/**
 *
 * @author Saturn
 */
 public class MainServer {
 private static ServerSocket Server = null;
 private static boolean networking = true;


public static void main(String[] args) throws IOException{
    try{
        System.out.println("Server listening!");
        Server = new ServerSocket(4444);


    }catch (IOException io){
     System.err.println("Error while making ServerSocket!");
     System.exit(-1);
    }
    while (networking)
    System.out.println("Networking!");

        new NetworkingThread(Server.accept()).start();     

}

}

服務器協議:

  /*
 * To change this template, choose Tools | Templates
  * and open the template in the editor.
*/
package libgdx.server;

import java.util.Calendar;
import java.util.Date;

/**
*
* @author Saturn
*/
public class AlphaProtocol {
    private static final int CONNECTING = -1;

    private static final int MOVE = 0;
    private static final int NEW_PLAYER = 1;
    private int state  =  CONNECTING;
    private Calendar Date;
    private Date Time = Date.getTime();

public String ProcessInput(String input){
    String output = null;
    String name = null;
    String X= null,Y = null;

    if (input.equals("connect")){
      System.out.println("Connection granted!");

        output =  Time + ":" + "Got a Connection over here!";
    }
    if (input.equals("Bye!")){
        System.err.println("Client said Bye!");
    }
    return output;

}

}

現在這些是客戶端文件:

桌面:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package libgdx.test;

import com.badlogic.gdx.backends.jogl.JoglApplication;

 /**
  *
  * @author Saturn
  */
public class Desktop {
   public static void main(String[] args) {
    // TODO code application logic here
    new JoglApplication(new LibGDXTest(),"Test #1",640,480,false);

}

}

LibGDXTest:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
   package libgdx.test;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.jogl.JoglApplication;
import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import java.util.Scanner;

 /**
 *
 * @author Saturn
 */
  public class LibGDXTest implements ApplicationListener {
    SpriteBatch spriteBatch;
    Texture texture;
    BitmapFont font;
    Vector2 textPosition = new Vector2(100, 100);
    Vector2 textDirection = new Vector2(1, 1);
    String ip;
    int port;
    Networking net = new Networking();

    /**
    * @param args the command line arguments
    */


      @Override
    public void create() {
     Scanner in = new Scanner(System.in);
    System.out.println("Hello, welcome to LibGDX Network test: #1 ");
    System.out.println("Type the server ip in:");
   ip = in.next();
   System.out.println("Type the server port in:");
   port = in.nextInt();
   System.out.println("IP:" + ip + "Port:" + port);
   net.Connect(ip,port);

   }

    @Override
    public void resize(int i, int i1) {

  }

   @Override
   public void render() {

  }

    @Override
    public void pause() {

   }

   @Override
   public void resume() {

   }

   @Override
  public void dispose() {

   }

}

網絡:是在客戶端上實現所有網絡的類:)

   /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package libgdx.test;

   /**
    *
   * @author Saturn
   */
      import java.io.BufferedReader;
      import java.io.IOException;
       import java.io.InputStreamReader;
       import java.io.PrintWriter;
       import java.net.*;
     import java.util.logging.Level;
     import java.util.logging.Logger;

   public class Networking {

   private Socket Client;
    private  PrintWriter out;
   private BufferedReader in;


   public void Connect(String arg1, int arg2){
       try{
           Client = new Socket(arg1,arg2);
                out = new PrintWriter(Client.getOutputStream(),true);
             in = new BufferedReader(new InputStreamReader(Client.getInputStream()));

        }catch(UnknownHostException uhk) {
           System.err.println("Cannot find host:" + arg1);
           System.exit(-1);

      }catch (IOException ioe) {
           System.err.println("Cannot get I/O for the connection:" + arg1);
          System.exit(-1);

       }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromUser;
        String fromServer;
        try {
            while ((fromServer = in.readLine()) != null) {
              System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye!"))
                  break;

              fromUser = stdIn.readLine();
          if (fromUser != null) {
                  System.out.println("Client: " + fromUser);
                  out.println(fromUser);
           }
          }
       } catch (IOException ex) {
           Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
      }



   }
  }

當我運行應用程序時,

我得到這個:

   Client:    Hello, welcome to LibGDX Network test: #1 
   Type the server ip in:
   127.0.0.1 // My INPUT!
   Type the server port in:
   4444  // My INPUT!
   IP:127.0.0.1Port:4444

和服務器:

Server listening!
Networking!
Networking!
Networking!
Networking!
Networking!
Networking!
Networking!

它只是打印“網絡!”。

任何幫助,將不勝感激!

您的程序中有一個錯誤,如果使用調試器,這將是顯而易見的。 你寫

while (networking)
    System.out.println("Networking!");

由於networking永遠是真實的,因此這是無止境的。 我建議您使用IDE格式化並將{}放入循環中,直到您確信不需要它們為止。

請注意,在package libgdx.server; NetworkingThread類中package libgdx.server; 您正在調用outputLine = alpha.ProcessInput(null); 類AlphaProtocol的名稱,但是由於您提供null作為參數,因此該函數返回方法中定義的null。 由於變量輸入為空,沒有條件滿足,因此輸出始終保持為空,這是從此方法返回的結果。

public String ProcessInput(String input){
  String output = null;
  String name = null;
  String X= null,Y = null;

  if (input.equals("connect")){
    System.out.println("Connection granted!");

      output =  Time + ":" + "Got a Connection over here!";
  }
  if (input.equals("Bye!")){
      System.err.println("Client said Bye!");
  }
  return output;
}

因此,在接收到該空值的客戶端網絡類中,在您的這段代碼中while ((fromServer = in.readLine()) != null) , the Client shuts itself down. So please try to provide some String value in place of null here, while ((fromServer = in.readLine()) != null) , the Client shuts itself down. So please try to provide some String value in place of null here, outputLine = alpha.ProcessInput(“ Hello Client”);`

我想這就是您應該致力於使事情正常運行的問題。

問候`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM