Converting void* GetClientData to integer Topic is solved
-
- Earned a small fee
- Posts: 22
- Joined: Sat May 20, 2006 2:45 am
Converting void* GetClientData to integer
I was wondering if there was any simple way to convert from void* to int. I have a map of sockets to players and the only way to find out what player is to find the set socket number. So far i have only been able to get an int* to convert successfully, but the map needs a regular int*. If anyone has any ideas, please share, or i can provide code if you need to see what I am currently doing.
-
- Earned a small fee
- Posts: 22
- Joined: Sat May 20, 2006 2:45 am
It's still not returning the correct id from the socket.
Code for display:
RemoveSocket:
Output:
Code for display:
Code: Select all
bool isRemoved = removeSocket(sock);
if (isRemoved)
{
int id= (int)*sock->GetClientData();
wxString sRemove = "Client succesfuly removed, clientID= ";
sRemove += wxString::Format("%d", id);
sRemove+="\n";
((ServerFrame*)sFrame)->textChat->AppendText(sRemove);
}
else
{
((ServerFrame*)sFrame)->textChat->AppendText(_("Socket was not removed from system.\n"));
sock->Destroy();
}
Code: Select all
bool Network::removeSocket(wxSocketBase *remsocket)
{
int id = (int)*remsocket->GetClientData();
clientSockets[id]->Destroy;
clientSockets[id] = NULL;
return true;
}
Code: Select all
OnSocketEvent: wxSOCKET_CONNECTION
New client connection accepted
Client was added, clientID= 0
OnSocketEvent: wxSOCKET_INPUT
Recieving incoming data
Client Identify Recieved.
grim
OnSocketEvent: wxSOCKET_LOST
Deleting socket.
Client succesfuly removed, clientID= -17891602
-
- wxWorld Domination!
- Posts: 1471
- Joined: Tue Nov 14, 2006 7:58 am
- Location: Saint-Petersburg, Russia
Hi,
try to change
to
try to change
Code: Select all
int id= (int)*sock->GetClientData();
Code: Select all
int id= *((int *)sock->GetClientData());
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
yeah, was a little late yesterdayBelgabor wrote:Shouldn't that bephlox81 wrote:If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
int i = (int)vp;
?
The other would probably convert a pointer to vp to int...
To make sure we should see where and how the client data is assigned =)
-
- Earned a small fee
- Posts: 22
- Joined: Sat May 20, 2006 2:45 am
Code: Select all
int openSlot = findOpenPlayerSlot();
int* clientData = new int(openSlot);
sock->SetClientData(clientData);
that just returns cannot convert 'void*' to 'int'Shouldn't that be
int i = (int)vp;
?
-
- wxWorld Domination!
- Posts: 1471
- Joined: Tue Nov 14, 2006 7:58 am
- Location: Saint-Petersburg, Russia
In ths casexskater11x wrote:thats how its set, openslot is returned from a functionCode: Select all
int openSlot = findOpenPlayerSlot(); int* clientData = new int(openSlot); sock->SetClientData(clientData);
that just returns cannot convert 'void*' to 'int'Shouldn't that be
int i = (int)vp;
?
Code: Select all
int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
don't use delete for pointers that aren't created with new!tan wrote:
In ths case
Code: Select all
int *ptmp = (int *)sock->GetClientData(); int res = *ptmp; delete ptmp;
-
- wxWorld Domination!
- Posts: 1471
- Joined: Tue Nov 14, 2006 7:58 am
- Location: Saint-Petersburg, Russia
But it is:phlox81 wrote:don't use delete for pointers that aren't created with new!tan wrote:
In ths case
Code: Select all
int *ptmp = (int *)sock->GetClientData(); int res = *ptmp; delete ptmp;
Code: Select all
int* clientData = new int(openSlot);
sock->SetClientData(clientData);
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
-
- Earned a small fee
- Posts: 22
- Joined: Sat May 20, 2006 2:45 am
Code: Select all
First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
Code: Select all
clientSockets[id]->Destory();
-
- wxWorld Domination!
- Posts: 1471
- Joined: Tue Nov 14, 2006 7:58 am
- Location: Saint-Petersburg, Russia
Post more relevant code, i belive the problem is anywhere else, then converting to int.xskater11x wrote:is what i now get using tan's suggestionCode: Select all
First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
is what brings up the error. Which makes me believe the number its converting to is wrong, because the sockets function perfectly.Code: Select all
clientSockets[id]->Destory();
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
The correct statement then is (I think):xskater11x wrote:thats how its set, openslot is returned from a functionCode: Select all
int openSlot = findOpenPlayerSlot(); int* clientData = new int(openSlot); sock->SetClientData(clientData);
Code: Select all
int i = ((int*) vp)*;