is it because another \0 is in another place in the char?
Main Topics
Browse All Topicshello I try to zero terminate a char
char szBuffer[BUFFER_SIZE];
szBuffer[nRet-1]='\0'; // zero terminate the char so we know where it ends
But I get debug errors when running it
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
int counter=0;
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer)-1, 0);
cout << "c: " << counter << " nRet: " << nRet <<endl;
szBuffer[nRet-1]='\0'; // zero terminate the char so we know where it ends
if (nRet == SOCKET_ERROR)
break;
// fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
break;
// Remove the http host header and store the content to a file
if (nRet !=0 || nRet != SOCKET_ERROR) {
if(counter==0) {
char* szBuffer_beginning1 = strstr(szBuffer, "\r\n\r\n") + 4;
WriteToFile(pszFile,(char*
}
if(counter==1) {
char* szBuffer_beginning2 = strstr(szBuffer, "\r\n\r\n") + 4;
WriteToFile(pszFile,(char*
}
if(counter>1) {
WriteToFile(pszFile,(char*
}
}
counter++;
}
closesocket(Socket);
>I don't really know what you mean about releasemode.
to the right of your startdebugging you can specifiy debug o relese mode... -> set to debug
after you get to your breakpoint the second time, use F10 to debug step by step through your code. I just wont to find out if it is really this line causing the exception. I can't be that nRet really is 115 or 34 and a exception is called
>just set after each instruction a breakpoint
hmm.. I've added breakpoints on all the lines occur here:
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer)-1, 0);
if (nRet == SOCKET_ERROR)
break;
cout << "c: " << counter << " nRet: " << nRet <<endl;
szBuffer[nRet]='\0'; // zero terminate the char so we know where it ends
// fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
break;
// Remove the http host header and store the content to a file
if (nRet !=0 || nRet != SOCKET_ERROR) {
if(counter==0) {
char* szBuffer_beginning1 = strstr(szBuffer, "\r\n\r\n") + 4;
WriteToFile(pszFile,(char*
}
if(counter==1) {
char* szBuffer_beginning2 = strstr(szBuffer, "\r\n\r\n") + 4;
WriteToFile(pszFile,(char*
}
if(counter>1) {
WriteToFile(pszFile,(char*
}
}
counter++;
}
closesocket(Socket);
Error 1 error LNK2001: unresolved external symbol __imp__WSAStartup@8 CRetriveWebPage.obj
Error 2 error LNK2001: unresolved external symbol __imp__WSACleanup@0 CRetriveWebPage.obj
Error 3 error LNK2001: unresolved external symbol __imp__inet_addr@4 CRetriveWebPage.obj
Error 4 error LNK2001: unresolved external symbol __imp__gethostbyname@4 CRetriveWebPage.obj
Error 5 error LNK2001: unresolved external symbol __imp__gethostbyaddr@12 CRetriveWebPage.obj
Error 6 error LNK2001: unresolved external symbol __imp__socket@12 CRetriveWebPage.obj
Error 7 error LNK2001: unresolved external symbol __imp__getservbyname@8 CRetriveWebPage.obj
Error 8 error LNK2001: unresolved external symbol __imp__htons@4 CRetriveWebPage.obj
Error 9 error LNK2001: unresolved external symbol __imp__connect@12 CRetriveWebPage.obj
Error 10 error LNK2001: unresolved external symbol __imp__closesocket@4 CRetriveWebPage.obj
Error 11 error LNK2001: unresolved external symbol __imp__send@16 CRetriveWebPage.obj
Error 12 error LNK2001: unresolved external symbol __imp__recv@16 CRetriveWebPage.obj
Error 13 fatal error LNK1120: 12 unresolved externals C:\Skole\semester4\Nettver
ok,
strstr retruns 0 if it can't find "\r\n\r\n", this means szBuffer_beginning2 = 0+4 -> this is not good, this is not a valid pointer and its not 0, this could cause a crash within WriteToFile.
Try
char* szBuffer_beginning2 = strstr(szBuffer, "\r\n\r\n");
if (szBuffer_beginning2 != 0)
WriteToFile(pszFile,szBuff
also change the code for szBuffer_beginning1
btw:
Just want to say that the working code looks like this now:
int counter=0;
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer)-1, 0);
if (nRet == SOCKET_ERROR)
break;
cout << "c: " << counter << " nRet: " << nRet <<endl;
szBuffer[nRet]='\0'; // zero terminate the char so we know where it ends
// fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
break;
// Remove the http host header and store the content to a file
if (nRet !=0 || nRet != SOCKET_ERROR) {
if(counter==0) {
char* szBuffer_beginning1 = strstr(szBuffer, "\r\n\r\n");
if (szBuffer_beginning1 != 0)
WriteToFile(pszFile,szBuff
}
if(counter>0) {
WriteToFile(pszFile,(char*
}
}
counter++;
}
closesocket(Socket);
i think if (nRet !=0 || nRet != SOCKET_ERROR) should be if (nRet !=0 && nRet != SOCKET_ERROR)
in this case you can delete this line cuz you ceckt both cases before and break the loop if they fail
int counter=0;
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer)-1, 0);
if (nRet == SOCKET_ERROR)
break;
cout << "c: " << counter << " nRet: " << nRet <<endl;
szBuffer[nRet]='\0'; // zero terminate the char so we know where it ends
// fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
break;
// Remove the http host header and store the content to a file
if(counter==0) {
char* szBuffer_beginning1 = strstr(szBuffer, "\r\n\r\n");
if (szBuffer_beginning1 != 0)
WriteToFile(pszFile,szBuff
}
if(counter>0) {
WriteToFile(pszFile,(char*
}
counter++;
}
closesocket(Socket);
Business Accounts
Answer for Membership
by: m-jansenPosted on 2006-04-14 at 14:14:50ID: 16457331
nRet = recv(Socket, szBuffer, sizeof(szBuffer)-1, 0);