Click here to Skip to main content
15,897,291 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++ Components Question Pin
Nick Parker16-May-02 8:28
protectorNick Parker16-May-02 8:28 
QuestionThe clause of IDispatch #3105 error? Pin
sages15-May-02 17:32
sages15-May-02 17:32 
AnswerRe: The clause of IDispatch #3105 error? Pin
Roger Wright15-May-02 21:23
professionalRoger Wright15-May-02 21:23 
GeneralLetters Instead of Numbers Pin
nywebhead15-May-02 17:25
nywebhead15-May-02 17:25 
GeneralRe: Letters Instead of Numbers Pin
Christian Graus15-May-02 17:35
protectorChristian Graus15-May-02 17:35 
GeneralRe: Letters Instead of Numbers Pin
nywebhead15-May-02 17:40
nywebhead15-May-02 17:40 
GeneralRe: Letters Instead of Numbers Pin
Christian Graus15-May-02 18:16
protectorChristian Graus15-May-02 18:16 
GeneralWSASend & WSARecv :: Winsock Pin
valikac15-May-02 15:50
valikac15-May-02 15:50 
Hi.

I am implementing a send and receive part of a simple message program. Everything seems to work okay, except the send and receive are not in sync.

For send, I use WSASend. I first send a "header" buffer with the same of the real data buffer. Next, I send the real data buffer.

For receive, I use WSARecv. I first receive the "header" buffer and determine its DWORD size. I then allocate a character array of that size. Next, I receive the real data buffer based on the size.

The problem is the "header" buffer I receive does not hold the correct data size. For example, let say the data size is 10 byte (10 characters). Instead of 10, I will get something at least in 90+ when I cast the "header" to a DWORD.

Here is the sample code for both send and receive. Please keep me updated if you find any known problem.

-----
char *data = new char[4];
data = "test";
DWORD bufferSize = static_cast<dword>(strlen(data)), sentSize = 0;
WSABUF bufferHDR,
bufferDATA;
char *header = new char[4];
CString sizeCnt;
sizeCnt.Format("%d", bufferSize);
strcpy(header, static_cast<lpctstr>(sizeCnt));
bufferHDR.len = 4;
bufferHDR.buf = header;

// sending first data buffer contain the size

if (WSASend(socket, &bufferHDR, 1, &sentSize, MSG_OOB, 0, 0) == 0)
{
bufferDATA.len = bufferSize;
bufferDATA.buf = data;
sentSize = 0;
DWORD bufferProgress = 0;

// make sure all data gets sent
while (sentSize < bufferSize)
{
if (WSASend(m_ActiveSocket, &bufferDATA, 1, &bufferProgress, MSG_OOB, 0, 0) == 0)
{
sentSize += bufferProgress;
bufferProgress = 0;
}

else
{
DetermineErrorWSASend();
sentSize = bufferSize;
}
}

else
DetermineErrorWSASend();
-----

-----
DWORD bufferSize = 0,
receivedSize = 0,
flags = 0;
WSABUF bufferHDR,
bufferDATA;
char *header = new char[4], *data;
bufferHDR.len = 4;
bufferHDR.buf = header;


// reading first buffer to determine real data size
if (WSARecv(socket, &bufferHDR, 1, &receivedSize, &flags, 0, 0) == 0)
{
CString x;
bufferSize = static_cast<dword>(*bufferHDR.buf);
x.Format("%s%d", "bufferSize = ", bufferSize);
AfxMessageBox(x);

// allocating memory based on buffer size

data = new char[bufferSize];
bufferDATA.len = bufferSize;
bufferDATA.buf = data;
receivedSize = 0;
DWORD bufferProgress = 0;

// make sure all buffer get read

while (receivedSize < bufferSize)
{
if (WSARecv(socket, &bufferDATA, 1, &bufferProgress, &flags, 0, 0) == 0)
{
x.Format("%d", bufferProgress);
AfxMessageBox(x);
newData += *bufferDATA.buf;
receivedSize += bufferProgress;
}

else
{
DetermineErrorWSARecv();
receivedSize = bufferSize;
}
}
delete [] data;
delete [] header;
}
-----

Thanks,
Kuphryn
GeneralC++ Specification Pin
Andres Manggini15-May-02 15:43
Andres Manggini15-May-02 15:43 
GeneralRe: C++ Specification Pin
Maxwell Chen15-May-02 16:17
Maxwell Chen15-May-02 16:17 
GeneralDisableing the Windows Key Pin
Marc Richarme15-May-02 13:55
Marc Richarme15-May-02 13:55 
GeneralRe: Disableing the Windows Key Pin
Alex Cramer15-May-02 21:25
Alex Cramer15-May-02 21:25 
GeneralRe: Disableing the Windows Key Pin
Marc Richarme16-May-02 9:40
Marc Richarme16-May-02 9:40 
GeneralASSERT on CFileDialog.DoModal() Pin
Szymon Pusz15-May-02 13:05
Szymon Pusz15-May-02 13:05 
Generalheader file and library file Pin
peter ho15-May-02 12:40
peter ho15-May-02 12:40 
GeneralRe: header file and library file Pin
Joaquín M López Muñoz15-May-02 12:39
Joaquín M López Muñoz15-May-02 12:39 
Questionhow to import a library into my MFC project? Pin
peter ho15-May-02 11:36
peter ho15-May-02 11:36 
AnswerRe: how to import a library into my MFC project? Pin
Chris Losinger15-May-02 11:45
professionalChris Losinger15-May-02 11:45 
AnswerRe: how to import a library into my MFC project? Pin
Ravi Bhavnani15-May-02 11:57
professionalRavi Bhavnani15-May-02 11:57 
QuestionHow to handle Ctrl+<char> in an Edit control Pin
Chen Venkataraman15-May-02 11:31
Chen Venkataraman15-May-02 11:31 
AnswerRe: How to handle Ctrl+<char> in an Edit control Pin
PJ Arends15-May-02 18:10
professionalPJ Arends15-May-02 18:10 
GeneralCListView MDI - Double Buffering to reduce flicker Pin
RobJones15-May-02 10:51
RobJones15-May-02 10:51 
GeneralRe: CListView MDI - Double Buffering to reduce flicker Pin
Joaquín M López Muñoz15-May-02 12:27
Joaquín M López Muñoz15-May-02 12:27 
GeneralRe: CListView MDI - Double Buffering to reduce flicker Pin
Shog915-May-02 12:49
sitebuilderShog915-May-02 12:49 
GeneralRe: CListView MDI - Double Buffering to reduce flicker Pin
elnino15-May-02 13:49
elnino15-May-02 13:49 

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.