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

Author Topic: Receive loop  (Read 17781 times)

0 Members and 1 Guest are viewing this topic.

Dummie

  • Newbie
  • *
  • Posts: 25
    • View Profile
Receive loop
« on: July 01, 2008, 07:27:07 pm »
How will be a receive loop done in SFML?

This seems to be the wrong way:
Code: [Select]

char recvBuf[1024];
size_t received;
for (;;)
{
   sf::Socket::Status status = client.Receive(recvBuf, sizeof(recvBuf), received);
   if (status != sf::Socket::Done)
   {
      std::cerr << "Error: receive" << std::endl;
      return 1;
   }

   if (received == 0)
      break;

   recvBuf[received] = '\0';
   std::cout << "Server: " << recvBuf << std::endl;
}


How will it be done right in SFML?

Thanks,
Dummie

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Receive loop
« Reply #1 on: July 02, 2008, 02:47:46 am »
Looks good to me, why are you saying it's wrong ?
Laurent Gomila - SFML developer

Kernelpanic

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
    • http://eisenholz.bplaced.net
Receive loop
« Reply #2 on: July 02, 2008, 12:56:35 pm »
For such a loop you should not use "for".
It is a semantic absurdity, you should use "while(true)".

Dummie

  • Newbie
  • *
  • Posts: 25
    • View Profile
Receive loop
« Reply #3 on: July 02, 2008, 02:47:06 pm »
Quote from: "Kernelpanic"
For such a loop you should not use "for".
It is a semantic absurdity, you should use "while(true)".


Yes, you are right. Thank you!

The loop is working fine now. I dont know what the problem was.

Thank you :) :) :)

T.T.H.

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Receive loop
« Reply #4 on: July 03, 2008, 12:21:08 am »
...and while we're about semantic absurdities here I just want to mention that the static code analysis tool PC-lint actually suggest to use "for (;;)" instead of "while true". I have no clue why they do so but, hey, they seem to know their job because they develop a static code analysis tool for C and C++ since about 23 years.

Anyway, personally I'd use "while (true)", too :p

eleinvisible

  • Newbie
  • *
  • Posts: 47
    • View Profile
Receive loop
« Reply #5 on: July 03, 2008, 12:52:03 am »
I use while(1)...Shortest way is the best way.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Receive loop
« Reply #6 on: July 03, 2008, 01:46:52 am »
Quote from: "eleinvisible"
I use while(1)...Shortest way is the best way.


That's how I feel, but I use:

Code: [Select]
for (;;)


I do not care about semantics.

Kernelpanic

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
    • http://eisenholz.bplaced.net
Receive loop
« Reply #7 on: July 03, 2008, 01:47:10 pm »
Quote from: "Wizzard"
I do not care about semantics.


For should always mean "walk thorugh" or something like that.

I think
Code: [Select]

for(int unsigned i = 4711; i > 1; i  /= 2)
or
for(std::deque<int>::iterator it = my_deq.begin(), i = 0815; it != my_deq.end() && i != 0; it++, i--)

are unusual but acceptable, but for(;;) has nothing to do with the idea of a for-loop.

 

anything