在java中读取和写入连接(套接字)
我正在做一个2d多人游戏。我使用套接字制作了一些简单的应用程序,例如聊天室。现在我正试图将它应用到我的游戏中,但没有取得多大成功。也许我不理解循环或者读写不正确?如果调用read函数,读卡器是否等待数据? 我想做的是:两个玩家可以看到对方的位置(并在那里画一个黑色正方形)。我得到的:另一个玩家在屏幕上随机跳跃。(但至少已创建连接) 我试着这样做: 客户端(不是整个代码,只是不确定的部分和连接): 服务器:在java中读取和写入连接(套接字),java,multithreading,sockets,communication,Java,Multithreading,Sockets,Communication,我正在做一个2d多人游戏。我使用套接字制作了一些简单的应用程序,例如聊天室。现在我正试图将它应用到我的游戏中,但没有取得多大成功。也许我不理解循环或者读写不正确?如果调用read函数,读卡器是否等待数据? 我想做的是:两个玩家可以看到对方的位置(并在那里画一个黑色正方形)。我得到的:另一个玩家在屏幕上随机跳跃。(但至少已创建连接) 我试着这样做: 客户端(不是整个代码,只是不确定的部分和连接): 服务器: public class ServerBase { ServerSocket se
public class ServerBase {
ServerSocket serverSocket;
ArrayList clients; //for the client writers
private int id = 1;
class ClientHandler implements Runnable { //Every client gets a thread
private Socket soc;
private DataInputStream reader;
private DataOutputStream writer;
private int x;
private int y;
private int id;
public ClientHandler(Socket s, DataOutputStream wr) {
soc = s;
writer = wr;
try {
reader = new DataInputStream(soc.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(true) { //Maybe this is wrong too, I'm jut unsure of this
id = reader.readInt(); //get the id (first thing that player sends)
x = reader.readInt(); //same with x and y
y = reader.readInt();
sendEveryone(id, x, y); //send everyone the player datas
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendEveryone(int a, int b, int c) {
Iterator it = clients.iterator();
while(it.hasNext()){
try {
DataOutputStream writer = (DataOutputStream) it.next();
writer.writeInt(a);
writer.writeInt(b);
writer.writeInt(c);
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public void init() {
clients = new ArrayList();
try {
serverSocket = new ServerSocket(5099);
while(true) {
Socket clientSocket = serverSocket.accept(); //wait for a client
System.out.println("got one");
DataOutputStream clientWriter = new DataOutputStream(clientSocket.getOutputStream());
clients.add(clientWriter);
clientWriter.writeInt(id);
id++;
Thread t = new Thread(new ClientHandler(clientSocket, clientWriter)); //make the thread for the client
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerBase().init();
}
}
有人能解释一下我应该怎么做?还有没有一种更有效的方式来发送数据(在一块中?)。怎么做?看看我在生产中使用的
java.util.rmi
,它与套接字完全纠结在一起。在中,(true)
是否每次都创建一个t线程?它只存在于while范围内?如果调用read函数,读卡器是否等待数据?对阅读积木。不,它基本上永远存在。每个客户都有一条线索。@Elliott Frisch很有用,谢谢。
public class ServerBase {
ServerSocket serverSocket;
ArrayList clients; //for the client writers
private int id = 1;
class ClientHandler implements Runnable { //Every client gets a thread
private Socket soc;
private DataInputStream reader;
private DataOutputStream writer;
private int x;
private int y;
private int id;
public ClientHandler(Socket s, DataOutputStream wr) {
soc = s;
writer = wr;
try {
reader = new DataInputStream(soc.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(true) { //Maybe this is wrong too, I'm jut unsure of this
id = reader.readInt(); //get the id (first thing that player sends)
x = reader.readInt(); //same with x and y
y = reader.readInt();
sendEveryone(id, x, y); //send everyone the player datas
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendEveryone(int a, int b, int c) {
Iterator it = clients.iterator();
while(it.hasNext()){
try {
DataOutputStream writer = (DataOutputStream) it.next();
writer.writeInt(a);
writer.writeInt(b);
writer.writeInt(c);
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public void init() {
clients = new ArrayList();
try {
serverSocket = new ServerSocket(5099);
while(true) {
Socket clientSocket = serverSocket.accept(); //wait for a client
System.out.println("got one");
DataOutputStream clientWriter = new DataOutputStream(clientSocket.getOutputStream());
clients.add(clientWriter);
clientWriter.writeInt(id);
id++;
Thread t = new Thread(new ClientHandler(clientSocket, clientWriter)); //make the thread for the client
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerBase().init();
}
}