0

I have a client-server application in C programming language that uses TCP connection. Consider just the server side.

The main function is the following:

int main(int argc, char* argv[])
{
    struct addrinfo *ailist, *aip, hint;
    int sockfd, err, n;

    memset(&hint, 0, sizeof(hint)); //set to 0 all bytes

    hint.ai_flags |= AI_PASSIVE;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_addr = NULL;
    hint.ai_next = NULL;
    
    if((err = getaddrinfo(NULL, "60185", &hint, &ailist))!=0)
    {
        printf("Error getaddrinfo %d\n", gai_strerror(err));
        syslog(LOG_ERR, "ruptimed: getaddrinfo error %s", gai_strerror(err));
        exit(1);
    }
    for (aip = ailist; aip!=NULL; aip = aip->ai_next)
    {
        if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN))>=0)
        {
            serveImg(sockfd);
            printf("Exiting \n");
            exit(0);
        }
    }
    exit(1);
}

init_server is the function creating the socket:

int initserver(int type, const struct sockaddr *addr, socklen_t alen, int qlen)
{
    int fd, err;
    int reuse = 1;
    if ((fd = socket(addr->sa_family, type, 0))<0)
    {
        return (-1);
    }
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))<0)
    {
        goto errout;
    }
    if(bind(fd, addr, alen)<0)
    {
        goto errout;
    }
    if (type == SOCK_STREAM || type == SOCK_SEQPACKET)
    {
        if(listen(fd, qlen)<0)
        {
            goto errout;
        }
    }
    return fd;
    errout:
        err = errno;
        close (fd);
        errno = err;
        return(-1);
}

As you can see I have used the SOCK_STREAM socket that is used for TCP connections. The application works.

Now I want to convert the application to use SCTP protocol. For that I tried to use SOCK_SEQPACKET instead of SOCK_STREAM. I tried the program both on OSX and on Ubuntu. On the former the function getaddrinfo returns the error Bad hints while on the latter, the main program exit without starting serving (the condition if ((sockfd = initserver(...))>=0) is never satisfied).

How can I convert my TCP application to a SCTP one?

roschach
  • 8,390
  • 14
  • 74
  • 124

0 Answers0