Java 通过套接字发送多个变量?
我最近开始学习java中的Socket网络。所以我创建了一个多人游戏,可以在同一台计算机上玩,但我想让它成为网络多人游戏,然后我学习了套接字,现在,我想把玩家在游戏中的位置变量发送到服务器,然后服务器可以将该玩家放置在另一台机器上运行的其他游戏实例中的该位置。问题是,我只是失败了,所有的数据都没有被接收或读取。我也希望这个职位能不断地被发送和接收,这对我来说也是个问题 我尝试使用ObjectOutputStream和ObjectInputStream发送带有变量的int数组,但也失败了,所以您能告诉我怎么做吗,因为我不知道,而且我似乎无法在线找到答案Java 通过套接字发送多个变量?,java,sockets,networking,client,Java,Sockets,Networking,Client,我最近开始学习java中的Socket网络。所以我创建了一个多人游戏,可以在同一台计算机上玩,但我想让它成为网络多人游戏,然后我学习了套接字,现在,我想把玩家在游戏中的位置变量发送到服务器,然后服务器可以将该玩家放置在另一台机器上运行的其他游戏实例中的该位置。问题是,我只是失败了,所有的数据都没有被接收或读取。我也希望这个职位能不断地被发送和接收,这对我来说也是个问题 我尝试使用ObjectOutputStream和ObjectInputStream发送带有变量的int数组,但也失败了,所以您能
Thx作为最简单的解决方案,使用对象流将您创建的对象发送到存储这些坐标的位置,但此类必须实现可序列化接口。例如,对于二维坐标:
class Coords implements Serializable {
int x, y;
public Coords(int x, int y) {
this.x = x;
this.y = y;
}
}
...
// To write:
// oos = ObjectOutputStream of the socket
Coords tmp = new Coords(x, y);
oos.writeObject(tmp);
oos.flush();
...
//To read:
//ois = ObjectInputStream of the socket
Coords tmp = (Coords)ois.readObject();
也可以帮助您。作为最简单的解决方案,使用对象流将您创建的对象发送到存储这些坐标的位置,但此类必须实现可序列化接口。例如,对于二维坐标:
class Coords implements Serializable {
int x, y;
public Coords(int x, int y) {
this.x = x;
this.y = y;
}
}
...
// To write:
// oos = ObjectOutputStream of the socket
Coords tmp = new Coords(x, y);
oos.writeObject(tmp);
oos.flush();
...
//To read:
//ois = ObjectInputStream of the socket
Coords tmp = (Coords)ois.readObject();
也可以帮助您。尝试以下方法:
import java.io.*;
import java.net.*;
class Server extends Thread {
Server() throws IOException {
serverSocket = new ServerSocket(0);
}
public void run() {
while (true) {
try {
Socket client = serverSocket.accept();
Connect c = new Connect(client);
c.start();
} catch (Exception e) {}
}
}
final ServerSocket serverSocket;
}
class Data implements Serializable {
int[] data = { 1, 2, 3 };
}
class Connect extends Thread {
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch (Exception e1) {
try {
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return;
}
}
public void run() {
try {
oos.writeObject(new Data());
oos.flush();
ois.close();
oos.close();
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("done");
}
final Socket client;
ObjectInputStream ois;
ObjectOutputStream oos;
}
class Client {
Client(int port) {
this.port = port;
}
void connectAndRead() {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Socket socket = null;
Data data = null;
try {
socket = new Socket("127.0.0.1", port);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
data = (Data) ois.readObject();
oos.close();
ois.close();
for (int d : data.data)
System.out.println(d);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
final int port;
}
public class Main {
public static void main(String[] arguments) throws IOException, InterruptedException {
Server server = new Server();
server.start();
Client client = new Client(server.serverSocket.getLocalPort());
client.connectAndRead();
}
}
试着这样做:
import java.io.*;
import java.net.*;
class Server extends Thread {
Server() throws IOException {
serverSocket = new ServerSocket(0);
}
public void run() {
while (true) {
try {
Socket client = serverSocket.accept();
Connect c = new Connect(client);
c.start();
} catch (Exception e) {}
}
}
final ServerSocket serverSocket;
}
class Data implements Serializable {
int[] data = { 1, 2, 3 };
}
class Connect extends Thread {
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch (Exception e1) {
try {
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return;
}
}
public void run() {
try {
oos.writeObject(new Data());
oos.flush();
ois.close();
oos.close();
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("done");
}
final Socket client;
ObjectInputStream ois;
ObjectOutputStream oos;
}
class Client {
Client(int port) {
this.port = port;
}
void connectAndRead() {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Socket socket = null;
Data data = null;
try {
socket = new Socket("127.0.0.1", port);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
data = (Data) ois.readObject();
oos.close();
ois.close();
for (int d : data.data)
System.out.println(d);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
final int port;
}
public class Main {
public static void main(String[] arguments) throws IOException, InterruptedException {
Server server = new Server();
server.start();
Client client = new Client(server.serverSocket.getLocalPort());
client.connectAndRead();
}
}
到目前为止,您尝试了什么(代码)?为什么它没有像你期望的那样工作?有例外吗?如果有异常,我们可以看到堆栈跟踪吗?你上网有多努力?一个简单的谷歌搜索给了我数百个使用
ObjectOutputStream
/ObjectInputStream
和Socket
的例子。欢迎来到。请注意,我们确实希望看到源代码。:)谢谢我还没有把它放在我的游戏中,我想在我这么做之前确定它是如何工作的。到目前为止,你试过什么(代码)?为什么它没有像你期望的那样工作?有例外吗?如果有异常,我们可以看到堆栈跟踪吗?你上网有多努力?一个简单的谷歌搜索给了我数百个使用ObjectOutputStream
/ObjectInputStream
和Socket
的例子。欢迎来到。请注意,我们确实希望看到源代码。:)谢谢实际上,我还没有把它放在我的游戏中,我想先确定它是如何工作的。你必须以相反的顺序创建这些对象流:先输出,然后输入。大多数代码来自-它确实工作,但这只是一种方式。如果你在两端都按顺序操作,你会遇到死锁。如果我放置int I=ois.read();在Connect.run()中的oos.flush()和oos.write(42)之后;在connectAndRead()中读取对象之后,它似乎仍然有效。我看不出这有什么关联,但我重复一遍。如果在两端的ObjectOutputStream之前创建ObjectInputStream,它将死锁。这就是为什么您应该首先创建ObjectOutputStream。然后它就不会死锁,即使你不能控制另一端的代码。你必须以相反的顺序创建这些对象流:先输出,然后输入。大多数代码都来自-它确实可以工作,但这只是一种方式。如果你在两端都按顺序操作,你会遇到死锁。如果我输入int i=ois.read();在Connect.run()中的oos.flush()和oos.write(42)之后;在connectAndRead()中读取对象之后,它似乎仍然有效。我看不出这有什么关联,但我重复一遍。如果在两端的ObjectOutputStream之前创建ObjectInputStream,它将死锁。这就是为什么您应该首先创建ObjectOutputStream。这样它就不会死锁,即使您无法控制另一端的代码。