Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-08-2004, 12:41 AM
|
#1
|
Member
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421
Rep:
|
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++) {
write(csock[i],user[j],sizeof(user[j]));
write(csock[i],"\n",1);
}
where user[j] stores the username.
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.
|
|
|
07-08-2004, 06:03 AM
|
#2
|
Member
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251
Rep:
|
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...
|
|
|
07-08-2004, 08:56 AM
|
#3
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Another option is to do something like this:
sprintf(buf, "%d\n", k);
write(csock[i], buf, strlen(buf)+1);
|
|
|
07-08-2004, 09:03 AM
|
#4
|
Member
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421
Original Poster
Rep:
|
Quote:
Originally posted by shishir
also better to use send instead of write for sockets...
|
Could you please elaborate further on this suggestion?
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...
Thanks  and of course thanks for showing the way to send variable over sockets 
|
|
|
07-08-2004, 09:07 AM
|
#5
|
Member
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421
Original Poster
Rep:
|
Quote:
Originally posted by itsme86
sprintf(buf, "%d\n", k);
|
Wow, an excellent idea of changing integer to string!
Thanks 
|
|
|
07-08-2004, 09:30 AM
|
#6
|
Member
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251
Rep:
|
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..
|
|
|
07-08-2004, 09:42 AM
|
#7
|
Member
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421
Original Poster
Rep:
|
Thanks guys!
Now my server/client chat system is ready! 
|
|
|
All times are GMT -5. The time now is 09:49 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|