Unix Network Programming Episode 73

SCTP One-to-Many-Style Streaming Echo Server: ‘main’ Function

#include "unp.h"

int main(int argc, char **argv)
{
    int sock_fd, msg_flags;
    char readbuf[BUFFSZIE];
    struct sockaddr_in servaddr, clientaddr;
    struct sctp_sndrcvinfo sri;
    struct sctp_event_subscribe events;
    int stream_increment=1;
    socklen_t len;
    size_t rd_sz;

    if(argc==2)
    {
        stream_increment=atoi(argv[1]);
    }

    sock_fd=Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=htonl(INVADDR_ANY);
    servaddr.sin_port=htons(SERVA_PORT);

    Bind(sock_fd, (SA *)&servaddr, sizeof(servaddr));

    bzero(&events,sizeof(events));
    events.sctp_data_io_event=1;
    Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events));

    Listen(sock_fd, LISTENQ);
    for(;;)
    {
        len=sizeof(struct sockaddr_in);
        rd_sz=Sctp_recvmsg(sock_fd, readbuf,sizeof(readbuf), (SA *)&clientaddr, &len, &sri, &msg_flags);
        if(stream_increment)
        {
            sri.sinfo_stream++;
            if(sri.sinfo_stream>=sctp_get_no_strms(sock_fd, (SA *)&clientaddr, len))
                sri.sinfo_stream=0;
        }
        Sctp_sendmsg(sock_fd, readbuf, rd_sz, (SA*)&clientadr, len, sri.sinfo_ppid,
            sri.sinfo_flags, sri.sinfo_stream,0,0);
    }
}

SCTP streaming echo server

SCTP One-to-Many-Style Streaming Echo Client: ‘main’ Function

#include "unp.h"

int main(int argc, char **argv)
{
    int sock_fd;
    struct sockaddr_in servaddr;
    struct sctp_event_subscribe events;
    int echo_to_all=0;
    if(argc<2)
        err_quit("Missing host argument - use '%s host [echo] '\n", argv[0]);
    if(argc>2)
    {
        printf("Echoing messages to all streams\n");
    }
    sock_fd=Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(SERV_PORT);
    Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
    bzero(&events, sizeof(events));
    Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events));
    if(echo_to_all==0)
        sctpstr_client(stdin,sock_fd, (SA *)&servaddr, sizeof(servaddr));
    else
        sctpstr_client_echoall(stdin, sock_fd, (SA *)&servaddr, sizeof(servaddr));

    Close(socke_fd);
    return 0;
}

SCTP streaming echo client main

SCTP Streaming Echo Client: ‘str_cli’ Function

#include "unp.h"

void sctpstr_client(FILE *fp, int sock_fd, struct sockaddr *to, socklen_t tolen)
{
    struct sockaddr_int peeraddr;
    struct sctp_sndrcvinfo sri;
    char sendline[MAXLINE], recvlien[MAXLINE];
    socklen_t len;
    int out_sz, rd_sz;
    int msg_flags;

    bzero(&sri,sizeof(sri));
    while(fgets(sendline, MAXLINE, fp)!=NULL)
    {
        if(sendline[0]!=' [')
        {
            printf("Error, line must be of the form '[streamnum] text'\n");
            continue;
        }
        sri.sinfo_stream=strtol(&sendline[1],NULL,0);
        out_sz=strlen(sendline);
        Sctp_sendmsg(sock_fd, sendline, out_sz, to, tolen, 0, 0, sri.sinfo_stream,0,0);
        len=sizeof(peeraddr);
        rd_sz=Sctp_recvmsg(sock_fd, recvline,sizeof(recvline), (SA *)&peeraddr, &len, sri, &msg_flags);
        printf("From str:%s seq:%d (assoc: 0x%x):"sri.sinfo_stream,sri.sinfo_ssn,(u_int)sri.sinfo_assoc_id);
        printf("%.*s",rd_sz, recvline);
    }
}

SCTP sctp_strcli function

你可能感兴趣的:(Unix,Network,Programming,unix,服务器)