【问题标题】:Sending multiple variables through a socket?通过套接字发送多个变量?
【发布时间】:2011-12-23 23:31:20
【问题描述】:

我最近开始学习使用 java 中的套接字进行网络连接。所以我创建了一个可以在同一台计算机上玩的多人游戏,但我想让它成为网络多人游戏,然后我了解了套接字,现在,我想将游戏中玩家位置的变量发送到服务器然后可以将该玩家放置在另一台机器上运行的另一个游戏实例中的那个位置。问题是,我只是失败了,所有数据都没有被接收或读取。我还希望职位不断发送和接收,这对我来说也是一个问题......

我尝试使用 ObjectOutputStream 和 ObjectInputStream 发送一个带有变量的 int 数组,但也失败了,所以请你告诉我如何做到这一点,因为我不知道,我似乎无法在网上找到答案。

谢谢

【问题讨论】:

  • 到目前为止你尝试了什么(代码)?为什么它没有按您的预期工作?有例外吗?如果有异常,我们能看到堆栈跟踪吗?你在网上看有多难?一个简单的谷歌搜索给了我数百个使用ObjectOutputStream/ObjectInputStreamSocket的例子。
  • 欢迎来到Stack Overflow。请注意,我们真的很喜欢看源代码。 :) 谢谢!
  • 我还没有真正将它放入我的游戏中,我想在我这样做之前确定它是如何工作的。
  • I BRING YOU “JOHNNYLASER”(我从 2011 年开始的第一款真正的多平台单人太空射击游戏))抱歉耽搁了,

标签: java sockets networking client


【解决方案1】:

作为最简单的解决方案,使用 Object Streams 将您创建的对象发送到您存储这些坐标的位置,但此类必须实现 Serializable 接口。例如二维坐标:

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

http://java.sun.com/developer/technicalArticles/ALT/sockets/ 也可以帮助你。

【讨论】:

    【解决方案2】:

    试试这样的:

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

    【讨论】:

    • 您必须以相反的顺序创建这些对象流:先输出,然后输入。
    • 大部分代码来自java.sun.com/developer/technicalArticles/ALT/sockets - 它确实有效,但它只是一种方式。
    • 如果你在两端都按照你的顺序做,你会陷入僵局。
    • 如果我放一个 int i=ois.read();在 Connect.run() 中的 oos.flush() 和 oos.write(42) 之后;在 connectAndRead() 中读取对象之后,它似乎仍然有效。
    • 我看不出这有什么相关性,但我再说一遍。如果在两端的 ObjectOutputStream 之前创建 ObjectInputStream,则会死锁。这就是您应该首先创建 ObjectOutputStream 的原因。那么它就不会死锁,即使你无法控制另一端的代码。
    猜你喜欢
    • 2012-08-09
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多