Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Packet reception - interoperability with Java (?)  (Read 4160 times)

0 Members and 1 Guest are viewing this topic.

radario

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Packet reception - interoperability with Java (?)
« on: May 09, 2012, 11:47:48 pm »
Hi, I'm new here, hello to everybody:)

I'm trying to send strings from Java to an SFML application, via TCP, and I want to use the packet abstraction, which I found nice.

Java-wise, the code is this:

String message = "something";
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
int length = message.getBytes().length + 4;
dos.writeInt(length);
dos.writeBytes(message);            
dos.flush();

In SFML, the following code doesn't work:

sf::Packet packet;
socket.receive(packet);
std::cout << "Packet size: " << packet.getDataSize() << std::endl;
std::string stringa;
packet >> stringa;        
std::cout << stringa << std::endl;

The problem is that packet.getDataSize() returns 0, and subsequently nothing is received.
In order to debug this, I tried to call directly socket.receive, and I discovered that it receives 1 byte at a time, even if I specify 4 of them (to receive the first int with the size of the packet);

So, to properly receive the packet I wrote this:

char *dataSize = new char[4];

std::size_t received = 0;
std::size_t receivedSoFar = 0;

do {
   socket.receive((void *) (dataSize + receivedSoFar), 1, received);
   receivedSoFar += received;            
} while (receivedSoFar < 4);

then I convert the 4 char to a number, I allocate a proper buffer, and then I happily receive, with the same mechanism, the rest of the string.
I also noticed that socket.receive, when called on a connection coming from a C++ SFML application, does indeed receives 4 bytes at a time. Maybe it's related to the fact that when receiving a packet sent from Java, only the first byte is read, then it's treated as a whole int and as such its value is zero.

I'm running SFML RC1 as packaged in ArchLinux.

Bye:)
« Last Edit: May 10, 2012, 01:39:12 pm by radario »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Packet reception - interoperability with Java (?)
« Reply #1 on: May 10, 2012, 08:17:01 am »
First, you can't mix packets and something else. They have their own internal format (the Uint32 size is encoded at the beginning of the packet), and you shouldn't even try to rely on this because it might change in the future -- it's an implementation detail.

Second, if you receive data bytes one by one when you send with Java and not SFML, then it's probably caused by Java.
Laurent Gomila - SFML developer

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Packet reception - interoperability with Java (?)
« Reply #2 on: May 10, 2012, 12:08:57 pm »
You shouldn't rely on SFML's packet abstraction for this. Both Java and SFML use the same low level stream based socket communication, so it is entirely possible to build your own Packet protocol on top of that (but you have to code both the Java and C++ side).

radario

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Packet reception - interoperability with Java (?)
« Reply #3 on: May 10, 2012, 01:36:02 pm »
Hi

thanks for the answers:)

First, you can't mix packets and something else.

Actually I'm not trying to mix packets and something else. What I did was trying to reverse-engineer the packet format in order to being able to use the "packet >> string" form, that I find convenient. I'm using sf::Packet to send data to Java (and it works) and I just wanted to do the opposite (and it doesn't work).

They have their own internal format (the Uint32 size is encoded at the beginning of the packet), and you shouldn't even try to rely on this because it might change in the future -- it's an implementation detail.

Ok. So I can assume that sf::Packet are meant only for SFML-SFML communication, right?

Second, if you receive data bytes one by one when you send with Java and not SFML, then it's probably caused by Java.

Sure, Java seems to send one byte at a time. But the bytes are indeed there. I'm wondering if there's an assumption about the count of received bytes somewhere in SFML code.

thanks for answering:)

bye!
« Last Edit: May 10, 2012, 01:37:58 pm by radario »

radario

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Packet reception - interoperability with Java (?)
« Reply #4 on: May 10, 2012, 01:37:31 pm »
Hi!

it is entirely possible to build your own Packet protocol on top of that (but you have to code both the Java and C++ side).

Yes, actually I'm trying to avoid writing the C++ side since it's already there:)

bye!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Packet reception - interoperability with Java (?)
« Reply #5 on: May 10, 2012, 01:45:50 pm »
Quote
So I can assume that sf::Packet are meant only for SFML-SFML communication, right?
Yes.

In the future I might change this: use a more generic data stream class and add the specific network format only at the socket level.
Laurent Gomila - SFML developer

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Packet reception - interoperability with Java (?)
« Reply #6 on: May 11, 2012, 12:12:02 pm »
Another problem with java is that it only supports Signed data types. Since SFML and C++ in general uses a lot of unsigned data types (like the header in a sf::Packet) this can become a problem. You would need to write your own number converters and store Uint32 in the java Long type. UInt64 cannot be represented in Java at all (unless you use some special BigNumber class wrapper).

radario

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Packet reception - interoperability with Java (?)
« Reply #7 on: May 11, 2012, 09:17:55 pm »
Ok don't worry I'll take care of this:)

bye:)