File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
blog/25-12-03/unix-sockets/ex Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+
2+
3+ // ipc-client-linux.c
4+
5+
6+ #include <sys/socket.h>
7+ #include <sys/un.h>
8+ #include <stdio.h>
9+ #include <unistd.h>
10+ #include <string.h>
11+
12+
13+ int main () {
14+
15+ int sock = socket (AF_UNIX , SOCK_STREAM , 0 );
16+ if (sock < 0 ){
17+ perror ("socket" );
18+ return 1 ;
19+ }
20+
21+ struct sockaddr_un addr ;
22+ memset (& addr ,0 ,sizeof (addr ));
23+ addr .sun_family = AF_UNIX ;
24+ strcpy (addr .sun_path ,"/tmp/mysock" );
25+
26+ if (connect (sock ,(struct sockaddr * )& addr ,sizeof (addr ))< 0 ){
27+ perror ("connect" );
28+ close (sock );
29+ return 1 ;
30+ }
31+
32+ // Read message from server
33+ char buf [256 ];
34+ int n = read (sock ,buf ,sizeof (buf )- 1 );
35+ if (n > 0 ){
36+ buf [n ] = '\0' ;
37+ printf ("Client received: %s\n" ,buf );
38+ }
39+
40+ // Send reply to server
41+ const char * reply = "Hello back from client!" ;
42+ write (sock ,reply ,strlen (reply ));
43+
44+ close (sock );
45+ return 0 ;
46+
47+ }//main
48+
49+
You can’t perform that action at this time.
0 commit comments