2011-12-23 25 views
1

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

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

THX

+2

你尝试过什么到目前为止(代码)?为什么它没有按照你的预期工作?有例外吗?如果有例外,我们可以看到堆栈轨迹吗?你有多难看上网?一个简单的Google搜索给了我数百个使用'ObjectOutputStream' /'ObjectInputStream'和'Socket'的例子。 – Jeffrey 2011-12-23 23:38:05

+0

欢迎来到[SO]。请注意,我们非常喜欢看到源代码。 :) 谢谢! – sarnold 2011-12-23 23:59:22

+0

我还没有真正把它放在我的游戏中,我想确保它在我做之前是如何工作的。 – Ezen 2011-12-24 13:59:39

回答

4

作为最简单的方法,使用对象流送您存储这些坐标由您创建一个对象,但这个类必须实现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

尝试这样:

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

您必须以相反的顺序创建这些对象流:先输出然后输入。 – EJP 2011-12-24 01:30:55

+0

大部分代码来自http://java.sun.com/developer/technicalArticles/ALT/sockets/ - 它确实有效,但它只是一种方式。 – 2011-12-24 01:38:07

+0

如果你在你的订单中这样做,你会陷入僵局。 – EJP 2011-12-26 21:31:41