LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-08-2004, 12:41 AM   #1
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Rep: Reputation: 31
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.
 
Old 07-08-2004, 06:03 AM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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...
 
Old 07-08-2004, 08:56 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Another option is to do something like this:

sprintf(buf, "%d\n", k);
write(csock[i], buf, strlen(buf)+1);
 
Old 07-08-2004, 09:03 AM   #4
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Original Poster
Rep: Reputation: 31
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
 
Old 07-08-2004, 09:07 AM   #5
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by itsme86
sprintf(buf, "%d\n", k);
Wow, an excellent idea of changing integer to string!
Thanks
 
Old 07-08-2004, 09:30 AM   #6
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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..
 
Old 07-08-2004, 09:42 AM   #7
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Original Poster
Rep: Reputation: 31
Thanks guys!
Now my server/client chat system is ready!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getpeername: Socket operation on non-socket KevStA Linux - General 6 08-25-2020 12:40 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
connect: socket operation on non-socket bit7 Linux - Networking 0 02-18-2003 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration