write() a variable(integer) to socket?
I am working on a server/client chat system.
I want to add a feature to the server so that when the client sends "/list\n" to the server, the server will send all the names of the users currently logged in, together with the total number of the logged-in users. So, I send the usernames like this from the server: Code:
for (j=0; j<k; j++) { So, how can I send the number of the logged-in users, k, to the socket so that the client can get it? btw, k is an integer. Thanks. |
simply send an integer cast as a void * and taken out as an int..size of the buffer being 4 or 2 as the case may be, also better to use send instead of write for sockets...
|
Another option is to do something like this:
sprintf(buf, "%d\n", k); write(csock[i], buf, strlen(buf)+1); |
Quote:
I have just started learning socket programming at school, our lecturer used write() instead of send() throughout the given examples. I am interested to know about the reasons... :p Thanks :) and of course thanks for showing the way to send variable over sockets :cool: |
Quote:
Thanks :) |
the reason i say send is because send is meant only for sockets and write is mainly for all fd's (incl. sockets), also you are getting a lot of options like out of bound data, etc that means that the other side can read this data as urgent data..etc...MSG_OOB, et al.
this option is the main difference other than the errors returned by the send call when you try a write on a non socket fd.. |
Thanks guys!
Now my server/client chat system is ready! :D |
All times are GMT -5. The time now is 06:48 PM. |