Subj : Re: problems with sockets while implemting posix sockets To : comp.programming.threads From : Dave Thompson Date : Mon Feb 28 2005 07:09 am On 21 Feb 2005 12:50:09 -0800, "puzzlecracker" wrote: > In the function msg_receive, fd is a socket connected to a remote > Internet client. What is wrong with msg_receive? Suggest a fix. 0) Missing #include's of needed headers and/or (prototype) declarations. This is arguably not an error 'with' the function itself. Fix: supply them. > #define MAXVALS 65536 > > typedef struct _msg_t { > unsigned int num; > unsigned int values[MAXVALS]; > } msg_t; > > int msg_receive (int fd, msg_t *m) 1) It uses (a typedef referring to) a struct tag beginning with an underscore, which is reserved to the implementation at file scope in the tag namespace (also ordinary). Again not really in the function. Fix: use msg_t; tags do _not_ conflict with ordinary identifiers. > { > size_t sz = sizeof (unsigned int); 2) There is no reason to use a variable for this value, and even more so to use the same one for the sizes of both .num and .values[i]. Fix: use sizeof(appropriate data item) below. > assert (m); 3) In C90, assert() is only guaranteed to work for integers, not other scalar types. In practice it does, and fixed in C99. Fix: != NULL. > if (read (fd, (void *)&m->num, sz) < sz) 4) Cast not needed, given a correct prototype. Fix: delete. > return -1; > return read (fd, (void *)&m->values, sz * m->num); 5) Your turn. This is almost certainly the one (maybe two) your teacher wanted you to find, and is basic to the usual definition of sockets semantics which you are supposed to be learning. Which is offtopic here, as are details of C also. > } - David.Thompson1 at worldnet.att.net .