Click here to Skip to main content
16,004,974 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How can I send/receive a struct via winsock? Pin
Mark Salsbery30-Apr-07 7:50
Mark Salsbery30-Apr-07 7:50 
QuestionRe: How can I send/receive a struct via winsock? Pin
deerhunter891-May-07 3:33
deerhunter891-May-07 3:33 
AnswerRe: How can I send/receive a struct via winsock? Pin
Moak1-May-07 3:59
Moak1-May-07 3:59 
QuestionRe: How can I send/receive a struct via winsock? Pin
deerhunter891-May-07 7:10
deerhunter891-May-07 7:10 
AnswerRe: How can I send/receive a struct via winsock? Pin
Moak1-May-07 7:33
Moak1-May-07 7:33 
GeneralRe: How can I send/receive a struct via winsock? Pin
deerhunter892-May-07 11:15
deerhunter892-May-07 11:15 
AnswerRe: How can I send/receive a struct via winsock? Pin
Mark Salsbery1-May-07 4:32
Mark Salsbery1-May-07 4:32 
AnswerRe: How can I send/receive a struct via winsock? Pin
Moak1-May-07 8:11
Moak1-May-07 8:11 
deerhunter89 wrote:
char* inBuffer = (char*)malloc(2048);
int nRecv = recv(sckClient, inBuffer, 2048, 0);

if ( nRecv == sizeof(DATASTRUCT*) )
{
DATASTRUCT IncomingData;
IncomingData = (DATASTRUCT*)inBuffer;
MessageBox(hWnd, IncomingData->sParam1, "incoming data", MB_OK);
}


Unfortunately this code has some problems (and a memory leak):
a) TCP is a stream oriented protocol and you may not receive data chunks in exact the same size as they have been sent out, even if your code above works in the LAN it will fail with peers on the Internet. What you need to do in your client: collect all incoming data in a buffer, when you have enough data collected process the buffer.
b) The above code tests if you have 4 bytes of the data struct (a pointer has the size of 4 bytes on 32bit architectures). I suggest to collect incoming data until enough data was received to get a full 'DATASTRUCT' (move any possible "left overs" to the beginning of the buffer)... e.g. if(nBytesCollected >= sizeof(DATASTRUCT)) { ProcessData(m_buffer); UpdateReceiveBuffer(); }
AnswerRe: How can I send/receive a struct via winsock? Pin
hmaturana30-Apr-07 13:36
hmaturana30-Apr-07 13:36 
Questionlist control and selected records Pin
hero199529-Apr-07 23:24
hero199529-Apr-07 23:24 
AnswerRe: list control and selected records Pin
Venkata Rama Subbarao30-Apr-07 1:45
Venkata Rama Subbarao30-Apr-07 1:45 
QuestionRe: list control and selected records Pin
David Crow30-Apr-07 3:17
David Crow30-Apr-07 3:17 
Questionthe equivalent of instructions in assembly Pin
zon_cpp29-Apr-07 22:51
zon_cpp29-Apr-07 22:51 
AnswerRe: the equivalent of instructions in assembly Pin
Cmania29-Apr-07 23:00
Cmania29-Apr-07 23:00 
AnswerRe: the equivalent of instructions in assembly Pin
Cmania29-Apr-07 23:06
Cmania29-Apr-07 23:06 
GeneralRe: the equivalent of instructions in assembly Pin
zon_cpp30-Apr-07 0:17
zon_cpp30-Apr-07 0:17 
GeneralRe: the equivalent of instructions in assembly Pin
markkuk30-Apr-07 0:29
markkuk30-Apr-07 0:29 
GeneralRe: the equivalent of instructions in assembly Pin
zon_cpp30-Apr-07 1:18
zon_cpp30-Apr-07 1:18 
Questiondoes thread returns a value.... Pin
neha.agarwal2729-Apr-07 21:16
neha.agarwal2729-Apr-07 21:16 
AnswerRe: does thread returns a value.... Pin
Stephen Hewitt29-Apr-07 21:21
Stephen Hewitt29-Apr-07 21:21 
Questionregarding property pages Pin
Sunshine Always29-Apr-07 21:08
Sunshine Always29-Apr-07 21:08 
AnswerRe: regarding property pages Pin
_AnsHUMAN_ 29-Apr-07 21:37
_AnsHUMAN_ 29-Apr-07 21:37 
Questioncompare SW version Pin
SWDevil29-Apr-07 20:49
SWDevil29-Apr-07 20:49 
AnswerRe: compare SW version Pin
GuyM29-Apr-07 20:57
GuyM29-Apr-07 20:57 
AnswerRe: compare SW version Pin
beko29-Apr-07 22:41
beko29-Apr-07 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.