{"id":148,"date":"2009-11-16T17:58:31","date_gmt":"2009-11-16T17:58:31","guid":{"rendered":"http:\/\/kumouse.aafox.com\/?p=148"},"modified":"2009-11-16T17:58:31","modified_gmt":"2009-11-16T17:58:31","slug":"unix-%e5%9f%9f-%e5%a5%97%e6%8e%a5%e5%ad%97-%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=148","title":{"rendered":"unix \u57df \u5957\u63a5\u5b57 \u793a\u4f8b"},"content":{"rendered":"<p>\u8fd9\u4e2a\u4e0eAF_INET\u4e0d\u540c\u5730\u65b9\u5728\u4e8e\uff0c\u5ba2\u6237\u7aef\u4e5f\u8981\u7528bind\u7ed1\u5b9a\u4e00\u4e0b\uff0c\u4e0d\u8981\u9760connect\u5206\u914d\uff0c\u4e0d\u7136\u670d\u52a1\u5668\u662f\u65e0\u6cd5\u77e5\u9053\u5ba2\u6237\u7528\u7684\u662f\u54ea\u4e2a\u5957\u63a5\u5b57\u6587\u4ef6<\/p>\n<p>\u670d\u52a1\u5668\u7aef<\/p>\n<blockquote><p>[ssj@main test]$ cat AFunixserver.c<br \/>#include &lt;stdio.h&gt;<br \/>#include &lt;stdlib.h&gt;<br \/>#include &lt;stddef.h&gt;<br \/>#include &lt;sys\/socket.h&gt;<br \/>#include &lt;sys\/un.h&gt;<br \/>#include &lt;string.h&gt;<br \/>#include &lt;time.h&gt;<br \/>#include &lt;sys\/stat.h&gt;<\/p>\n<p>#define PATH &quot;\/home\/ssj\/source\/test\/connect.socket&quot;<br \/>#define STALE 30<br \/>#define MAX 1024<\/p>\n<p>void my_turn(char *p)<br \/>{<br \/>if (p == NULL)return;<br \/>while( *p != &#39;\u0000&#39;)<br \/>{<br \/>if( *p &gt;= (char)0x61 &#038;&#038; *p &lt;= (char)0x7a)<br \/>*p=(char)((int)*p-(int)0x20);<br \/>p++;<br \/>}<br \/>}<\/p>\n<p>int init(int *lfd,char *path)<br \/>{<br \/>int fd,len;<br \/>struct sockaddr_un un;<\/p>\n<p>if((fd=socket(AF_UNIX,SOCK_STREAM,0))==-1)<br \/>  {perror(&quot;Fail to socket&quot;);return -1;}<\/p>\n<p>unlink(path);<br \/>memset(&#038;un,0,sizeof(un));<br \/>un.sun_family=AF_UNIX;<br \/>strcpy(un.sun_path,path);<\/p>\n<p>len=offsetof(struct sockaddr_un,sun_path)+strlen(path);<br \/>if(bind(fd,(struct sockaddr *)&#038;un,len)==-1)<br \/>  {perror(&quot;Fail to bind&quot;);goto err;}<\/p>\n<p>if(listen(fd,10)==-1){perror(&quot;Fail to listen&quot;);goto err;}<\/p>\n<p>*lfd=fd;<br \/>return 0;<\/p>\n<p>err:<br \/>close(fd);return -1;<br \/>}<\/p>\n<p>int main(void)<br \/>{<br \/>int lfd,cfd,len,n;<br \/>time_t staletime;<br \/>struct sockaddr_un un;<br \/>struct stat statbuf;<br \/>char buf[MAX];<\/p>\n<p>if(init(&#038;lfd,PATH)==-1)<br \/>  exit(1);<br \/>printf(&quot;running&#8230;&#8230;n&quot;);<\/p>\n<p>while(1)<br \/>{<br \/>len=sizeof(struct sockaddr_un);<br \/>if((cfd=accept(lfd,(struct sockaddr *)&#038;un,&#038;len))==-1)<br \/>  {perror(&quot;Fail to accept&quot;);exit(1);}<\/p>\n<p>len-=offsetof(struct sockaddr_un,sun_path);<br \/>un.sun_path[len]=&#39;\u0000&#39;;<\/p>\n<p>if(stat(un.sun_path,&#038;statbuf)==-1){perror(&quot;Fail to get status&quot;);exit(1);}<br \/>if((statbuf.st_mode&#038;(S_IRWXG|S_IRWXO))||(statbuf.st_mode&#038;S_IRWXU)!=S_IRWXU)<br \/>  {printf(&quot;wrong permissionsn&quot;);exit(1);}<\/p>\n<p>staletime=time(NULL)-STALE;<br \/>if(statbuf.st_atime&lt;staletime||statbuf.st_ctime&lt;staletime||statbuf.st_mtime&lt;staletime){printf(&quot;client is too oldn&quot;);close(cfd);break;}<\/p>\n<p>if(unlink(un.sun_path)==-1){perror(&quot;Fail to unlink&quot;);exit(1);}<\/p>\n<p>n=read(cfd,buf,MAX);<br \/>if (n==-1)<br \/>  {<br \/>  perror(&quot;Fail to read&quot;);<br \/>  exit(1);<br \/>  }<br \/>else if(n==0)<br \/>  {<br \/>   printf(&quot;the connect closedn&quot;);<br \/>   close(cfd);<br \/>   continue;<br \/>  }<\/p>\n<p>if(strcmp(buf,&quot;Nothing&quot;)==0)<br \/>goto close;<\/p>\n<p>my_turn(buf);<br \/>if(write(cfd,buf,n)==-1){perror(&quot;Fail to write&quot;);exit(1);}<br \/>close(cfd);<\/p>\n<p>}<\/p>\n<p>if(unlink(PATH)==-1){perror(&quot;Fail to unlink&quot;);exit(1);}<\/p>\n<p>close(lfd);<br \/>return 0;<\/p>\n<p>close:<br \/>write(cfd,&quot;Server Close&quot;,13);<br \/>close(cfd);<br \/>close(lfd);<br \/>if(unlink(PATH)==-1){perror(&quot;Fail to unlink&quot;);exit(1);}<br \/>return -1;<br \/>}<\/p><\/blockquote>\n<p>\u5ba2\u6237\u7aef<\/p>\n<blockquote><p>[ssj@main test]$ cat AFunixclient.c<br \/>#include &lt;stdio.h&gt;<br \/>#include &lt;stddef.h&gt;<br \/>#include &lt;sys\/stat.h&gt;<br \/>#include &lt;sys\/socket.h&gt;<br \/>#include &lt;sys\/un.h&gt;<br \/>#include &lt;stdlib.h&gt;<\/p>\n<p>#define PATH &quot;\/home\/ssj\/source\/test\/connect.socket&quot;<br \/>#define C_PATH &quot;\/home\/ssj\/source\/test\/&quot;<br \/>#define MAX 1024<\/p>\n<p>int main(int argc,char *argv[])<br \/>{<br \/>int cfd,len;<br \/>struct sockaddr_un un;<br \/>char buf[MAX],*str=&quot;Nothing&quot;;<br \/>if(argc&gt;1)<br \/>str=argv[1];<\/p>\n<p>if((cfd=socket(AF_UNIX,SOCK_STREAM,0))==-1)<br \/>  {perror(&quot;Fail to socket&quot;);exit(1);}<\/p>\n<p>memset(&#038;un,0,sizeof(struct sockaddr_un));<br \/>un.sun_family=AF_UNIX;<br \/>sprintf(un.sun_path,&quot;%s%d&quot;,C_PATH,getpid());<br \/>len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path);<\/p>\n<p>unlink(un.sun_path);<\/p>\n<p>if(bind(cfd,(struct sockaddr *)&#038;un,len)==-1)<br \/>  {perror(&quot;Fail to bind&quot;);exit(1);}<\/p>\n<p>if(chmod(un.sun_path,S_IRWXU)&lt;0){perror(&quot;Fail to chmod&quot;);exit(1);}<\/p>\n<p>memset(&#038;un,0,sizeof(struct sockaddr_un));<br \/>un.sun_family=AF_UNIX;<br \/>strcpy(un.sun_path,PATH);<\/p>\n<p>len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path);<\/p>\n<p>if(connect(cfd,(struct sockaddr *)&#038;un,len)&lt;0)<br \/>  {perror(&quot;Fail to connect&quot;);exit(1);}<\/p>\n<p>if(write(cfd,str,len)==-1){perror(&quot;Fail to write&quot;);exit(1);}<\/p>\n<p>if(read(cfd,buf,MAX)==-1){perror(&quot;Fail to read&quot;);exit(1);}<\/p>\n<p>printf(&quot;recive from server:%sn&quot;,buf);<br \/>close(cfd);<br \/>return 0;<br \/>}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u4e0eAF_INET\u4e0d\u540c\u5730\u65b9\u5728\u4e8e\uff0c\u5ba2\u6237\u7aef\u4e5f\u8981\u7528bind\u7ed1\u5b9a\u4e00\u4e0b\uff0c\u4e0d\u8981\u9760connect\u5206\u914d\uff0c\u4e0d\u7136\u670d\u52a1\u5668\u662f\u65e0\u6cd5\u77e5\u9053 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-linux-c"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":0,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}