我正处于使用套接字和服务器套件等制作双人游戏的早期阶段。我已经编写了游戏和客户端并且没有编译错误,但问题是它们何时运行。当我在控制台中收到连接成功的消息时,它们将连接。但是,他们假设只是为了测试连接来回反弹消息,但事实并非如此。有人可以看看吗?当我按照自己的意愿结束一个或另一个时,我确实会遇到异常。这是代码:
服务器:
public class Server
{
private ServerSocket listener;
public Server()
{
try
{
listener = new ServerSocket(25565);
}
catch (IOException e)
{
System.err.println("Uknown IOException when creating the server socket; " + e.getMessage());
}
}
public void startServer()
{
try
{
Game game = new Game();
while (true)
{
Socket socket = listener.accept();
System.out.println("Connection " + socket.getLocalSocketAddress() + " was accepted!");
Player player = new Player(socket);
new Thread(player).start();
if (game.one == null)
game.one = player;
else if (game.two == null)
game.two = player;
else
player.rejectConnection();
}
}
catch (IOException e)
{
System.err.println("Uknown IOException when starting server; " + e.getMessage());
}
}
public static void main(String[] args)
{
new Server().startServer();
}
class Player implements Runnable
{
private BufferedReader in;
private PrintWriter out;
public Player(Socket socket)
{
try
{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
}
catch (IOException e)
{
System.err.println("Uknown IOException when creating instance of player; " + e.getMessage());
}
}
public void send(String message)
{
out.println(message);
}
public void rejectConnection()
{
out.println("REFUSECON");
}
public void run()
{
try
{
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
out.println("RECIVED");
}
}
catch (IOException e)
{
System.err.println("Uknown IOException when listening on player; " + e.getMessage());
}
}
}
class Game
{
private Player one = null;
private Player two = null;
public Game()
{
}
}
}
客户端:
public class Client
{
private Socket socket;
private PrintWriter out;
public static void main(String[] args)
{
new Client();
}
public Client()
{
try
{
socket = new Socket("localhost", 25565);
out = new PrintWriter(socket.getOutputStream());
Listener listener = new Listener(socket);
new Thread(listener).start();
this.send("Hello World!");
}
catch (UnknownHostException e)
{
System.err.println("Uknown UnknownHostException when connecting to the server; " + e.getMessage());
}
catch (IOException e)
{
System.err.println("Uknown IOException when connecting to the server; " + e.getMessage());
}
}
public void send(String message)
{
out.println(message);
}
class Listener implements Runnable
{
private BufferedReader in;
public Listener(Socket socket)
{
try
{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (IOException e)
{
System.err.println("Uknown IOException when trying to listen client side; " + e.getMessage());
}
}
public void run()
{
String line;
try
{
while ((line = in.readLine()) != null)
{
System.out.println(line + " - In Client");
}
}
catch (IOException e)
{
System.err.println("Uknown IOException when trying to listen client side; " + e.getMessage());
}
}
}
}
提前谢谢你。我希望你解释一下我做错了什么,请不要纠正它,因为它更多的是学习如何做到这一点,而不是让程序运作。
答案 0 :(得分:0)
PrintWriters没有启用自动刷新功能。每次out.flush()
后尝试out.println()
。或者,在创建PrintWriter时将true
作为第二个参数传递。