File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
blog/25-12-03/unix-sockets/ex Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments