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

Author Topic: Is there a way of knowing the number of bytes pending in a socket?  (Read 2635 times)

0 Members and 1 Guest are viewing this topic.

paoc

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi everybody,

I'm new with TCP communication... Maybe it is a stupid question but I was wondering if there is a way of knowing the number of bytes in a socket without having to read it?
(I trying to understand how to do if I receive my message in not one but few packets...)

Hope my question is clear enough,
Thanks for you help! :)

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Is there a way of knowing the number of bytes pending in a socket?
« Reply #1 on: April 22, 2016, 03:52:06 pm »
Your question is clear, but I don't understand why you want to do this. Can't you simply read the data in a buffer to get the number ?

Looking at the doc, it doesn't seem like you can do what you exactly asked without reading it previously.
« Last Edit: April 22, 2016, 03:56:41 pm by nicox11 »

paoc

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is there a way of knowing the number of bytes pending in a socket?
« Reply #2 on: April 22, 2016, 04:19:35 pm »
Because I will receive commands that all start with the same header and the header start with a magic number. In the header I will have access to the size of the whole message I need.

If I can wait in order to take the whole message, I'll be sure that the new one will start with the magic number. It would have make it easier instead of having to copy everything and always look for the begining of the new message...

But thank you for your reply nicox11 :)

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Is there a way of knowing the number of bytes pending in a socket?
« Reply #3 on: April 22, 2016, 04:28:46 pm »
SFML provides a high level class to send data : sf::packet, you should check the official tutorial (and the others tutorials as well, it'll help you avoiding low level problematics) :

http://www.sfml-dev.org/tutorials/2.3/network-packet.php

It's easy to use, and you are sure to receive the whole packet at each time (ie: not a part of the packet) :

// on sending side
std::string header = "MyCommandHeaderCode";
double objectToSend = 0.6;

sf::Packet packet;
packet << header << objectToSend;

tcpSocket.send(packet);
 

//receiving sides

//You are sure to receive the whole packet : not a part, not more
tcpSocket.receive(packet);

std::string headerCode;
double data;

packet >> headerCode >>data;
 
« Last Edit: April 22, 2016, 04:32:27 pm by nicox11 »

paoc

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is there a way of knowing the number of bytes pending in a socket?
« Reply #4 on: April 22, 2016, 05:36:00 pm »
Great,

I will have a deeper look into that. But I'm afraid that it won't fit for me cause the client side is not written with the SFML lib...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is there a way of knowing the number of bytes pending in a socket?
« Reply #5 on: April 24, 2016, 12:34:33 pm »
You should be aware that unlike UDP, TCP is a stream-based protocol, packets are a higher-level abstraction of the application layer.

The library you're using for the TCP client (which one?) may provide stream functionality that allows to read one or multiple bytes in a blocking way. That is, the read function only returns when data is available. With that, you can then read the header (known number of bytes), and after that make another read call with the number of bytes specified by the header. For example, with Java Sockets this is quite simple to achieve.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: