ashur
ashur

Reputation: 4307

Java: XML object

I'm making simple peer to peer game and I decided to use XML to send information over sockets(example below). But I'm not sure how to send it ? I should simply use ObjectOutputStream.writeObject(obj) and as parameter use object from my example ? Mainly I'm asking, how does look proper sending XML objects over sockets.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class SendIPObject {
    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance(IPSender.class);

        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Player object = new Player();
        object.setID(0);
        object.setIP("192.167.211.167");

        m.marshal(object, System.out);

    }
}






import java.io.Serializable;

abstract public class Player implements Serializable{
    private String ip;
    private int id;

    public String getIP() {
        return ip;
    }

    public int getID() {
        return id;
    }

    public void setIP(String ip) {
        this.ip = ip;
    }

    public void setID(int id) {
        this.id = id;
    }

}

Upvotes: 2

Views: 526

Answers (2)

Jacob Schoen
Jacob Schoen

Reputation: 14192

If the sender and receiver are both java based, you can actually skip the converting to xml back and forth, and send the object over ObjectInputStream. This is essentially an alternative to using xml. You could also use the ObjectInputStream with the String class if you wanted to.

I changed your Player class slightly to to be concrete and have added a toString() method to show what was received on both ends.

Here is how the client looks:

public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {

        Player player = new Player("192.168.1.10", 1);
        //create connection
        Socket sock = new Socket("127.0.0.1", 8080);
        //get teh output stream to send in over
        ObjectOutputStream cout = new ObjectOutputStream(sock.getOutputStream());
        //send it 
        System.out.println("Sending: "+player.toString());
        cout.writeObject(player);
        //close connection
        sock.close();
        System.out.println("Closed");
    }

}

Here is how the Server looks:

public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true) {
            try {
                System.out.print("\nserver>> ");

                // listen for connection from client
                Socket sock = serverSocket.accept();
                System.out.println("CONNECTION ACCEPTED");

                ObjectInputStream sin = new ObjectInputStream(sock.getInputStream());

                //read the player in
                Player player = (Player) sin.readObject();
                System.out.println("from Client>> " + player.toString());

                // close the connection
                sock.close();
                System.out.println("server>> CONNECTION CLOSED");
                System.exit(0);
            } catch (Exception e) {
            }
        }
    }    
}

Upvotes: 2

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

XML is sent as plain string. So first step would be to create clients that can send and receive from each other strings, e.g. "Hello, world.". Next you need to create an object and using JAXB convert to String and send. Than receive it on other client and using JAXB again. This way you can easily debug what you send and receive. Once you've done it you can try to avoid converting to temporary xml string and use socket streams directly on marshalling and unmarshalling.
I think you shouldn't use ObjectOutputStream because it serializes object to byte array. But you need to "serialize" your object to XML (string) and send it.
Here you can see how can you marshal object to java String: I want to convert an output stream into String object

Upvotes: 3

Related Questions