Choklander
asked on
Winsock 2.2 recv not working
The recv function is not properly working for me.
I am trying to send an array of strings from one computer to the other.
In most cases from the 4 variables only 2 are sent!.
It feels like the buffer on recv is not properly flushing or some other annoying ****.
This is how I sent:
for (unsigned int i = 0; i < m_vsListName.size(); ++i)
{
iResult = SendMessageToSocket(m_Clie ntSocket, m_vsListName.at(i).GetBuff er());
if (iResult == SOCKET_ERROR)
{
printf("Could not Send list. sent: %d, still to send: %d\n", i, m_vsListName.size() - 1);
return iResult;
}
}
====================
int SendMessageToSocket(SOCKET &SocketToSentTo, const char* pMessage)
{
return iResult = send(SocketToSentTo, pMessage, (int) strlen(pMessage) + 1, 0);
}
The +1 is for the "\n".
========================== ========== =========
This is how I receive.
int Cclient_streamDlg::GetList ()
{
int iResult;
int recvbuflen = 512;
char recvbuf[512];
int i = 0;
do
{
iResult = recv(m_ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
CString str;
str.Format(L"%d", i + 1);
m_lcList.InsertItem(i, str);
m_lcList.SetItemText(i, 1, CStringW(recvbuf));
++i;
}
else if (iResult == 0)
{
printf("Clossing connection\n");
}
else
{
printf("Connection error\n");
}
} while (iResult > 0);
m_lcList.UpdateData();
return iResult;
}
Any clue why only those 2 are sent????
I am trying to send an array of strings from one computer to the other.
In most cases from the 4 variables only 2 are sent!.
It feels like the buffer on recv is not properly flushing or some other annoying ****.
This is how I sent:
for (unsigned int i = 0; i < m_vsListName.size(); ++i)
{
iResult = SendMessageToSocket(m_Clie
if (iResult == SOCKET_ERROR)
{
printf("Could not Send list. sent: %d, still to send: %d\n", i, m_vsListName.size() - 1);
return iResult;
}
}
====================
int SendMessageToSocket(SOCKET
{
return iResult = send(SocketToSentTo, pMessage, (int) strlen(pMessage) + 1, 0);
}
The +1 is for the "\n".
==========================
This is how I receive.
int Cclient_streamDlg::GetList
{
int iResult;
int recvbuflen = 512;
char recvbuf[512];
int i = 0;
do
{
iResult = recv(m_ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
CString str;
str.Format(L"%d", i + 1);
m_lcList.InsertItem(i, str);
m_lcList.SetItemText(i, 1, CStringW(recvbuf));
++i;
}
else if (iResult == 0)
{
printf("Clossing connection\n");
}
else
{
printf("Connection error\n");
}
} while (iResult > 0);
m_lcList.UpdateData();
return iResult;
}
Any clue why only those 2 are sent????
The TCP/IP protocol merely states that packets will be received in the same ORDER as they were sent. It has no notion of where your "message" begins or ends though. To the infrastructure it's all just one STREAM. Routers along the way may get backed up and hold your data temporarily while it does its thing. Then more packets may come in and it will add it to the buffer. When it gets back to your data it sends off what it has in whatever "chunk size" it thinks is best for the conditions.
With that in mind, ONE message may actually be broken up and received in multiple events as little pieces or MULTIPLE messages may end up getting crammed together...or any combination. The pieces will all be in the correct order though.
The standard way to handle this is to develop some kind of "end of message marker" that makes sense for your data. Then, as you receive packets you add the data to a buffer and then parse it looking for the EOM marker. This way you can keep partial pieces of messages until they are completely received. It will also allow you to find complete messages that have been concatenated together.
I don't code in C/C++ so I can't give you an example but that is the general idea... =)
With that in mind, ONE message may actually be broken up and received in multiple events as little pieces or MULTIPLE messages may end up getting crammed together...or any combination. The pieces will all be in the correct order though.
The standard way to handle this is to develop some kind of "end of message marker" that makes sense for your data. Then, as you receive packets you add the data to a buffer and then parse it looking for the EOM marker. This way you can keep partial pieces of messages until they are completely received. It will also allow you to find complete messages that have been concatenated together.
I don't code in C/C++ so I can't give you an example but that is the general idea... =)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the answers.
ASKER
I receive 2 items because the second one contains everything!. How is that possible. Is it not that for each send it corresponds one recv? Guess not. How would I go about it and take the second big string apart and recognize what is new and what not?
What if I do not stream strings but numbers? What a mess that would be. How would I go about identifying how big each send was so that I read exactly that much from recv?