Robby75
Robby75

Reputation: 3455

how to read/write from/to a SOCK_SEQPACKET socket?

I try to use a SOCK_SEQPACKET socket with this:

int    rc, len;
int    worker_sd, pass_sd;
char   buffer[80];
struct iovec   iov[1];
struct msghdr  msg;

memset(&msg,   0, sizeof(msg));
memset(iov,    0, sizeof(iov));

iov[0].iov_base = buffer;
iov[0].iov_len  = sizeof(buffer);
msg.msg_iov     = iov;
msg.msg_iovlen  = 1;

if((socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
{
    perror("server: socket");
    exit -1;
}
memset(&server_address, 0, sizeof(server_address));
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "/mysocket");

unlink("/mysocket");       
if(bind(socket_fd, (const struct sockaddr *) &server_address, sizeof(server_address)) < 0)
{
    close(socket_fd);
    perror("server: bind error");
    return 1;
}


while(1)
{
    printf("wait for message\n");

    bytes_received = recvmsg(socket_fd, &msg, MSG_WAITALL);


    printf("%d bytes\n", bytes_received);

}

The problem is that the process does not wait but receives -1 from recvmsg and loops forever. Nowhere in the manpages is there any reference what functions shall be used with SOCK_SEQPACKET-style sockets, for example I am not really sure whether recvmsg is even the correct function.

Upvotes: 4

Views: 5081

Answers (2)

jProg2015
jProg2015

Reputation: 1128

recvmsg() returns -1 when an error has occured - errno will be set to the error number.

Read here: http://pubs.opengroup.org/onlinepubs/009695399/functions/recvmsg.html

Upvotes: 1

TheRealChx101
TheRealChx101

Reputation: 1544

SOCK_SEQPACKET is connection-orientated so you must first accept a connection then do your IO on the accepted client socket.

Upvotes: 1

Related Questions