Skip to content

Commit 98b4a16

Browse files
save file
1 parent 9697e19 commit 98b4a16

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+

0 commit comments

Comments
 (0)