Re: sending echo to all clients




arnuld wrote:

On Mon, 30 Jun 2008 19:43:52 -0700, David Schwartz wrote:

On Jun 30, 3:21�am, arnuld <sunr...@xxxxxxxxxxxxxxx> wrote:

nready = poll( clients, max_index + 1, INFTIM );

Oops, you forgot to setup 'clients'. It will be right the first time,
but it will be wrong the second time.


I did initialize it up properly, see:

/* poll() initialisation */
clients[0].fd = sockfd;
clients[0].events = POLLIN;
max_index = 0;

for(i = 1; i < OPEN_MAX; ++i )
{
clients[i].fd = -1;
}


Is there something wrong with that kind of initialization ?

You're right. For 'poll', you only need to stet it up once and you are
guarantee that each call to 'poll' will set the 'revents' parameter
correctly.

/* After the check, now we can add to events */
clients[curr_index].events = POLLIN;

Umm, you're manipulating the 'clients' you got *BACK* from 'poll'.
That makes no sense.

ummmm... that is just a new accept()ed connection added into the clients
array and hence I need to make "events" POLLIN.

There is only one array and that is an array of pollfd structures named
"clients" in my code and simply "client" in code from UNP.

it seems like I did not get your point.

You are correct. This is only an issue with 'select', not 'poll'.

And if you throw away the return value from 'recv', how will you
know how many bytes to send each client?

I threw it away because I am really confused between bytes and the
array of characters I send. I want send a sentence Like "David Schwartz is
an ancient ape who knows about sockets ;)" but that sentence can be sent
as an array of characters only but then I can't because send() sends bytes
not array. Well, how I am supposed to send the sentence ? and on the top
of that recv() gives me bytes, not the characters. Why do I have to mess
with bytes rather than the intent of what I want to send ?

Mixing my program design, my characters, sentence and bytes I see that
send() does send *exactly* the whole sentence as bytes even when I tell it
the size in an integer format, not in byte format. ... Eh..... I am
getting confused :-\

You always send and receive bytes. How how translate those into
characters is up to you. Normally, you have a "protocol specification"
that specifies exactly how the bytes are supposed to be arranged on
the wire. Since you don't have one, you make mistakes in converting
form the application units (chunks of characters) to the protocol
units (chunks of a stream of bytes).

if( send( clients[i].fd, arrc,ARRSIZE, 0 ) < 0 ) � � � � � � �
� � � � � � � { � � � � � � � � � � � � � � �
perror("SEND() error"); � � � � � � � � � � � � � � �
exit( EXIT_FAILURE ); � � � � � � � � � � � � � � } � �� � �

Umm, you didn't receive 'ARRSIZE' bytes. You received *at* *most*
ARRSIZE bytes. So you are sending the clients the wrong number of bytes.
You are also clobbering your outer loop index.

same as above

� if( send(sockfd, arrc, strlen(arrc), 0) == -1 ) � � { � � �
perror("SEND() error");
� � � exit( EXIT_FAILURE );
� � }

Note that this does not send the terminating zero byte. It does send the
newline. This may or may not make sense.

so it should be strlen(arrc) + 1

That depends on whether you wan to send the terminating zero byte.
IMO, there's no point. Sending C-style strings won't guarantee that
you receive C-style strings, so there's really no point to doing it.

� if( recv(sockfd, arrc_store, ARRSIZE, 0) == -1 ) � � { � � �
perror("RECV() error");
� � � exit( EXIT_FAILURE );
� � }

You threw away the return value from 'recv'. Now you have no idea how
many bytes you got back. Also, you have no idea if 'recv' returned zero
(indicating normal closing of the connection).


� printf("--%s", arrc_store);

Umm, '%s' is for C-style strings, not for arbitrary bytes. You need to
either make a C-style string or print each character received.

arrac_store is declared as an array of characters. May be C automatically
converted the bytes into an array of characters

An array of characters is not a string. A string may be stored in an
array of characters, but not every array of characters is a string.
Consider:

char j[2]={'a', 'b'};

What would happen if you do this:
printf("%s\n", j);

Notice that this is illegal and %s will not know when to stop and will
overrun the array.

� /* just to keep client connected */ � while(1) � � { � � � ;
� � }

Umm, huh? You don't need to run the CPU at 100% just to keep the client
connected. Why not block in 'recv'?

because recv() does not work. Client disconnects automatically when I
replace while(1) with recv(...)

That's probably a different bug entirely. How did you call 'recv'?

You have a large number of bugs.

I did not know that when I created the program. Well, thats why I posted
it here, to ask for improvements from socket-masters =:)

You should start by writing a protocol specification. It would have
probably avoided a large number of the bugs you made having to do with
not knowing what to send or what you received.

DS
.



Relevant Pages

  • Re: Array reinitialize
    ... |> | by a character string literal, ... |> | initialize the elements of the array." ... | is really 21 characters long. ... an array via an aggregate initialiser, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: sending echo to all clients
    ... I did initialize it up properly, ... There is only one array and that is an array of pollfd structures named ... as an array of characters only but then I can't because sendsends bytes ... So you are sending the clients the wrong number of bytes. ...
    (comp.unix.programmer)
  • Re: TCHAR, terminating and _tcscat
    ... That doesn't guarantee newstring "will always be nul-terminated". ... Note also that I don't need to initialize ... and if it copies fewer characters than the ... does it makes sense to fill an array with 250 zeroes and then ...
    (microsoft.public.vc.mfc)
  • Re: Subquery Confusion
    ... Then I got this crazy idea that an Array can only contain a maximum ... number of characters, ... Then I decide that maybe I'm completely wrong with my query, ... it out of Excel VBA and spit it into Microsoft SQL Server Management ...
    (microsoft.public.excel.programming)
  • Re: ifstream::get() surprise
    ... Extracts characters and stores them into successive locations ... > null character into the next successive location of the array. ... since the rule is to post the shortest code suffering from the ...
    (comp.lang.cpp)