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

Author Topic: SFML2 Split Packets and Cut Strings  (Read 4208 times)

0 Members and 1 Guest are viewing this topic.

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML2 Split Packets and Cut Strings
« on: October 18, 2010, 08:54:02 am »
I have my Server and Client. My issue is that when I send a message through my client to my server the server receives it as chunks and if the word was longer than 4 characters it expunges any characters past the fourth and if it's 4 or greater it appends a set of funky symbols to the end of each packet.

Screens:

Client


Server

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML2 Split Packets and Cut Strings
« Reply #1 on: October 18, 2010, 09:38:11 am »
Just a guess, which is the best I can do without seeing your code: maybe you send sizeof(some_ptr), which is always 4, instead of the actual number of bytes pointed by your pointer?
Laurent Gomila - SFML developer

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML2 Split Packets and Cut Strings
« Reply #2 on: October 18, 2010, 10:13:37 am »
That might be it, this is the function I'm using for my Send function.

Code: [Select]
void Client::Send( const char message[] )
{
if( Socket.Send( message, sizeof( message ), ConnectedTo.Address, ConnectedTo.Port ) != sf::Socket::Done )
Debug( "Network", "Error sending Packet" );
else Debug( "Network", "Message Sent" );
}


Here's my Servers Receive Function

Code: [Select]
void Server::Listen( void )
{
if( Socket.Receive( Data, sizeof( Data ), Received, Sender, Port ) != sf::Socket::Done )
Debug( "Network", "Error receiving Packet" );
else printf( "%s \"%s\"\n", Sender.ToString().c_str(), Data );
}


But if I'm not mistaken that code is near identical to what you used in the SDK Sample, including the sizeof parameter.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML2 Split Packets and Cut Strings
« Reply #3 on: October 18, 2010, 11:06:24 am »
Nop, it's not identical. It's tricky because the type of the data looks like the same (const char[]), but in fact it is not: when declaring an argument of type T[], in fact it is a T*, the compiler looses the size information. So sizeof(message) is always 4 in your function, you have to manage the size explicitely.
Laurent Gomila - SFML developer

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML2 Split Packets and Cut Strings
« Reply #4 on: October 18, 2010, 12:33:18 pm »
Thanks, I managed to get the proper length of the array. But now I'm coming up with a separate problem, in my classes I have a 'char Data[]' variable. And after I send my first message or define a default value for Data I can't send any data larger than the first string I sent, or at least the server refuses to take it. I assume this is because char Data[] defines it's max length based on it's first value ( I jumped right into std::string when I learned C++ so I'm a bit ignorant on this sort of thing. ) So I made the char *Data instead, but this causes a crash during run-time, and my debugger ( as unreliable as it may be ) tells me it occurs on the line the Client's Send Function is called. This may be a bit off the side from any actual SFML business, but I was wondering what sort of string I should be passing to sf::UdpSocket::Send, the prototype says const char *, but as seen before that doesn't make my Server too happy.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML2 Split Packets and Cut Strings
« Reply #5 on: October 18, 2010, 02:05:07 pm »
Quote
But now I'm coming up with a separate problem, in my classes I have a 'char Data[]' variable.

Same thing as before: it's in fact a char*, and you need to allocate memory for it. You should avoir using this array notation (with no explicit size), it's confusing because most of the time it's just a pointer, and it doesn't behave like an array with explicit size. Use std::vector if you want a dynamic array.

Quote
I was wondering what sort of string I should be passing to sf::UdpSocket::Send, the prototype says const char *

You can pass anything, here char* doesn't mean "string", it means "a sequence of bytes". So the input type doesn't really matter, you can simply reinterpret_cast it to const char*.
Laurent Gomila - SFML developer

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML2 Split Packets and Cut Strings
« Reply #6 on: October 18, 2010, 05:26:19 pm »
Thanks, I think I'm going to stick to std::string from now on. Character arrays are really not my thing apparently. I haven't gotten the chance to cast yet, but I'm sure it'll work, thanks for your time.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
SFML2 Split Packets and Cut Strings
« Reply #7 on: October 19, 2010, 08:38:55 am »
And please, as a sidenote, check your "debugging words" before posting such screenshots to a public forum. There might be groups of people who won't be very happy to read something that begins with "Nigg...".