Java: 套接字未获得输入
Java: socket not getting input
所以,我正在做一个井字游戏项目,我遇到了网络部分的问题,它已经全部完成,只缺少连接玩家彼此的部分,这是 class 问题:
public class Enemy implements Runnable{
private static Socket enemy;
public Enemy(Socket sock){
enemy = sock;
}
public static void passaJogada(int xPos, int yPos){
try {
PrintWriter saida = new PrintWriter(enemy.getOutputStream());
String x = "" + xPos;
saida.println(x);
String y = "" + yPos;
System.out.print(x + y);
saida.println(y);
} catch (IOException e) {
System.out.println("Ocorreu um erro!");
}
}
public void run() {
try {
BufferedReader entrada = new BufferedReader(new InputStreamReader(enemy.getInputStream()));
while(!EndGameWindow.getEnd()) {
int x = Integer.parseInt(entrada.readLine());
int y = Integer.parseInt(entrada.readLine());
GameWindow.gameButton[x][y].fazerJogada(x,y);
}
entrada.close();
} catch (IOException e) {
System.out.println("Um errro ocorreu!");
}
}
}
我不知道发生了什么,我只知道 PrintWriter 正在写入,但 BufferedReader 没有读取。
忽略变量和方法的葡萄牙语名称即可。
请参阅 PrintWriter 的 API,特别是您正在使用的单个参数 OutputStream
构造函数:
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
换句话说,PrintWriter
被缓冲,需要被刷新。要启用自动行刷新,请使用适当的构造函数
PrintWriter saida = new PrintWriter(enemy.getOutputStream(), true);
...或显式刷新 PrintWriter:
....
saida.println(y);
saida.flush();
我假设您希望玩家互相玩游戏,即使他们 none 坐在服务器 "game" 所在的计算机上。在那种情况下,您应该做的是将游戏与网络分开。我还将假设您的所有游戏部分都按应有的方式工作。所以这是你可以做的。
1)首先创建一个class,其中只有连接到服务器的部分发生,你可以将它命名为服务器(看下面的示例代码)。
2) 制作另一个 class 来制作这些客户的对象(在您的例子中是玩家)。(再次查看下面的代码)
3) 然后你可以让他们与游戏互动。
如果您在修复它时遇到问题,请告诉我,也许我可以为您提供更详细的帮助。
你这个简单的服务器部分。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class Server {
ServerSocket serverSocket;
public static List<ClientConnection> clientConnectionList = new ArrayList<ClientConnection>();
public Server() {
try {
serverSocket = new ServerSocket(12345);
System.out.println("Server started");
} catch (IOException ioe) {
System.out.println("Something went wrong");
}
}
public void serve(){
Socket clientSocket;
try {
while((clientSocket = serverSocket.accept()) != null){
System.out.println("got client from"+clientSocket.getInetAddress());
ClientConnection clientConnection = new ClientConnection(clientSocket);
clientConnectionList.add(clientConnection);
ClientHandler clientHandler = new ClientHandler(clientConnection,serverSocket);
clientHandler.start();
}
}catch (IOException ioe){
System.out.println("Something went wrong");
}
}
}
本例中的 clientConnection 可以是您应该创建的播放器对象,只是为了让您自己更轻松。
clientHandler是可以同时处理每个玩家的class。
这里的"clientConnectionList"只是为了存储连接,所以你知道在哪里可以找到它们。
这里是 ClientConnection class。
import java.io.*;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientConnection {
private Socket connection;
private DataInputStream streamIn;
private DataOutputStream streamOut;
private BufferedInputStream bufferedInputStream;
public ClientConnection(Socket socket){
connection = socket;
try{
bufferedInputStream = new BufferedInputStream(connection.getInputStream());
streamIn = new DataInputStream(bufferedInputStream);
streamOut = new DataOutputStream(connection.getOutputStream());
}catch (IOException ioe){
System.out.println("Something went wrong when trying create the Connection Object");
}
}
public boolean isBound(){
return connection.isBound();
}
public DataInputStream getStreamIn() {
return streamIn;
}
public DataOutputStream getStreamOut() {
return streamOut;
}
public void close(){
try{
connection.close();
}catch (IOException ioe){
System.out.print("Something went wrong trying to close the connection");
}
}
public String getAddress(){
return connection.getInetAddress().toString();
}
}
这是 ClientHandler Class
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientHandler extends Thread{
private ServerSocket serverSocket;
String data;
ClientConnection connection;
public ClientHandler(ClientConnection clientConnection, ServerSocket serverSocket) {
connection = clientConnection;
}
public void makeMove(String data){
//This is where you put the logic for how the server can
//interpetate the readed data to a game move
}
@Override
public void run(){
System.out.println("ClientHandler running");
try{
while(connection.isBound()){
data= connection.getStreamIn().readUTF();
this.makeMove(data);
}
connection.close();
}catch (IOException ioe){
System.out.println("Connection dead "+connection.getAddress());
Server.clientConnectionList.remove(connection);
}
}
}
请告诉我您的效果如何。也许我可以更好地帮助你 :) 祝你好运 :)
所以,我正在做一个井字游戏项目,我遇到了网络部分的问题,它已经全部完成,只缺少连接玩家彼此的部分,这是 class 问题:
public class Enemy implements Runnable{
private static Socket enemy;
public Enemy(Socket sock){
enemy = sock;
}
public static void passaJogada(int xPos, int yPos){
try {
PrintWriter saida = new PrintWriter(enemy.getOutputStream());
String x = "" + xPos;
saida.println(x);
String y = "" + yPos;
System.out.print(x + y);
saida.println(y);
} catch (IOException e) {
System.out.println("Ocorreu um erro!");
}
}
public void run() {
try {
BufferedReader entrada = new BufferedReader(new InputStreamReader(enemy.getInputStream()));
while(!EndGameWindow.getEnd()) {
int x = Integer.parseInt(entrada.readLine());
int y = Integer.parseInt(entrada.readLine());
GameWindow.gameButton[x][y].fazerJogada(x,y);
}
entrada.close();
} catch (IOException e) {
System.out.println("Um errro ocorreu!");
}
}
}
我不知道发生了什么,我只知道 PrintWriter 正在写入,但 BufferedReader 没有读取。
忽略变量和方法的葡萄牙语名称即可。
请参阅 PrintWriter 的 API,特别是您正在使用的单个参数 OutputStream
构造函数:
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
换句话说,PrintWriter
被缓冲,需要被刷新。要启用自动行刷新,请使用适当的构造函数
PrintWriter saida = new PrintWriter(enemy.getOutputStream(), true);
...或显式刷新 PrintWriter:
....
saida.println(y);
saida.flush();
我假设您希望玩家互相玩游戏,即使他们 none 坐在服务器 "game" 所在的计算机上。在那种情况下,您应该做的是将游戏与网络分开。我还将假设您的所有游戏部分都按应有的方式工作。所以这是你可以做的。 1)首先创建一个class,其中只有连接到服务器的部分发生,你可以将它命名为服务器(看下面的示例代码)。 2) 制作另一个 class 来制作这些客户的对象(在您的例子中是玩家)。(再次查看下面的代码) 3) 然后你可以让他们与游戏互动。
如果您在修复它时遇到问题,请告诉我,也许我可以为您提供更详细的帮助。
你这个简单的服务器部分。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class Server {
ServerSocket serverSocket;
public static List<ClientConnection> clientConnectionList = new ArrayList<ClientConnection>();
public Server() {
try {
serverSocket = new ServerSocket(12345);
System.out.println("Server started");
} catch (IOException ioe) {
System.out.println("Something went wrong");
}
}
public void serve(){
Socket clientSocket;
try {
while((clientSocket = serverSocket.accept()) != null){
System.out.println("got client from"+clientSocket.getInetAddress());
ClientConnection clientConnection = new ClientConnection(clientSocket);
clientConnectionList.add(clientConnection);
ClientHandler clientHandler = new ClientHandler(clientConnection,serverSocket);
clientHandler.start();
}
}catch (IOException ioe){
System.out.println("Something went wrong");
}
}
}
本例中的 clientConnection 可以是您应该创建的播放器对象,只是为了让您自己更轻松。
clientHandler是可以同时处理每个玩家的class。
这里的"clientConnectionList"只是为了存储连接,所以你知道在哪里可以找到它们。
这里是 ClientConnection class。
import java.io.*;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientConnection {
private Socket connection;
private DataInputStream streamIn;
private DataOutputStream streamOut;
private BufferedInputStream bufferedInputStream;
public ClientConnection(Socket socket){
connection = socket;
try{
bufferedInputStream = new BufferedInputStream(connection.getInputStream());
streamIn = new DataInputStream(bufferedInputStream);
streamOut = new DataOutputStream(connection.getOutputStream());
}catch (IOException ioe){
System.out.println("Something went wrong when trying create the Connection Object");
}
}
public boolean isBound(){
return connection.isBound();
}
public DataInputStream getStreamIn() {
return streamIn;
}
public DataOutputStream getStreamOut() {
return streamOut;
}
public void close(){
try{
connection.close();
}catch (IOException ioe){
System.out.print("Something went wrong trying to close the connection");
}
}
public String getAddress(){
return connection.getInetAddress().toString();
}
}
这是 ClientHandler Class
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientHandler extends Thread{
private ServerSocket serverSocket;
String data;
ClientConnection connection;
public ClientHandler(ClientConnection clientConnection, ServerSocket serverSocket) {
connection = clientConnection;
}
public void makeMove(String data){
//This is where you put the logic for how the server can
//interpetate the readed data to a game move
}
@Override
public void run(){
System.out.println("ClientHandler running");
try{
while(connection.isBound()){
data= connection.getStreamIn().readUTF();
this.makeMove(data);
}
connection.close();
}catch (IOException ioe){
System.out.println("Connection dead "+connection.getAddress());
Server.clientConnectionList.remove(connection);
}
}
}
请告诉我您的效果如何。也许我可以更好地帮助你 :) 祝你好运 :)