I must program a client socket
-query the status from the server
-send data to the server
The data is sent using a struct, and around 110,000 records are sent in a session of approximately half an hour. During this time I should check the
status from the server continuosly.
What I want to do is create two threads that share the same socket, one
for sending the data (Thread 1) and another to check the status(Thread 2). I am using mutex but
I dont know if it is right.Because I need to meet the following conditions;
Thread 2 should finish when Thread 1 finishes.
If there is an error in the status Thread2 should communicate this to Thread 1 so it must stop.
Ideally, There 2 should be executed only once after certain time.
Basically I have a few lines of code, something like this:
int main (void)
{ DWORD ThreadID;
cout <<"Client application...";
hSocketMutex = CreateMutex( NULL,FALSE,NULL);
DWORD dwCount=0, dwWaitResult;
ifstream archConfig(file);
if (!archConfig)
{
printf("File not found");
return 1;
}
streampos inicio = archConfig.seekg(0, ios::beg).tellg();
int wordCount=0;
char word[80];
while (!archConfig.eof()) {
dwWaitResult = WaitForSingleObject(hSocketMutex, INFINITE);
switch (dwWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
//__try {
// TODO: split content of the file and send through the socket
//}
//__finally {
// Release ownership of the mutex object
if (! ReleaseMutex(hSocketMutex))
{
// Handle error.
}
//}
break;
case WAIT_ABANDONED:
return FALSE;
}
}
return TRUE;
}
I am completely new in thread programing so I dont know if I am in the right direction? could you please give me some advice. Specially for the function to
get the status since I dont have any idea of how to manage to execute that code only once at certain time and sharing the socket with initTransmition.
how many ports you are using for the two sockets in the two threads? When receivng status from the server, is the client side in "Listen" ( if yes, it's a server behavior).
What I can imagine is the two jobs better to be done by two individual pairs of client and server.