简体   繁体   English

Unix数据报套接字仅适用于第一帧

[英]Unix Datagram Socket works for first frame only

I'm attempting to do IPC with a UNIX data-gram socket but I'm having issues with the implementation. 我正在尝试使用UNIX数据克套接字进行IPC,但我遇到了实现问题。 I was able to successfully do a UNIX stream socket but the functionality of my program mandates the use of data-grams. 我能够成功地执行UNIX流套接字,但我的程序的功能要求使用数据克。

Here is the code for the sending side: 以下是发送方的代码:

struct sockaddr_un remote;
struct sockaddr_un local;
socklen_t size_remote;
socklen_t size_local;
if(out_sock == -1)
{
    if ((out_sock = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    {
        perror("toYYYYY socket");
        exit(1);
    }

    char remote_path[28] = "/tmp/sockets/fromXXXXXXX0000";
    sprintf(remote_path + 24, "%d", portOffset + XXXXXX_CTRL_UDP_PORT);
    memset(&remote, 0, sizeof(struct sockaddr_un));
    remote.sun_family = AF_UNIX;
    strcpy(remote.sun_path, remote_path);
    size_remote = (offsetof(struct sockaddr_un, sun_path) + strlen(remote_path));

    char local_path[28] = "/tmp/sockets/toYYYYY0000";
    sprintf(local_path + 20, "%d", portOffset + XXXXXX_CTRL_UDP_PORT);
    memset(&local, 0, sizeof(struct sockaddr_un));
    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, local_path);
    size_local = (offsetof(struct sockaddr_un, sun_path) + strlen(local_path));

    unlink(local_path);
    int rtv = bind(out_sock, (struct sockaddr *)&local, size_local);
    if(rtv)
            perror("toYYYYY bind");

}
written = sendto(out_sock, packet, bytes, 0, (struct sockaddr *)&remote, size_remote);

if (written != bytes)
{
    status = AS_FAILURE;
    perror("toYYYYY send");
}

return status;

The receiving side will without fail get the first frame sent. 接收方将毫无疑问地获得第一帧发送。 However, the sendto function throws the "No such file or directory" error on this successful send. 但是,sendto函数会在此成功发送时抛出“No such file or directory”错误。 The following two sends produce the same error, but the sendto function returns (-1) instead of the correct buffer length like in the first call. 以下两个发送产生相同的错误,但sendto函数返回(-1)而不是像第一次调用中那样正确的缓冲区长度。 In the subsequent calls to sendto the error becomes "Transort endpoint is not connected". 在随后的sendto调用中,错误变为“未连接Transort端点”。

The receiving process is more complicated to copy/paste but essentially it first gets the file descriptor from the socket call, sets address structures, binds, and reads with recvfrom(). 接收过程复制/粘贴更复杂,但实际上它首先从套接字调用获取文件描述符,设置地址结构,绑定和使用recvfrom()读取。

Here is a sample output log that may or may not be helpful: 以下是一个示例输出日志,可能有用也可能没用:

Receiving Process Output 接收过程输出

remote_len: 30
local_len: 26
local: '/tmp/sockets/toYYYYY5248' remote: '/tmp/sockets/fromXXXXXXX5248'
Binding to: '/tmp/sockets/fromXXXXXXX5248'
entered selected (This is receiving function with recvfrom())
recv from '/tmp/sockets/toYYYYY5248' len: 263
Closing socket

Sending Process Output 发送流程输出

send (successful call): Success
send: No such file or directory
send: No such file or directory
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected
send: Transport endpoint is not connected

My questions are: 我的问题是:

  1. If SOCK_DGRAM is connectionless and I'm using sendto(), why is it complaining about the transport endpoints? 如果SOCK_DGRAM是无连接的并且我正在使用sendto(),为什么它抱怨传输端点?
  2. Why does this work for the first frame to be sent, but not others? 为什么这适用于要发送的第一帧,而不是其他帧? Why does the successful frame still throw an error? 为什么成功的框架仍然会出错?
  3. How can I fix this? 我怎样才能解决这个问题?

You only initialize remote when out_sock == -1 . 您只在out_sock == -1时初始化remote The second time you call the function remote will be uninitialized and so you get an error. 第二次调用remote函数将是未初始化的,因此您会收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM