读取和写入java中的连接(套接字)

时间:2014-03-19 15:27:58

标签: java multithreading sockets communication

我正在进行2D多人游戏。我使用套接字制作了一些简单的应用程序,例如聊天室。现在我正试图在我的游戏中实现它并没有太大的成功。也许我不理解循环或正确的读写?如果我调用读取函数,读者是否会等待数据? 我想做的事情:2名能够看到彼此位置的球员(并在那里画出一个黑色方块)。我得到了:另一个玩家在屏幕上随意地跳来跳去。 (但至少创建了连接) 我试过这样的: 客户端(不是整个代码,只是不确定的部分,连接):

class Client {
        private DataInputStream reader;    //I do make these somewhere in my code, these are chained(?) tho the socket
    private DataOutputStream writer;
        int ox, oy;

        private void tick(double delta) {   //logic of the game
        player.move();     //just move the player (client sided)
        try {
            writer.writeInt(ID);      //send player id
            writer.writeInt(player.getX());       //sent the player's x
            writer.writeInt(player.getY());       //same with y
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        class msgReader implements Runnable {      //A new thread for reading the server messages
        public void run() {
            try {
                ID = reader.readInt(); //When the connection is created, player gets an id from server (this actually works) 
                System.out.println(ID);

                while(true) {          //reading loop, maybe this is the wrong part
                    int oid = reader.readInt();  //read an id 
                    if (oid != ID){    //if this is not the player's id
                        ox = reader.readInt();  //store other's x
                        oy = reader.readInt();  //same with y
                    } else {
                        reader.readInt();  //else this is our player
                        reader.readInt();  //so we don't need the data just read it
                    }
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

服务器:

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();
    }
}

有人可以解释我应该采取哪些不同的做法?还有一种更有效的方式来发送数据(一体式?)。怎么样?

0 个答案:

没有答案