Skip to content

Commit ed33603

Browse files
save file
1 parent 526df07 commit ed33603

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
4+
5+
#include <sys/socket.h>
6+
#include <sys/un.h>
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
#include <string.h>
10+
11+
int main() {
12+
13+
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
14+
if(sock<0){
15+
perror("socket");
16+
return 1;
17+
}
18+
19+
struct sockaddr_un addr;
20+
memset(&addr,0,sizeof(addr));
21+
addr.sun_family = AF_UNIX;
22+
strcpy(addr.sun_path,"/tmp/mysock");
23+
// remove old socket file
24+
unlink("/tmp/mysock");
25+
26+
if(bind(sock,(struct sockaddr*)&addr,sizeof(addr))<0){
27+
perror("bind");
28+
return 1;
29+
}
30+
31+
listen(sock,1);
32+
int conn = accept(sock,NULL,NULL);
33+
if(conn<0){
34+
perror("accept");
35+
return 1;
36+
}
37+
// Send a test message (replace with your packet buffer)
38+
const char *msg = "Hello from C over Unix socket!";
39+
write(conn,msg,strlen(msg));
40+
41+
close(conn);
42+
close(sock);
43+
return 0;
44+
45+
}//main
46+
47+

0 commit comments

Comments
 (0)