Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WizHarDx

How do I transfer data other than char*

This topic is 5953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Like when I tried to convert a long, by doing: long varible=5; char* Buffer=reinterpret_cast(varible); send(...,Buffer); But on the server side I got a completly diffrent number , why is this ? thanx in advance WizHarD Also I am trying to send a bitmap accross the network by using fopen & getting all the data in the bitmap then transfering but I don''t quite know how to recv it on the server end. How do I use fwrite & know when I have recived the whole bitmap ? thanx in advance WizHarD

Share this post


Link to post
Share on other sites
Advertisement
hi,

Check this thread out, i think that would be help :D

http://www.gamedev.net/community/forums/topic.asp?topic_id=64149

Mfg Impz0r

Share this post


Link to post
Share on other sites
Your code is incorrect for what you are trying to accomplish. If I understand you correctly, you want to send a long value to the server.

What you need is a memory copy:



long variable = 5;
char *buffer = new char[sizeof(variable)];

memcpy(buffer, &variable, sizeof(variable));

send(socket, buffer, sizeof(variable), 0);



The above code is the long way (no pun intended hehe). An easier, more straight forward way would be:



long variable = 5;
send(socket, (char*)&variable, sizeof(variable), 0);



That should do what you want.

EDIT: Becareful when sending binary data between heterogenous operation systems/platforms. Some store memory in different formats (big endian, little endian etc) and there you need to translate the memory representation into a common format. This is very similiar to the htons(), ntohs() line of calls.

Hope that helps,



Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on February 1, 2002 1:31:11 PM

Share this post


Link to post
Share on other sites
WizHarDx, data must be serialized to a byte buffer in order to be sent across a socket, just like writing to a file.

Like Dire.Wolf pointed out, your cast is incorrect. You can easily convert a long value to a string like so:

sprintf(buffer, "%ld", vible);

However, you would then have to re-interpret this string on the other end and convert it back to a long. Dire.Wolf''s solution is better because it writes data from the address of the long variable directly onto the byte stream. Conceptually, it may be easier for you to think of the socket buffer like this:

BYTE sockbuff[MAX_BUFF];

Instead of char*... then you don''t get confused about type conversion.

To anwer your other question... a receiving socket knows it has all the data available when it makes a call to recv() and succeeds, but reads 0 bytes.

Share this post


Link to post
Share on other sites
quote:

To anwer your other question... a receiving socket knows it has all the data available when it makes a call to recv() and succeeds, but reads 0 bytes.



Careful, that is misleading recv() doesn''t return 0 bytes when all the data has been received. For a connection oriented socket, recv() returns 0 if the connection has been disconnected gracefully and all data has been received from the remote connection. For a connection-less socket (UDP for example) 0 bytes from a call to recv() is valid. This makes sense when you think about it.



Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites
Hmmmm... perhaps I am not remembering my source code very well. It''s been a while. But the last time I made a socket app it was an async mode app... and I looped through a call to recv() until I read 0 bytes. Of course, I don''t have my source code right here, so like I said maybe I am not remembering correctly.

For a blocking socket of course the call to recv() would just sit until data was available. For a non-blocking socket that doesn''t use WSAAsync-style notification, you would look for a return value of WSAEWOULDBLOCK, indicating that there is nothing to be read.

Share this post


Link to post
Share on other sites
quote:

For a blocking socket of course the call to recv() would just sit until data was available. For a non-blocking socket that doesn''t use WSAAsync-style notification, you would look for a return value of WSAEWOULDBLOCK, indicating that there is nothing to be read.



recv() should still return 0, even if the socket is non-blocking, if the other end disconnected gracefully. If you call one of the WSAxxx functions on an asynchronous TCP socket, the overlapped I/O shouldn''t complete until it actually reads something or fails, or the client disconnects (and therefore bytes transferred == 0). Very strange, I''ll have to look into that

Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites
My bad... you are right, a 0 is only returned when the socket has closed gracefully. I thought I was reading 0 to signal the end of the data because my code looked like this:


while ( (rResult = recv((SOCKET) wParam, msg, 80, 0)) != 0)
{
if (rResult == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
MessageBox("Some error");
}
break;
}
...


So, obviously my code is exiting the while look because it gets a WSAEWOULDBLOCK, not because it's getting back a 0 from recv(). Duh! That's what I get for not doing any socket programming for so long...



Edited by - Pyabo on February 8, 2002 7:40:12 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!