Note that there are some explanatory texts on larger screens.

plurals
  1. POUnix Domain Socket Code Fails on Embedded Device
    primarykey
    data
    text
    <p>I've added a Unix domain socket to a project I'm working on. The socket has a simple function, it simply broadcasts data that the code extracts from another device, the idea is that other applications will be able to read this data from the socket.</p> <p>I've written a simple server code, and when I run the code on my laptop, using a Ubuntu 10.04 VM, it works perfectly well. However, when I copy the code over onto the embedded device I'm using the code fails, when my application tries to write to the socket the code exits.</p> <p>In <code>/var/log/messages</code> I see the following messages:</p> <pre><code>Dec 2 15:12:17 box local1.info my-app[17338]: Socket Opened Dec 2 15:12:17 box local1.err my-app[17338]: Socket Failed Dec 2 15:12:17 box local1.err my-app[17338]: Protocol wrong type for socket Dec 2 15:12:38 box local1.info ./server[17178]: accept failed: Invalid argument </code></pre> <p>Here is the server code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/un.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/types.h&gt; #include &lt;errno.h&gt; #include&lt;syslog.h&gt; #define SV_SOCK_PATH "/tmp/rtig.sock" //path to be used by socket #define BUF_SIZE 256 //Max length of string listened to #define BACKLOG 5 int main(int argc, char *argv[]){ struct sockaddr_un addr; int sfd, cfd; //File Descriptors for the server and the client ssize_t numRead; //Length of the string read from the client. u_int8_t buf[BUF_SIZE]; //String that reads messages char plain[BUF_SIZE]; //Plain string for writing to the log memset(plain, 0, sizeof plain); //blank out plain string openlog(argv[0], LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); //Write the messages to the syslog //---Declare socket-------------------------------------- sfd = socket(AF_UNIX, SOCK_STREAM, 0); if(sfd!=0){ syslog(LOG_INFO, "socket success"); } else{ syslog(LOG_INFO, "socket unsuccessful"); } //--Test to see if there's already a socket at SV_SOCK_PATH, and remove it if there is. if (remove(SV_SOCK_PATH) == -1 &amp;&amp; errno !=ENOENT){ syslog(LOG_INFO, "error removing socket"); } //----------------------------------------------------------- //--blank out the socket address, then write the information to it memset(&amp;addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path)-1); //ensure path is null terminated //----Bind the socket to the address------------------------------------- if (bind(sfd, (struct sockaddr *) &amp;addr, sizeof(struct sockaddr_un))!=0){ syslog(LOG_INFO, "bind unsuccessful"); } else{ syslog(LOG_INFO, "bind successful"); } //------------------------------------------------------------------------ //-----Listen on the socket----------------------------------------------- if (listen(sfd, BACKLOG) != 0){ syslog(LOG_INFO, "listen failed"); } else{ syslog(LOG_INFO, "listen succeeded"); } //------------------------------------------------------------------------- //--------Accept messages on the socket------------------------------------ socklen_t csize; while(1){ cfd = accept(sfd, (struct sockaddr *)&amp;addr,&amp;csize); if (cfd &lt; 0) { syslog(LOG_INFO, "accept failed: %s", strerror(errno)); } while ( (numRead=read(cfd, buf, BUF_SIZE)) &gt; 0 ){ dump_packet(buf, numRead); } } //------------------------------------------------------------------------- //---code never gets here but this is how to close the log and the socket-- closelog(); close(cfd); } </code></pre> <p>And here's a simple version of the client that connects to this server from my app:</p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/un.h&gt; #define SV_SOCK_PATH "/tmp/rtig.sock" //path to be used by socket #define BACKLOG 5 int isDaemon = 1; void etmlog(int level, char *message) { isDaemon == 1 ? syslog(level, message) : printf(message); } int main(){ struct sockaddr_un addr; unsigned int sockfd; ssize_t numRead; if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) &gt; 0) { etmlog(LOG_INFO, "Socket Opened\n"); } else { etmlog(LOG_ERR, "Socket Failed:\n"); etmlog(LOG_ERR, strerror(errno)); exit(-1); } memset(&amp;addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path) - 1); // -1 ensures null terminated string if (connect (sockfd, (struct sockaddr *)&amp;addr, sizeof(struct sockaddr_un)) == -1) { etmlog(LOG_ERR, "Socket Failed\n"); etmlog(LOG_ERR, strerror(errno)); exit(1); } else { etmlog(LOG_INFO, "Socket Connection Successful\n"); } while (1){ // some data is read into buf up here if (write(sockfd, buf, rdlen) &lt; 0) { etmlog(LOG_ERR, "Write to Socket Failed:"); etmlog(LOG_ERR, strerror(errno)); } } close(sockfd); return 0; } </code></pre> <p>I appreciate that I've just posted a lot of code to read through, but I'd be very grateful if someone could give me a few pointers on this. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload