unix - 如何创建套接字类型的特殊文件?

标签 unix gdb

我需要为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

关于unix - 如何创建套接字类型的特殊文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6025755/

相关文章:

linux - Unix消息队列

c++ - 使用线程并行化 ssh 调用的 malloc 问题

ios - 如何使用 Xcode 调试器打印出属性的内容?

c++ - 从核心转储中识别 auto_ptr 背后对象的具体类型

c++ - gdb中的inf是什么

debugging - 尝试使用 gdb(和 cgdb)在 go(golang)中调试程序,但似乎存在问题

python - Enthought Python、Sage 或其他(在 Unix 集群中)

unix - hadoop与mrjob管道在壳上

linux - 在 24 小时内复制生成的 Unix 文件

c - 使用独立于 Unix 发行版的数据库/索引顺序文件