void StartRecvFile(SOCKET s) {// retrives message from server that gives filename, and filelen; int left; int ret; int bytesrecieved; char temprb[1024]; // temp recieve buffer. int readidx,writeidx; int msglen; // found out how long the message is. bytesrecieved=0; writeidx=0; while(bytesrecieved < 4) { // wait untill we know how big the message is going to be. ret = recv(s,&temprb[writeidx],4-bytesrecieved,0); if(ret == 0) { MessageBox(NULL,"Socket Closed","Client Error",MB_OK); PostQuitMessage(0); return; } if(ret == SOCKET_ERROR) { MessageBox(NULL,"Socket Error","Client Error",MB_OK); PostQuitMessage(0); return; } else { bytesrecieved += ret; writeidx += ret; } } readidx=0; BufferRead(&msglen,4,1,temprb,&readidx); // Now recieve the rest of the message. left = msglen-4; while(left) { ret = recv(s,&temprb[writeidx],left,0); if(ret == 0)// Socket closed. { MessageBox(NULL,"Socket Closed","Client Error",MB_OK); PostQuitMessage(0); return; } else if(ret == SOCKET_ERROR) { MessageBox(NULL,"Socket Error","Client Error",MB_OK); PostQuitMessage(0); return; } else // success. { left -= ret; writeidx += ret; } }// end while(bytes left to read) // Now read the filename and filelen; readidx = 6; // skip past header and get to data. BufferReadString(filename,temprb,&readidx); BufferRead(&filelen,4,1,temprb,&readidx); sprintf(temprb,"filename:%s\nfilelength:%d",filename,filelen); MessageBox(NULL,temprb,"Client Notice",MB_OK); // Create the file for writing fp = fopen(filename,"wb"); if(fp == NULL) { MessageBox(NULL,"Unable to create file","Error",MB_OK); PostQuitMessage(0); } }// end StartFileRecv(sock). unsigned _stdcall RecvFileProc(void *param) { long count; // number of bytes left for entire file. int ret; StartRecvFile(s); count = filelen; while(count) { ret = recv(s,rb,BUFFER_SIZE,0); if(ret == 0) { MessageBox(NULL,"Error Socket Closed.","Client Error",MB_OK); return -1; } else if(ret == SOCKET_ERROR) { MessageBox(NULL,"Socket Error.","Client Error",MB_OK); return -1; } else // success. { count -= ret; total_recieved += ret; fwrite(rb,1,ret,fp); InvalidateRect(PROGRAMWND,NULL,1); } }// end while(file not done). MessageBox(NULL,"File Complete.\nPlease Close this program window.","Success",MB_OK); fclose(fp); return 0; }// end RecvProc(...)