T
Tom
I try to write a simple tcp based job queue server. It's purpose is to
get commands from a client and store them in a queue (STL). The server
has a thread which checks periodically the queue, executes the commands
in it and removes them from the queue after done so. It also has a
thread for every client connection. I am using the low level SOCKET
API. My server is a Win32 console app and my client is MFC. The
followinf struct is passed through the sockets:
typedef struct
{
int iCmdId;
char szJobID[JOBIDLENGTH];
char szWorkPath[PATHLENGTH];
char szUser[USERLENGTH];
} SERVERCMD;
The client can query the server about the jobs currently queued. The
server sends then each item (the whole struct) in the queue to the
client. The wired problem with this function is that for a while
everything seems fine. I get all queued jobs listed in my clients
CListBox. But after repeatingly calling the ListJobs() function my data
get somehow corrupted (although the queue doesn't change). In the
following I post the sourcode for the server's and client's ListJobs()
function and the debuging output which clearly shows the problem.
server:
void ListJobs( SOCKET client )
{
int bytesSent = 0;
char buffer[5];
itoa( CmdQueue.size(), buffer, 10 );
bytesSent = send( client, (char*)&buffer, sizeof(buffer), 0 );
LogMessage( "ListJobs: sent %d bytes -> nJobs=%d\n", bytesSent,
CmdQueue.size() );
for (int i=0; i<CmdQueue.size(); i++)
{
SERVERCMD job = CmdQueue;
bytesSent = send( client, (char*)&job, sizeof(SERVERCMD), 0 );
LogMessage( "ListJobs: sent %d bytes of jobdata\n", bytesSent
);
}
LogMessage( "\n" );
}
client:
SERVERCMD * ListJobs( int * nJobs )
{
if ( sockClient == NULL )
return NULL;
int bytesSent,bytesRecv;
SERVERCMD * jobs;
// send list request to server
SERVERCMD job;
job.iCmdId = CMD_LSTJOB;
bytesSent = send( sockClient, (char*)&job, sizeof(SERVERCMD), 0 );
if ( bytesSent == SOCKET_ERROR )
return NULL;
// receive number of jobs
*nJobs = 0;
char buffer[5];
bytesRecv = recv( sockClient, (char*)&buffer, sizeof(buffer), 0 );
if ( bytesRecv != SOCKET_ERROR )
{
*nJobs = atoi( buffer );
debug("ListJobs: reveiced %d bytes -> nJobs=%d\n", bytesRecv,
*nJobs);
// allocate space for jobs
size_t jobsSize = *nJobs * sizeof(SERVERCMD);
jobs = (SERVERCMD*)malloc( jobsSize );
debug("ListJobs: allocating %d bytes\n", jobsSize);
// receive jobs
for (int i=0;i<*nJobs; i++)
{
bytesRecv = recv( sockClient, (char*)&(jobs),
sizeof(SERVERCMD), 0 );
if ( bytesRecv == SOCKET_ERROR ) return NULL;
debug("ListJobs: received %d bytes of jobdata\n",
bytesRecv);
}
debug("\n");
}
else
return NULL;
return jobs;
}
In the following debugging output 2 jobs were put into the queue. The
queue was then queried multible times.
server:
ListJobs: sent 5 bytes -> nJobs=1 <-- one job in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
(...)
ListJobs: sent 5 bytes -> nJobs=2
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
client:
ListJobs: reveiced 5 bytes -> nJobs=1 <-- one job in queue
ListJobs: allocating 1228 bytes <-- size of 1*SERVERCMD
ListJobs: received 1228 bytes of jobdata <-- this is OK
ListJobs: reveiced 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: allocating 2456 bytes <-- OK
ListJobs: received 1228 bytes of jobdata <-- OK
ListJobs: received 1228 bytes of jobdata <-- OK
(after quering queue multible times)
ListJobs: reveiced 5 bytes -> nJobs=2
ListJobs: allocating 2456 bytes
ListJobs: received 1228 bytes of jobdata
ListJobs: received 232 bytes of jobdata <-- Woho, what's that?
ListJobs: reveiced 5 bytes -> nJobs=0 <-- HELP!!!
ListJobs: allocating 0 bytes
I really need help with that. Maybe this is a thread thing, but in my
test I used only one client.
__
Tom
get commands from a client and store them in a queue (STL). The server
has a thread which checks periodically the queue, executes the commands
in it and removes them from the queue after done so. It also has a
thread for every client connection. I am using the low level SOCKET
API. My server is a Win32 console app and my client is MFC. The
followinf struct is passed through the sockets:
typedef struct
{
int iCmdId;
char szJobID[JOBIDLENGTH];
char szWorkPath[PATHLENGTH];
char szUser[USERLENGTH];
} SERVERCMD;
The client can query the server about the jobs currently queued. The
server sends then each item (the whole struct) in the queue to the
client. The wired problem with this function is that for a while
everything seems fine. I get all queued jobs listed in my clients
CListBox. But after repeatingly calling the ListJobs() function my data
get somehow corrupted (although the queue doesn't change). In the
following I post the sourcode for the server's and client's ListJobs()
function and the debuging output which clearly shows the problem.
server:
void ListJobs( SOCKET client )
{
int bytesSent = 0;
char buffer[5];
itoa( CmdQueue.size(), buffer, 10 );
bytesSent = send( client, (char*)&buffer, sizeof(buffer), 0 );
LogMessage( "ListJobs: sent %d bytes -> nJobs=%d\n", bytesSent,
CmdQueue.size() );
for (int i=0; i<CmdQueue.size(); i++)
{
SERVERCMD job = CmdQueue;
bytesSent = send( client, (char*)&job, sizeof(SERVERCMD), 0 );
LogMessage( "ListJobs: sent %d bytes of jobdata\n", bytesSent
);
}
LogMessage( "\n" );
}
client:
SERVERCMD * ListJobs( int * nJobs )
{
if ( sockClient == NULL )
return NULL;
int bytesSent,bytesRecv;
SERVERCMD * jobs;
// send list request to server
SERVERCMD job;
job.iCmdId = CMD_LSTJOB;
bytesSent = send( sockClient, (char*)&job, sizeof(SERVERCMD), 0 );
if ( bytesSent == SOCKET_ERROR )
return NULL;
// receive number of jobs
*nJobs = 0;
char buffer[5];
bytesRecv = recv( sockClient, (char*)&buffer, sizeof(buffer), 0 );
if ( bytesRecv != SOCKET_ERROR )
{
*nJobs = atoi( buffer );
debug("ListJobs: reveiced %d bytes -> nJobs=%d\n", bytesRecv,
*nJobs);
// allocate space for jobs
size_t jobsSize = *nJobs * sizeof(SERVERCMD);
jobs = (SERVERCMD*)malloc( jobsSize );
debug("ListJobs: allocating %d bytes\n", jobsSize);
// receive jobs
for (int i=0;i<*nJobs; i++)
{
bytesRecv = recv( sockClient, (char*)&(jobs),
sizeof(SERVERCMD), 0 );
if ( bytesRecv == SOCKET_ERROR ) return NULL;
debug("ListJobs: received %d bytes of jobdata\n",
bytesRecv);
}
debug("\n");
}
else
return NULL;
return jobs;
}
In the following debugging output 2 jobs were put into the queue. The
queue was then queried multible times.
server:
ListJobs: sent 5 bytes -> nJobs=1 <-- one job in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
(...)
ListJobs: sent 5 bytes -> nJobs=2
ListJobs: sent 1228 bytes of jobdata
ListJobs: sent 1228 bytes of jobdata
client:
ListJobs: reveiced 5 bytes -> nJobs=1 <-- one job in queue
ListJobs: allocating 1228 bytes <-- size of 1*SERVERCMD
ListJobs: received 1228 bytes of jobdata <-- this is OK
ListJobs: reveiced 5 bytes -> nJobs=2 <-- two jobs in queue
ListJobs: allocating 2456 bytes <-- OK
ListJobs: received 1228 bytes of jobdata <-- OK
ListJobs: received 1228 bytes of jobdata <-- OK
(after quering queue multible times)
ListJobs: reveiced 5 bytes -> nJobs=2
ListJobs: allocating 2456 bytes
ListJobs: received 1228 bytes of jobdata
ListJobs: received 232 bytes of jobdata <-- Woho, what's that?
ListJobs: reveiced 5 bytes -> nJobs=0 <-- HELP!!!
ListJobs: allocating 0 bytes
I really need help with that. Maybe this is a thread thing, but in my
test I used only one client.
__
Tom