通过套接字发送多个变量?

时间:2011-12-23 23:31:21

标签: java sockets networking client

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

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

THX

2 个答案:

答案 0 :(得分:4)

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

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/也可以帮助你。

答案 1 :(得分:1)

尝试这样的事情:

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