Page 1 of 1

Converting void* GetClientData to integer

Posted: Tue Jan 16, 2007 7:34 pm
by xskater11x
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.

Posted: Tue Jan 16, 2007 9:36 pm
by phlox81
First, you should know what hides behind your void*.
If its a real pointer then:
int* ip = (int*)vp;
or
int* ip = static_cast<int*>(vp);

If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;

Posted: Tue Jan 16, 2007 9:44 pm
by xskater11x
It's still not returning the correct id from the socket.

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();
			}
RemoveSocket:

Code: Select all

bool Network::removeSocket(wxSocketBase *remsocket)
{

	int id = (int)*remsocket->GetClientData();

	clientSockets[id]->Destroy;
	clientSockets[id] = NULL;

	return true;

}
Output:

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

Posted: Wed Jan 17, 2007 7:38 am
by tan
Hi,
try to change

Code: Select all

  int id= (int)*sock->GetClientData();
to

Code: Select all

  int id= *((int *)sock->GetClientData());

Posted: Wed Jan 17, 2007 3:00 pm
by Belgabor
phlox81 wrote:If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
Shouldn't that be
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 =)

Posted: Wed Jan 17, 2007 4:29 pm
by phlox81
Belgabor wrote:
phlox81 wrote:If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
Shouldn't that be
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 =)
yeah, was a little late yesterday :)

Posted: Wed Jan 17, 2007 7:30 pm
by xskater11x

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
Shouldn't that be
int i = (int)vp;
?
that just returns cannot convert 'void*' to 'int'

Posted: Thu Jan 18, 2007 6:26 am
by tan
xskater11x wrote:

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
Shouldn't that be
int i = (int)vp;
?
that just returns cannot convert 'void*' to 'int'
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;

Posted: Thu Jan 18, 2007 6:34 am
by phlox81
tan wrote:
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
don't use delete for pointers that aren't created with new!

Posted: Thu Jan 18, 2007 7:00 am
by tan
phlox81 wrote:
tan wrote:
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
don't use delete for pointers that aren't created with new!
But it is:

Code: Select all

int* clientData = new int(openSlot);
sock->SetClientData(clientData);

Posted: Thu Jan 18, 2007 7:26 pm
by xskater11x

Code: Select all

First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
is what i now get using tan's suggestion

Code: Select all

clientSockets[id]->Destory();
is what brings up the error. Which makes me believe the number its converting to is wrong, because the sockets function perfectly.

Posted: Fri Jan 19, 2007 7:06 am
by tan
xskater11x wrote:

Code: Select all

First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
is what i now get using tan's suggestion

Code: Select all

clientSockets[id]->Destory();
is what brings up the error. Which makes me believe the number its converting to is wrong, because the sockets function perfectly.
Post more relevant code, i belive the problem is anywhere else, then converting to int.

Posted: Sat Jan 20, 2007 1:23 am
by Belgabor
xskater11x wrote:

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
The correct statement then is (I think):

Code: Select all

int i = ((int*) vp)*;
If that doesn't work, indeed the problem has to be somewhere else.