gpt4 book ai didi

java - 服务器没有响应

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:57 24 4
gpt4 key购买 nike

我正在用 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();

}

}

服务器协议(protocol):

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

}

}

LibGDX测试:

/*
* 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() {

}

}

Networking:是处理客户端所有网络的类:)

   /*
* 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 格式并为循环放置 {},直到您确信不需要它们为止。

关于java - 服务器没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8545754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com