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

Author Topic: Selector.Add(socketTCP) error  (Read 2177 times)

0 Members and 1 Guest are viewing this topic.

CxyRobin

  • Newbie
  • *
  • Posts: 3
    • AOL Instant Messenger - J+M+Uglands+Vei+
    • View Profile
Selector.Add(socketTCP) error
« on: December 01, 2011, 02:57:02 pm »
Hi!

I've been trying networking in SFML for my latest game, and there is a memory corruption error that I just can't seem to fix. I thought I'd found it earlier, when I posted on this forum like 2 weeks ago, but then I moved around some stuff in my header file and it's all still screwed.

I've created a stripped down instance of the error, and I'm wondering if anyone can take a look and tell me what I'm doing wrong?

I'm hoping that I'm just retarded at including the SFML files or something, because I just can't figure this out.

The error is in the third file. Is it something very basic that I've forgotten? I've looked through the tutorials, and I just can't see why this doesn't work.

Main:
Code: [Select]

#include "CClient.h"

int main()
{
while(true)
{
CClient::getInstance().RunClient();
}
return 0;
}


CClient.h:
Code: [Select]

//=============================================================================
#ifndef _CCLIENT_H_
    #define _CCLIENT_H_

#include <SFML/Network.hpp>

//=============================================================================
class CClient{
public:
CClient();
void RunClient();
void SendPacket(int packetID, int entityID, int playerID, int command);

sf::SelectorTCP Selector;
sf::SocketTCP Listener;
static CClient& getInstance();
   
protected:
static CClient* client;
};

//=============================================================================

#endif


CClient.cpp:
Code: [Select]

#include "CClient.h"
#include <iostream>

const int Port = 4567;

CClient* CClient::client = 0;

CClient::CClient()
{
if (!Listener.Listen(Port))
        return;
Selector.Add(Listener);
// ERROR occurs here, when running the Selector.Add(Listener).
}

CClient& CClient::getInstance()
{
if(!client)
client = new CClient();

return *client;
}

void CClient::RunClient()
{

}

void CClient::SendPacket(int packetID, int entityID, int playerID, int command)
{

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Selector.Add(socketTCP) error
« Reply #1 on: December 01, 2011, 03:01:14 pm »
Does the selector tutorial's code work?

By the way, although your code is ok (compared to other users' "minimal" code snippets), you could have made this minimal code much simpler ;)
Code: [Select]
#include <SFML/Network.hpp>

int main()
{
   sf::SelectorTCP Selector;
   sf::SocketTCP Listener;

   if (!Listener.Listen(4567))
        return 0;
   Selector.Add(Listener);

   return 0;
}
Laurent Gomila - SFML developer

CxyRobin

  • Newbie
  • *
  • Posts: 3
    • AOL Instant Messenger - J+M+Uglands+Vei+
    • View Profile
Selector.Add(socketTCP) error
« Reply #2 on: December 01, 2011, 03:10:53 pm »
I suppose I could've made it shorter, I just removed the parts from my game code that were not needed to reach the error point. Thanks for the reply.

The downloadable code runs fine.

It seems I forgot to write the error I get upon running my project, it is:

Quote
Run-Time Check Failure #2 - Stack around the variable 'Listener' was corrupted.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Selector.Add(socketTCP) error
« Reply #3 on: December 01, 2011, 03:45:01 pm »
Does it fail the same way with the version of your code that I posted?
Which compiler do you use? If VC++ 2010, have you recompiled SFML?

Quote
I suppose I could've made it shorter, I just removed the parts from my game code that were not needed to reach the error point.

Think about the forum users that will read your code -- 10, maybe 100. It's a waste of time for everyone to mentally ignore the 90% of what you posted that are only comments or empty functions, and it's harder to focus on the actual code. If you do it before posting, it's just 30 extra seconds spent.

But anyway, let's focus on your problem :)
Laurent Gomila - SFML developer

CxyRobin

  • Newbie
  • *
  • Posts: 3
    • AOL Instant Messenger - J+M+Uglands+Vei+
    • View Profile
Selector.Add(socketTCP) error
« Reply #4 on: December 01, 2011, 04:45:41 pm »
Duh, it would seem that recompiling fixed it. I spent a whole lot of hours trying to solve what seemed like memory corruption until this error suddenty started popping up. :roll:

Thanks a whole lot for solving it! Sorry for wasting your time.  :wink: