#pragma module TCPIP$SCTP_SERVER "V5.7-00" /* ** BUILD ENVIRONMENT: ** ** If the current version of DECC does not suport SCTP, then you must ** manually configure the environment as follows: ** ** $ define netinet tcpip$examples ** ** BUILD INSTRUCTIONS: ** ** $ cc tcpip$sctp_server ** $ link tcpip$sctp_server ** ** SYNTAX: ** ** tcpip$sctp_server [port] ** ** EXAMPLE: ** ** Define a foreign symbol to point at the application. ** ** $ sctp_server :== $TCPIP$EXAMPLES:tcpip$sctp_server ** $ sctp_server */ #define _SOCKADDR_LEN #include #include #include #include #ifndef __VMS #include #else #include #endif typedef unsigned int socklen_t; /* usually defined in socket.h */ #include #include #include #include #include /* SCTP */ #ifndef IPPROTO_SCTP #define IPPROTO_SCTP 132 /* SCTP */ #endif #define DEF_PORT 55555 #define SA(x) ((struct sockaddr *)x) int main(int argc, char *argv[]) { int listen_sd, sd, cc, port; struct sockaddr_in local, peer; socklen_t peerlen = sizeof(peer); char peer_name[INET6_ADDRSTRLEN]; char buf[128]; if(argc == 2) port = atoi(argv[1]); else port = DEF_PORT; /* setup the local address that we will bind to and listen on */ local.sin_len = sizeof(local); local.sin_family = AF_INET; local.sin_port = htons(port); local.sin_addr.s_addr = INADDR_ANY; /* create an SCTP socket */ listen_sd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); if(listen_sd == -1) {perror("socket"); return 1;} /* bind to local port and address(es) */ if(bind(listen_sd, SA(&local), local.sin_len) == -1) {perror("bind"); return 1;} /* prepare to accept connections */ if(listen(listen_sd, 5) == -1) {perror("listen"); return 1;} printf("Waiting for connections on SCTP port %d ...\n", port); peerlen = sizeof(peer); if((sd = accept(listen_sd, (struct sockaddr *)&peer, &peerlen)) == -1) {perror("accept"); return 1;} close(listen_sd); /* finished with listen socket */ printf("Accepted connection from: %s:%d\n", inet_ntop(AF_INET, &peer.sin_addr.s_addr, peer_name, INET6_ADDRSTRLEN), ntohs(peer.sin_port)); while(1) { cc = read(sd, &buf, sizeof(buf)); if(cc == -1) /* error on socket */ {perror("read"); break;} if(cc == 0) /* peer disconnect */ {printf("Peer closed connection."); break;} /* data available */ buf[cc] = '\0'; printf("Received: %s\n", buf); if(buf[0] == 'Q') break; } /* read forever */ close(sd); printf("All done\n"); }