Jump to content

Search the Community

Showing results for tags 'tcp'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • RuneLocus
    • News & Announcements
    • Information Booth
    • Website Support
    • Introductions
    • Chat
    • Forum Games
  • RuneScape Development
    • RSPS General
    • RuneScape Private Server (RS2)
    • RuneScape Private Server (503+)
    • RuneScape Private Server (EoC 742+)
  • Other
    • Digital Art
    • Gaming
    • Webdevelopment
    • Computers
  • Marketplace
    • RuneScape Market
    • RSPS Market
    • Others Market
    • Freelance Middleman Services
  • Super Secret Club's Topics

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start



Found 1 result

  1. [I]Table of contents[/I]: [LIST=1] [*]Introduction [*]Basics [*]Preparing objects for seralization [*]Code Snippets [*]Other [/LIST] [B][SIZE=4]1. Introduction[/SIZE][/B] With [URL="http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html"]Java Sockets[/URL] you can send things like raw text, bytes, UTF, and class instances (objects). In this thread we are going to focus on sending and receiving objects. It's actually not as easy as you might think. [SIZE=4][B]2. Basics[/B][/SIZE] To send and receive objects via sockets you need to have an [URL="http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html"]ObjectInputStream[/URL] and an [URL="http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html"]ObjectOutputStream[/URL] and ofcourse a socket. When working with these you might encounter quite some obstacles that can take very long to get rid of. I too have spent many hours debugging and trying several things. First of all, when creating new instances of the ObjectOutputStream and the ObjectInputStream you have to make sure the ObjectOutputStream comes [B]FIRST[/B] and then the ObjectInputStream. This is because the ObjectInputStream will wait until there is an ObjectOutputStream in existance. This is commonly referenced to as the "Chicken and the egg" problem. [I][B]See Snippet 1.[/B][/I] Secondly, when sending an object, you always have to send an UTF header first. Java uses [URL="http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8"]Modified UTF-8[/URL] to work with serialized data. Using this header which can be anything (I mostly just send a random letter as the header) you tell the other side that it should now expect an object to come in. [B][I]See Snippet 2[/I][/B] To receive an object you simply have to read it from the ObjectInputStream and then check its name and cast it to the class. [B][I]See Snippet 3.[/I][/B] [B][SIZE=4]3. Preparing objects for serialization[/SIZE][/B] Serialization is required in order to be able to send objects via sockets. To do this you simply implement the [URL="http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html"]Serializable[/URL] interface to the class of the object you want to send. You can do everything you want with the class as transient variables and methods (including getters and setters) are not serialized. This means that only variables get serialized. [B][I]See Snippet 4.[/I][/B] Object classes have to be in the same package on both sides too otherwise the receiving side will throw an [URL="http://docs.oracle.com/javase/7/docs/api/java/io/OptionalDataException.html"]OptionalDataException[/URL] that basically means the received data cannot be used. [B][I]See image below[/I][/B]. [IMG]http://cdn.codiction.com/sockets.png[/IMG] [spoiler=4. Code Snippets] [B][SIZE=4]4. Code Snippets[/SIZE][/B] [I][SIZE=2][COLOR="#FF0000"]Note: these snippets are pseudo language and thus cannot be used without modification.[/COLOR][/SIZE][/I] [B]Snippet 1:[/B] [CODE] public void createStreams(Socket s) { ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); } [/CODE] [B]Snippet 2:[/B] [CODE] public void sendObject(Object o) { out.sendUTF("bla"); //you can send anything out.sendObject(o); out.flush(); //this is used to guarantee that everything gets sent. } [/CODE] [B]Snippet 3:[/B] [CODE] (inside a method): String utf; while((utf = in.readUTF()) != null) { parseObject(in.readObject()); } private void parseObject(Object o) { if(o.getClass().getSimpleName().equals("name of the class, for example Test")) { Test t = (Test) o; //t will have all the contents that it had at the other socket (client & server application) } } [/CODE] [B]Snippet 4:[/B] [CODE] public class Bla implements Serializable { int lol = 2; transient int lol2 = 5; //will not be sent public void test() { } } [/CODE] [/SPOILER] [B][SIZE=4]5. Other[/SIZE][/B] This is a basic introduction to the Java ObjectInputStream and ObjectOutputStream. There are many possibilities and things I haven't covered here but they are more advanced. If you have any questions you can ask them here.