unix – 如何创建socket类型的特殊文件?

前端之家收集整理的这篇文章主要介绍了unix – 如何创建socket类型的特殊文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要为kgdb-gdb远程连接创建串口套接字。

正如mkfifo在您的系统上创建FIFO一样,我们如何创建套接文件

@cidermonkey接受的答案中的链接非常好,如果您正在尝试编写使用套接字的应用程序。如果你真的只想创建一个,你可以在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

或者with a tiny C program,例如,将以下内容保存到create-a-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;
}

然后安装gcc,编译它,然后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
原文链接:https://www.f2er.com/bash/387259.html

猜你在找的Bash相关文章