Unix Datagram Socket only works for the first frame

I am trying to IPC using UNIX data graph, but I am having implementation issues. I was able to successfully execute a UNIX stream socket, but the functionality of my program uses grams of data.

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 party will definitely receive the first frame sent. However, the sendto function raises a "No such file or directory" error on this successful send. The next two sends give the same error, but the sendto function returns (-1) instead of the correct buffer length as in the first call. In subsequent calls to sendto, the error becomes "Transcot endpoint not connected".

The get process is more complicated to copy / paste, but essentially it first gets the file descriptor from the socket call, sets the address structures, binds and reads with recvfrom ().

Here is an example of an output log that may or may not be helpful:

Result of the admission process

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

      

Submitting the withdrawal process

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:

  • If SOCK_DGRAM is disabled and I am using sendto (), why is it complaining about transport endpoints?
  • Why does this work for sending the first frame but not others? Why does a successful frame still throw an error?
  • How can I fix this?
+3


source to share


1 answer


You only initialize remote

when out_sock == -1

. The second time you call the function remote

, it will be uninitialized and you will get an error.



+1


source







All Articles