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[i];
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[i]),
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 6 3231
I forgot to mention that this effect only appears when I run the server
on a different host. When both, server and client, are on one host the
problem do not occure.
On Mon, 8 Aug 2005, Tom wrote: 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[i]; 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[i]), 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
.... And the remaining of your post :
I forgot to mention that this effect only appears when I run the server on a different host. When both, server and client, are on one host the problem do not occure.
comp.lang.c isn't the right newsgroup to post to for your problem. I've a
feeling that c.u.p. might be more appropriate.
X-post & f.u.2 where relevant
--
"Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
que je veux !"
"The obvious mathematical breakthrough would be development of an easy
way to factor large prime numbers." (Bill Gates, The Road Ahead)
Tom wrote: I forgot to mention that this effect only appears when I run the server on a different host. When both, server and client, are on one host the problem do not occure.
Hi,
Your code looks rather like C++ to me... But your problems are likely
to do
with send/recv, which aren't really on topic in comp.lang.c++ either.
I suggest you ask in comp.os.ms-windows.programmer.win32, you will
get better help there (or in comp.unix.programmer, assuming your
problems aren't windows specific).
<OT> Look at recv documention for your system,
you may not get as many bytes as you ask for,
in which case you need to try again until you do. recv
takes flags that modify this behavior, look at them. <OT>
-David
I apologize for posting my issue on this group. I already posted it to
comp.os.ms-windows.programmer.win32,
microsoft.public.win32.programmer.networks and alt.winsock.programming.
But I tought maybe I can get help here too.
On 8 Aug 2005 09:03:31 -0700, "Tom" <ba***@b3s.de> wrote: 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
As others have noted the STL part is C++ and offtopic in c.l.C, but
this is relatively small and easily ignorable. "Mixed" declarations
(that is, not solely at the beginning of a block) and double-slash
comments which were introduced in C++ _are_ standard in C as of C99,
but not yet universally implemented. And double-slash comments are
still unwise in news postings, since those may get line breaks (wraps)
added at various points, and this breaks // comments but does not harm
/* */ comments. (It also harms long preprocessor #directives, the only
other place lines are significant.)
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
Server thread per connection/client scales poorly; see any week (of
the last N years) of comp.programming.threads. But leave that for now.
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;
In general it's a poor idea to send C-language structs over a network;
compilers on different types of systems can lay them out differently,
and sometimes also different compilers or the same compiler with
different options on the same system type. This is why you see proper
network protocols specified in terms of actual bits (or nowadays
usually octets) on the wire and not in C or other HLL. But since you
apparently are using Wintel and probably the same compiler at both
(all) endpoints, and structs that are _mostly_ chars, leave that also.
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 );
itoa() is not standard in C or C++. sprintf() is. If the number of
jobs is > 9999 this overflows the buffer, formally causing Undefined
Behavior although in practice on nearly all if not all machines this
particular UB doesn't actually cause harm until you get to at least 6
digits and probably 8 digits. And the latter at least probably won't
happen because you'll hit other limits first. (Like uptime!)
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[i]; bytesSent = send( client, (char*)&job, sizeof(SERVERCMD), 0 );
Perhaps clearer to use sizeof(job), and for an object/expression (but
not a typename) can omit the parentheses = sizeof job .
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);
If the server numjobs was > 9999 this reads an unterminated (and
possibly very wrong) value on which atoi() isn't safe. In fact atoi()
isn't safe in the presence of almost any error; strtol (and ul, and ll
and ull on C99 systems) handles some errors but not unterminated.
// allocate space for jobs size_t jobsSize = *nJobs * sizeof(SERVERCMD); jobs = (SERVERCMD*)malloc( jobsSize );
The cast wouldn't be needed in C, but is in C++, and you don't check
the result is nonnull before using it (below). In C++ it is briefer,
clearer, and more robust to just write jobs = new SERVERCMD [*nJobs],
which also throws (by default) on allocation failure.
debug("ListJobs: allocating %d bytes\n", jobsSize);
%d expects an int (signed) but jobsSize is size_t which is definitely
unsigned and may be a different size than int or unsigned int. In C99
there is a specific modifier for this; in C90 and C++ probably best to
cast to and specify unsigned long, or perhaps unsigned long long if
you have it. Or in C++ use << instead, to a stringstream if necessary.
// receive jobs for (int i=0;i<*nJobs; i++) { bytesRecv = recv( sockClient, (char*)&(jobs[i]), 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.
As already noted, recv() on a bytestream protocol like TCP can receive
only part of what you asked for (and was sent). You need to use
MSG_COMP if available, which I don't think it is on (most?) Winsock,
or be prepared to read multiple pieces and combine them.
- David.Thompson1 at worldnet.att.net
Dave Thompson <da*************@worldnet.att.net> writes:
[...] As others have noted the STL part is C++ and offtopic in c.l.C, but this is relatively small and easily ignorable. "Mixed" declarations (that is, not solely at the beginning of a block) and double-slash comments which were introduced in C++ _are_ standard in C as of C99, but not yet universally implemented. And double-slash comments are still unwise in news postings, since those may get line breaks (wraps) added at various points, and this breaks // comments but does not harm /* */ comments. (It also harms long preprocessor #directives, the only other place lines are significant.)
Lines are also significant in string literals. E.g., "splitting this
across lines" will cause problems.
[...]
In general it's a poor idea to send C-language structs over a network; compilers on different types of systems can lay them out differently, and sometimes also different compilers or the same compiler with different options on the same system type. This is why you see proper network protocols specified in terms of actual bits (or nowadays usually octets) on the wire and not in C or other HLL. But since you apparently are using Wintel and probably the same compiler at both (all) endpoints, and structs that are _mostly_ chars, leave that also.
It's not uncommon for structs to be laid out in the same way for
different compilers on the same platform, so there's some hope of
combining code compiled with different compilers, but generally it's
not wise to depend on it.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jane Austine |
last post by:
Running Python 2.3 on Win XP
It seems like socket is working interdependently with subprocesses of
the process which created socket.
------------------------------------
#the server side
>>>...
|
by: Sori Schwimmer |
last post by:
Hi,
I am working on an application which involves
interprocess communication. More to the point,
processes should be able to notify other processes
about certain situations, so the "notifyees"...
|
by: Skink |
last post by:
Hi,
I'm preparing a python server that sends java classes and resources to
custom java class loader. In order to make it faster I don't want to use
URLClassLoader that uses HTTP protocol 1.0 and...
|
by: SW1 |
last post by:
I wrote a small program which does something like tftp - transfering
files and some chat, anyway i got a problem with fwrite, here is a
snippet of my code:
while(length > 0) {
putchar('.');...
|
by: Matt |
last post by:
I wrote the tcp socket client-server program that the server will
echo the message received from the client.
In client program:
char sendBuf;
while(1)
{
cout << "Enter message:";...
|
by: Terry |
last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a
"recvFrom()" call and other thread sends a UDP packet to some address, that
if the machine on the other end isn't up,...
|
by: Tony Caduto |
last post by:
Hi,
I am developing a socket server in C# and I want it to be blocking.
Anyway I have it working with threads, as each client connects the socket
spawned from the accept method is sent to a client...
|
by: Tomas Machala |
last post by:
Hi, I'm trying to make an application communicating over TCP/IP. It should do only one thing - write received data to console and terminate itself when "exit" received. Problem is that if I send some...
|
by: ifmusic |
last post by:
I have This Code.
Token.c:
typedef struct {
unsigned char orden;
char ciudad;
unsigned char ip; //que son del router Asociado a la ciudad
unsigned int puerto; // """
//int socket;
}Ciudad;
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |