November 30th, 2010, 09:14 AM
#1
How to keep track of threads
I am having problems synchronizing two threads, one on the client side and one on the server side.
Basically, it is a file transfer function using TCP protocol. I think posting the code will make more sense.
TransferClient():
WCHAR wSendBuffer[32768];
WCHAR wFile[1024],wPendingCount[256];
WCHAR count[32];
memset(count,0,32);
char c[32];
memset(c,0,32);
houseKeeper->getTempPath(wFile,1024);
wcscat(wFile,L"Filecount");
wcscpy_s(wSendBuffer, 32768, L"TXE\n");
wcscat_s(wSendBuffer, 32768, wJobToTransfer);
wcscat_s(wSendBuffer, 32768, L"\n");
wcscat_s(wSendBuffer, 32768, wFileCount);
wcscat_s(wSendBuffer, 32768, L"\n");
int retry_times = 0;
cout << "7 - ";
bool blSent = true;
shData.sendAll((char*)wSendBuffer, sizeof(WCHAR)*wcslen(wSendBuffer));
cout << "8 - ";
cout << "Split: " << iCount << " - ";
int i = 1;
while(i<iCount){
WCHAR wSplitFileName[PATH_LENGTH];
wcscpy_s(wSplitFileName, PATH_LENGTH, wPendingTransfersFile);
wcscat_s(wSplitFileName, PATH_LENGTH, L".");
_itow_s(i, wFileCount, 256, 10);
wcscat_s(wSplitFileName, PATH_LENGTH, wFileCount);
cout<<"Count: "<<i<<endl;
int retVal = sendFile(wSplitFileName, shData);
cout << "RetVal: " << retVal << endl;
if (retVal==0) { //File failed to transfer.
return false;//retry file transfer
}
i++;
}
return true;
====================================================================
Transferserver():
if (wcsncmp (wMode, L"TXE", 3) == 0) {
WCHAR wFileName[PATH_LENGTH];
memset(wFileName, 0, sizeof(WCHAR)*PATH_LENGTH);
socHandler.readLine ((char*)wFileName);
WCHAR wFileCount[256];
socHandler.readLine ((char*)wFileCount);
WCHAR wPendingTransfersFile[PATH_LENGTH];
houseKeeper->getTempPath(wPendingTransfersFile, PATH_LENGTH);
wcscat_s(wPendingTransfersFile, PATH_LENGTH, L"PendingTransfers\\");
wcscat_s(wPendingTransfersFile, PATH_LENGTH, wFileName);
int iSplitFileCount = _wtoi(wFileCount);
WCHAR wStatusFile[PATH_LENGTH];
wcscpy_s(wStatusFile, PATH_LENGTH, wPendingTransfersFile);
wcscat_s(wStatusFile, PATH_LENGTH, L"x_importing");
HANDLE hStatusFile = CreateFile(wStatusFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS , 0, 0);
if (hStatusFile != INVALID_HANDLE_VALUE) {
CloseHandle(hStatusFile);
}
for (int i=1; i<=iSplitFileCount; i++) {
WCHAR wSplitFileName[PATH_LENGTH];
wcscpy_s(wSplitFileName, PATH_LENGTH, wPendingTransfersFile);
wcscat_s(wSplitFileName, PATH_LENGTH, L".");
_itow_s(i, wFileCount, 256, 10);
wcscat_s(wSplitFileName, PATH_LENGTH, wFileCount);
bool blRec = CHelper::receiveFile(wSplitFileName, socHandler);
cout<<"Count: "<<i<<endl;
wcout << "Rec: " << blRec << " - " << wSplitFileName << endl;
if (blRec == 0) {
socHandler.close();
DeleteFile(wStatusFile);
return;
}
}
End of Code
Whenever the function in the Transferclient is called, a new thread is created on the Transfer server. The function called receivefile() opens a handle to a file, and gets locked waiting on socket recv() function.
How do I synchronise the client and the server, such that each client gets to open only a single thread to avoid locking of file.
Or is there a way to time out the socket recv() function? Please note I am using ICP_PROTO with TCP_NODELAY level.
Tags for this Thread
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
Bookmarks