wxSocketServer how to get the type of received data? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

wxSocketServer how to get the type of received data?

Post by hats » Thu Dec 30, 2010 11:17 am

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?
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre » Thu Dec 30, 2010 11:24 am

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.

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:38 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:39 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:39 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:41 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:41 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Fri Dec 31, 2010 12:43 am

Thank you!I know how to do now.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

Post Reply