Page 1 of 1

wxSocketServer how to get the type of received data?

Posted: Thu Dec 30, 2010 11:17 am
by hats
a simple wxSocketClient and wxSocketServer program.
now they can send message and file to each other as well.
it is the code that send text message:

Code: Select all

const wxChar* buf; 
wxString text = TextCtrl1->GetValue();//text that to send
buf = text.c_str(); 
wxUint32 len = (wxStrlen(buf)+1)*sizeof(wxChar);//size of data
clientSocket->Write(buf,len); 
and it is the code that send file:

Code: Select all

wxFile file; 
file.Open(wxT("test.txt"),wxFile::read);//read mode
size_t fileSize = file.Length();//file size
wxUint8* buffer = new wxUint8[fileSize]; 
size_t len = file.Read(buffer,fileSize);//read data in buffer 
clientSocket->Write(buffer,len);//send
delete []buffer; 
file.Close();
wxSocketServer use wxSocketServer::Read()
could read wxChar* and wxUint8* success and work well.

but our server how to get the type that received data?
if data is wxChar* ,I will show it on gui,and if data id wxUint8*,
I will save it to a file. Could any body help me?

Posted: Thu Dec 30, 2010 11:24 am
by briceandre
A socket is not aware of the data type it receives. If you want to send different data types to a socket, you should add your own contextual info to know exactly what is transmitted.

For example, in your case, you could start sending a byte with data type (0 for messages, 1 for files), then four bytes for the data length, and after your data.

The client will thus have to read the five first bytes to know what it is currently receiving and the size of the data.

Posted: Fri Dec 31, 2010 12:38 am
by hats
Thank you!I know how to do now.

Posted: Fri Dec 31, 2010 12:39 am
by hats
Thank you!I know how to do now.

Posted: Fri Dec 31, 2010 12:39 am
by hats
Thank you!I know how to do now.

Posted: Fri Dec 31, 2010 12:41 am
by hats
Thank you!I know how to do now.

Posted: Fri Dec 31, 2010 12:41 am
by hats
Thank you!I know how to do now.

Posted: Fri Dec 31, 2010 12:43 am
by hats
Thank you!I know how to do now.