Re: sending echo to all clients
- From: arnuld <sunrise@xxxxxxxxxxxxxxx>
- Date: Tue, 01 Jul 2008 10:41:39 +0500
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 ?
/* 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.
Oops, you're checking 'errno' even if the return value is zero.
Now I know why Stevens did that. Code changed.
And why are you operating on 'accept_fd' rather than clients[i].fd?
My mistake. code changed.
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 :-\
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
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
/* 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(...)
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 =:)
--
www.lispmachine.wordpress.com
my email is @ the above blog
check the "About Myself" page
.
- Follow-Ups:
- Re: sending echo to all clients
- From: David Schwartz
- Re: sending echo to all clients
- References:
- Re: sending echo to all clients
- From: David Schwartz
- Re: sending echo to all clients
- Prev by Date: Re: sending echo to all clients
- Next by Date: Need Shell Script help
- Previous by thread: Re: sending echo to all clients
- Next by thread: Re: sending echo to all clients
- Index(es):
Relevant Pages
|