|
-
July 18th, 2003, 04:48 AM
#1
string parsing problem??
hi,
Im writing a client to connect to the MSN Messenger network. When connecting for the first time everything works fine, but, whne redirected by the server and opening a new connection, the parser goes strange. Ive put my code for the recv/parse function below. The problem is that instead of:
VER 1 MSNP7\r\n
INF 2 MD5\r\n
etc, i get:
ER 1 MSNP7\r\n
NF 2 MD5\r\n
which is strange because it works fine when opening the first connection. I have also noticed that if i throw an ASSERT exception, everything seems to work fine also. could someone please help me?
Code:
void recieveData()
{
char buffer;
CString cmd("a", 4299);
int index = 0;
while(true)
{
int nbytes = recv(ns->socket, &buffer, sizeof(unsigned char), 0);
if(nbytes == SOCKET_ERROR)
{
// log_write("Recieve error: %d", WSAGetLastError());
buffer = '0';
}
else
{
if((nbytes >= 0))
{
log_write("%c, %d\n", buffer, index);
char *cmdStr = cmd.GetBuffer(4298);
ASSERT(index < 4298); // if assert is not thrown, result is for some reason garbled.
cmdStr[index] = buffer;
// cmd.SetAt(index, buffer);
index++;
if(buffer == '\n')
{
cmd.SetAt(index, '\0');
cmd = cmd.Left(index);
log_write("%s", cmd);
serverwindow_write((char *)((const char *)cmd), JM_RECIEVED);
int end = cmd.Find(" ", 4);
int l = end - 4;
CString id = CString(' ', 6);
for(int i = 0; i < l; i++)
{
id.SetAt(i, cmd[i + 4]);
}
stringtoint((char *)((const char *)id), recievedMsg.trID);
recievedMsg.cmd[0] = cmd[0];
recievedMsg.cmd[1] = cmd[1];
recievedMsg.cmd[2] = cmd[2];
CString parms = cmd.Right(cmd.GetLength() - end - 1);
parms = parms.Left(parms.GetLength() - 2);
parms += '\0';
recievedMsg.nParams = 0;
recievedMsg.params = new char *[10];
int b = 0;
int e = 0;
int k = 0;
if(parms.Find(' ', 0) == -1) // if there is only one parameter
{
recievedMsg.params = new char *[1];
recievedMsg.params[0] = new char[parms.GetLength()];
strcpy(recievedMsg.params[0], parms.GetBuffer(parms.GetLength()));
recievedMsg.nParams = 1;
}
else
{
while(e != parms.GetLength())
{
if(b != 0) {b = e + 1;}
e = parms.Find(' ', b);
if(e == -1) e = parms.GetLength();
recievedMsg.params[k] = new char[e - b];
recievedMsg.nParams++;
for(i = 0; i < (e - b); i++)
{
char c = parms.GetAt(b + i);
strcpy(&(recievedMsg.params[k])[i], &c);
}
strcpy(&(recievedMsg.params[k])[e-b], "\0");
k++;
if(b == 0) {b++;}
}
}
cmdsWaiting = true;
cmd.Empty();
cmd = CString("a", 4299);
index = 0;
buffer = NULL;
}
}
}
}
}
thanks
-
July 18th, 2003, 05:56 AM
#2
First, I have to say that your code is a bit hard to understand/read... Why don't you implement a function that reads a whole string from a socket stream?
That would clear up things... Your bug is that you lose one char (at some point).
With a string read function, the parsing would be a lot simplier.
-
July 18th, 2003, 09:18 AM
#3
It is difficult to debug your code. There is no comment. How did you come up with the solution? Did you design it or was it a shot in the dark?
Kuphryn
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|