LinuxQuestions.org
Review your favorite Linux distribution.
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 09-22-2005, 05:42 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
unix domain socket: how to know if i use recvmsg or read


hiho@ll

question:
if i use accept(); on a unix domain socket (UDS) how do i know if i get a message (using recvmsg(); ) or just data (using read(); )

i know i could do something like a protocol
1. read();
2. if(buffer=="MSG")
2.1 readmsg();
2.2 else read();

but it would be cool if know this before using a read function

this means:
Code:
int client=accept();  // accept on UDS
if(????==???){   // got a message?
 recvmsg();
}
else  // no, just use read
{
 read();
}
i thought about using fcntl but it doesn't seem there is a flag for this
or i use getsockopt
but don't really know which option is the right
 
Old 09-23-2005, 12:26 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Do you have a stream or datagram socket? I've always just used read of stream sockets and recvfrom and datagram sockets. I've not used recvmsg very much, but you can use it to do things like pass file descriptors and the like. In general, I would say that your application should enforce a protocol on socket communications. Realistically, I'd just stick with stream sockets for basic IPC unless you have some reason not to.
 
Old 09-23-2005, 01:15 AM   #3
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Rep: Reputation: 15
Use recv (or read) when your socket is connect()ed, use recvfrom() when your socket is not connected.

This implies that you normally use recv for SOCK_STREAM sockets and recvfrom for unconnected SOCK_DGRAM sockets. Think of it like this: connect connects a socket to some address, which means that recvfrom (or sendto) no longer needs the remote address specified; so you'd use recv/send on connect()ed sockets since you've already told the system what the remote address is.
 
Old 09-23-2005, 02:51 AM   #4
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
hmm
sound's like i have to use my own little protocol
because i want to pass file descriptors around ;-)
that's what i need recvmsg

thx@ll

edit: i just found that i can read messages sent by sendmsg by the simple read(); command!!!!
so it seems that's impossible to know if a message arrives or just simple data for the read(); command

Last edited by Thinking; 09-23-2005 at 09:32 AM.
 
  


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
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 08:28 PM
Unix socket programming - how to sent a file Scrag Programming 9 03-28-2004 10:54 PM
How to set SQL to UNIX socket Skunk_Face Linux - General 1 01-19-2004 07:20 AM
KDevelop Unix Socket Problem shibdas Linux - Software 0 07-30-2003 02:50 PM

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

All times are GMT -5. The time now is 01:53 PM.

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