Code only runs properly if debugging step-by-step
Posted
by
Cornwell
on Stack Overflow
See other posts from Stack Overflow
or by Cornwell
Published on 2010-12-23T14:57:21Z
Indexed on
2010/12/23
16:54 UTC
Read the original article
Hit count: 218
Hello,
I'm making a webserver and I've come up with some very strange problems.
- My server was running as expected yesterday when I turned off my laptop, but today it only sends the http headers (I didn't change anything)
- When a user requests a file, if I send them using the following code, it works perfectly:
while ((n = fread(data, 1, sizeof(data), file)) > 0)
send(ts, data, n, 0);
but if I change it to this, it only sends ~2% of the file. And that's not a random number, it actually only sends about 2% of the file.
while ((n = fread(data, 1, sizeof(data), file)) > 0)
web.Send(data);
int WEB::Send(string data)
{
return send(TempSocket, data.c_str(), data.size(), 0);
}
changing string to char* doesn't solve the problem.
- I'm using visual studio2010. If I run my code step-by-step, I am able to solve problem #1, everything gets sent. And that is my main problem. I do not understand why it happens. Hopefully someone can explain it to me.
Thanks in advance.
EDIT:
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmd,int nShow)
{
SOCKET MainSocket=0;
MSG msg;
RedirectIOToConsole();
CreateThread(NULL, NULL, ListenThread, NULL, NULL, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
WSACleanup();
closesocket(MainSocket);
MainSocket = INVALID_SOCKET;
return msg.wParam;
}
DWORD WINAPI ListenThread(LPVOID lparam)
{
SOCKET MainSocket;
WSADATA wsaData;
SOCKET tmpsock;
struct sockaddr_in local, from;
int fromlen=sizeof(from);
WSAStartup(MAKEWORD(2, 2), &wsaData);
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons(PORT);
MainSocket=socket(AF_INET,SOCK_STREAM,0);
if(MainSocket==INVALID_SOCKET)
{
return 0;
}
if(bind(MainSocket,(struct sockaddr*)&local,sizeof(local))!=0)
{
return 0;
}
if(listen(MainSocket,10)!=0)
{
return 0;
}
while(1)
{
tmpsock = accept(MainSocket,(struct sockaddr*)&from,&fromlen);
CreateThread(NULL, NULL, SlaveThread, (LPVOID)tmpsock, NULL, NULL);
}
}
DWORD WINAPI SlaveThread(LPVOID lparam)
{
SOCKET ts = (SOCKET)lparam;//temporary socket
......
char data[4096];
int n;
unsigned long int length = statbuf.st_size;
web.SendHeaders(200, "OK", format("Content-Disposition: attachment; filename=\"%s\"", FileName.c_str()).c_str(), web.GetMimeType(ReqPath.c_str()), length, statbuf.st_mtime);
unsigned long int i=0,d=0;
while ((n = fread(data, 1, sizeof(data), file)) > 0)
{
d+=send(ts, data, n, 0);
i+=n;
}
printf("%i=%i=%i\n", length,i,d);
fclose(file);
© Stack Overflow or respective owner