This discussion is archived
9 Replies Latest reply: Jul 10, 2012 8:35 AM by gimbal2 RSS

Send Object with socket

tmazight Newbie
Currently Being Moderated
hi,

I have defined objects which correspond to my messages, those object have only "byte" attributes and are realizable, exemple

     private byte[]      source;
     private byte[]      destination=new byte[2];
     private byte[]      n_emetteur=new byte[2];
     private byte[]      n_message=new byte[2]; ...................

my problem is when i send and object by in.writeObject(myObject) i can't read it in the other hand, because my client is written with C language and i recieve only the name of my class package.package.myclass.

can you help my find out best way to send my objects and be compatible with any client?

thank's
  • 1. Re: Send Object with socket
    gimbal2 Guru
    Currently Being Moderated
    Don't use writeObject() to write out the array (java Object), use write() to write the bytes in the array itself.

    EDIT: expanded that a little
  • 2. Re: Send Object with socket
    tmazight Newbie
    Currently Being Moderated
    but the problem is that write() can't take an object as a parameter, is there an easy way to get the values of my object attributes and create a byte[] array with that?
    thx
  • 3. Re: Send Object with socket
    EJP Guru
    Currently Being Moderated
    It can take the array as a parameter. You will need to send the length first so that you can get it at the receiver, allocate an array of the right size, and read it via readFully().
  • 4. Re: Send Object with socket
    tmazight Newbie
    Currently Being Moderated
    that will be good, but with my program i'm writing a new server, and the client exist already and it is not modifiable, so i can't use readFully,
    my idea is to get the values of my object(who have already byte attributes) and put them in a byte array [] before using sinple in.write(), but sincerly i have no idea how can i do that, i'm always searching for a solution .

    thanks anyway :)
  • 5. Re: Send Object with socket
    EJP Guru
    Currently Being Moderated
    Then you can't use `ObjectOutputStream` at all, you have to conform to whatever wire protocol the client is already expecting.

    NB your pointless duplicate of this thread has been deleted.
  • 6. Re: Send Object with socket
    tmazight Newbie
    Currently Being Moderated
    I noticed that :D

    but i don't think it's a duplicate one, because i realized that what i need don't concern networking, but serializing, because i can use neither writeObject nor write().

    and I was searching for a way to get my object values and put them in a byte array.

    thanks anyway.
  • 7. Re: Send Object with socket
    gimbal2 Guru
    Currently Being Moderated
    and I was searching for a way to get my object values and put them in a byte array.
    Searching you do through Google, you don't do it by posting a nearly identical "how I do this" question in this forum.
  • 8. Re: Send Object with socket
    tmazight Newbie
    Currently Being Moderated
    i post this after a large search on google, and how can google get its content if peaple can't post on forums!!!!

    ok, anyway sorry if i did something wrong, my intention was not to flood the forum with posts
  • 9. Re: Send Object with socket
    gimbal2 Guru
    Currently Being Moderated
    tmazight wrote:
    i post this after a large search on google, and how can google get its content if peaple can't post on forums!!!!
    That's your biggest problem right there: you seem to have the strange idea that the answer must come from a forum; even Google must find the answer there, like there is nothing else but a forum to find answers in. But no, it doesn't work that way. Where did those people you expect to spit out the solution get their knowledge from huh?

    1) books
    2) articles
    3) courses (for example by going to school)
    4) experimentation
    5) thought

    I'm pretty sure that in stead of having this need to get an answer from a forum you would in fact sit down and think about it, you will manage to solve it yourself. If you can't then I'm afraid that even your basic knowledge if Java is severely lacking and you simply need to brush up on Java using a good book.

    Start by following the hint EJP already gave you. Apparently you have some client which expects data in a very specific way which is throwing up all these boundaries that you can't seem to be able to break. HOW is that client expecting it to be? And I'm talking about specifics. Its all bytes, in what way does the client expect the bytes to be. Given that starting point, how would you be able to take what you know about Java (which will really boil down to working with primitives in stead of objects) get you to provide the information in such a way that it is how the client can work with it?

    If you require further assistance you'll have to minimally give an example of the specifications - how does the client expect the bytes. Perhaps then someone can provide you with an example of how to produce something like that using Java code.

    See how that works? You do something, someone else can do something back for you. That's the only way it is going to work.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points