Re: sending echo to all clients



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

.



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 ... you receive C-style strings, so there's really no point to doing it. ...
    (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: test if handle exists? How???
    ... > ishandlegives an array of nine 0s ... does not work because 'handles.c' is a literal character vector of 9 characters and that character vector is not an empty array. ... If what you are trying to do is find out whether there is a graphics object with the tag 'abcdef' then use ...
    (comp.soft-sys.matlab)
  • Re: easy parsing problem in C for beginner
    ... As far as I can tell you're getting an array of time slot priorities ... so I went with the WHILE loops and my own counter. ... char prio = PA_GetArrayElementAtIndex; ... characters are in that array. ...
    (comp.sys.mac.programmer.help)