Ezen
Ezen

Reputation: 13

Sending multiple variables through a socket?

I recently started learning networking with sockets in java. So i've created a multiplayer game that is playable on the same computer but i wanted to make it network-multiplayer, then i learned about sockets, now, i want to send the variables of the position of the player in the game to a server which then can place that player in that position in the other game instance running on a different machine. And the thing is, i just fail at it and all the data doesnt get received or read. I also want the position to get sent and received constantly which is also a problem for me...

I tried using ObjectOutputStream and ObjectInputStream to send a int array with the variables but that also failed, so could you please show me how to do this, because i have no idea and i cant seem to find an answer online.

Thx

Upvotes: 1

Views: 8360

Answers (2)

Ray Tayek
Ray Tayek

Reputation: 10003

try something like this:

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

Upvotes: 1

Adonais
Adonais

Reputation: 1292

As the easiest solution, use the Object Streams to send an object created by you where you store these coordinates, but this class must implement Serializable interface. For example for 2d coordinates:

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/ can also aid you.

Upvotes: 4

Related Questions