Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)
I need to create a serial port socket for the kgdb-gdb remote connection.

mkfifo creates a FIFO on my system, how can I create the socket files?

1 Answer

0 votes
by (36.8k points)
edited by

The link in the accepted answer by @cidermonkey is great if you're trying to write an app that uses sockets. If you want to create one you can do it in python:

~]# python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/somesocket')"

~]# ll /tmp/somesocket 

srwxr-xr-x. 1 root root 0 Mar  3 19:30 /tmp/somesocket

Or you can use the tiny C program, for e.g., save the following to create the socket.c:

#include <fcntl.h>

#include <sys/un.h>

#include <sys/socket.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

int main(int argc, char **argv)

{

    // The following line expects the socket path to be first argument

    char * mysocketpath = argv[1];

    // Alternatively, you could comment that and set it statically:

    //char * mysocketpath = "/tmp/mysock";

    struct sockaddr_un namesock;

    int fd;

    namesock.sun_family = AF_UNIX;

    strncpy(namesock.sun_path, (char *)mysocketpath, sizeof(namesock.sun_path));

    fd = socket(AF_UNIX, SOCK_DGRAM, 0);

    bind(fd, (struct sockaddr *) &namesock, sizeof(struct sockaddr_un));

    close(fd);

    return 0;

}

Then install gcc, compile it, and ta-da:

~]# gcc -o create-a-socket create-a-socket.c

~]# ./create-a-socket mysock

~]# ll mysock

srwxr-xr-x. 1 root root 0 Mar  3 17:45 mysock

To know about Linux join the Linux training

Do check out the video below

Related questions

0 votes
1 answer
asked Dec 17, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...