Presentation is loading. Please wait.

Presentation is loading. Please wait.

SCTP Client / Server example

Similar presentations


Presentation on theme: "SCTP Client / Server example"— Presentation transcript:

1 SCTP Client / Server example
Chapter 10 SCTP Client / Server example

2 Simple echo server using SCTP protocol
Send line of text from client to server Server sends the same line back to the client Iterative server

3 Client Client reads a single line of text from standard input Text is send in [#]text format. # represents SCTP stream number where text is sent Server Reads the text from the network Increases the SCTP stream number by one (# in [#]text) Sends the message back to the client on this new stream Client Reads the reply message and prints it to standard output Stream number, sequence number and text string is printed

4 SCTP One-to-Many-Style Streaming Echo Server: main Function
SCTP streaming echo server

5 Server: Stream increment
Stream increment option If command line option is passed to program, program decides whether to increment stream number of incoming messages if (argc == 2) stream_increment = atoi(argv[1]); Server: Create an SCTP socket sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

6 Server: Bind address Bind any incoming address to certain server port.
bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(sock_fd, (SA *) &servaddr, sizeof(servaddr)); Server: Create an SCTP socket

7 Server: Set up for notification
Server changes its notification subscription for the one-to-many SCTP socket. This allows server to see the stream number where the message arrived. bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts));

8 Server: Incoming associations
Server enables incoming associations so it can listen the server socket for incoming messages. Listen(sock_fd, LISTENQ); After which server will enter the main loop Server: Wait for messages Initialize size of client socket address structure. Block until message arrives. len = sizeof(struct sockaddr_in); rd_sz = Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf), (SA *)&cliaddr, &len, &sri,&msg_flags);

9 Server: Increment stream number
Check increment flag and increase stream stream number is flag is set. If number is too large then number is set to 0. if(stream_increment) { sri.sinfo_stream++; if(sri.sinfo_stream >=sctp_get_no_strms(sock_fd, (SA *)&cliaddr,len)) sri.sinfo_stream = 0; }

10 Server: Send message back
Sending back the message to the client Sctp_sendmsg(sock_fd, readbuf, rd_sz, (SA *)&cliaddr, len, sri.sinfo_ppid, sri.sinfo_flags, sri.sinfo_stream, 0, 0); Server: Finish? Program runs forever until it is shutdown with an external signal. for ( ; ; ) { }

11 SCTP One-to-Many-Style Streaming Echo Client: main Function
SCTP streaming echo client main

12 Client: Validate arguments
Arguments: host and echo flag. 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"); echo_to_all = 1; } Client: Create a socket Create an SCTP socket sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

13 Client: Set up server address
Use given server address and well known port number to construct server address structure using Inet_pton(). 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);

14 Client: Set up for notifications
Explicitly setting notification subscription for one-to-many socket. bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd,IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts));

15 Client: Echo processing
If flag is not set then sctpstrcli() is called. Else sctpstr_cli_echoall() is called. if(echo_to_all == 0) sctpstr_cli(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr)); else sctpstr_cli_echoall(stdin,sock_fd,(SA*)&servaddr,sizeof(servaddr)); These functions are shown later. Client: Finish: When all is done, client closes socket and returns zero. Close(sock_fd); return(0);

16 10.4 SCTP Streaming Echo Client: str_cli Function

17 Client: Initialize Initialize structure and enter loop.
Block until user inputs line of text. bzero(&sri,sizeof(sri)); while (fgets(sendline, MAXLINE, fp) != NULL) { Client: Validate input Validate given input from the user. if(sendline[0] != '[') { printf("Error, line must be of the form '[streamnum]text'\n"); continue; }

18 Client: Stream number Stream number is translated from the user input and put into the structure. sri.sinfo_stream = strtol(&sendline[1],NULL,0); Client: Send message Message is sent. out_sz = strlen(sendline); Sctp_sendmsg(sock_fd, sendline, out_sz, to, tolen, 0, 0, sri.sinfo_stream, 0, 0);

19 Client: Send mesage Client: Display message
Client: Wait for responseProgram blocks until message is received back from the server. len = sizeof(peeraddr); rd_sz = Sctp_recvmsg(sock_fd, recvline, sizeof(recvline), (SA *)&peeraddr, &len, sri,& msg_flags); Client: Display message Received message is displayed to standard output. printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn, (u_int)sri.sinfo_assoc_id); printf("%.*s",rd_sz,recvline);

20 Client: Finish? Program reads lines from user until it is terminated by an external signal. Head-of-Line blocking Head-of-Line blocking occurs when TCP segment is lost and latter segments arrive out of order. All the subsequent segments are held until the lost segment is retransmitted. This ensures data arrives to the application in the order it was sent.

21 10.5 Exploring Head-of-Line Blocking
Sending three pictures over one TCP connection Sending three pictures over three SCTP streams

22 Exploring Head-of-Line Blocking

23 SCTP blocking of streams
SCTP allows multiple streams over single connection. Each stream can be transmitted independent of other streams. Blocking of one stream for retransmission does not block other streams. Client: Initialize and read input Initialize data structures. Enter loop and read user input. bzero(sendline,sizeof(sendline)); bzero(&sri,sizeof(sri)); while (fgets(sendline, SCTP_MAXLINE - 9, fp) != NULL) {

24 Client: Process user input
Process given user input. strsz = strlen(sendline); if(sendline[strsz-1] == '\n') { sendline[strsz-1] = '\0'; strsz--; } Send message to each stream. Append .msg. and stream number to each message for visual help of tracking messages. for(i=0;i<SERV_MAX_SCTP_STRM;i++) { snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d", i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0);

25 Client: Read, display and loop
Block until incoming data comes from streams. Display message and move to read next. for(i=0;i<SERV_MAX_SCTP_STRM;i++) { len = sizeof(peeraddr); rd_sz = Sctp_recvmsg(sock_fd, recvline, sizeof(recvline), (SA *)&peeraddr, &len, &sri,&msg_flags); printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn, (u_int)sri.sinfo_assoc_id); printf("%.*s\n",rd_sz,recvline); } After last message is read, loop back to user input.

26 Client: Modifications
Send two messages to each stream to see messages are being held for reordering. for(i=0;i<SERV_MAX_SCTP_STRM;i++) { snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d 1", i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0); snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d 2", i); } for(i=0;i<SERV_MAX_SCTP_STRM*2;i++) { len = sizeof(peeraddr);

27 Number of streams Termination:
FreeBSD KAME implementation: default 10 streams at start. Change must be made on the socket before an association is made. if (argc == 2) stream_increment = atoi(argv[1]); sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); bzero(&initm,sizeof(initm)); initm.sinit_num_ostreams = SERV_MORE_STRMS_SCTP; Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_INITMSG, &initm, sizeof(initm)); Termination: Two possible methods for closing one-to-many association: Close after sending message (eof) Close immediately (abort)

28 Termination: eof Server wishes to shutdown an association after sending message Set flag MSG_EOF in the reply message structure. Sctp_sendmsg(sock_fd, readbuf, rd_sz, (SA *)&cliaddr, len, sri.sinfo_ppid, (sri.sinfo_flags | MSG_EOF), sri.sinfo_stream, 0, 0); }

29 Termination: abort Server wishes to shutdown an association immediately. Set flag MSG_ABORT in the reply message structure. Forces immediate termination with an ABORT chunk. Any data not transmitted will be discarded. strcpy(byemsg,"goodbye"); Sctp_sendmsg(sock_fd, byemsg, strlen(byemsg), (SA *)&servaddr, sizeof(servaddr), 0, MSG_ABORT, 0, 0, 0); Close(sock_fd); Even if association has been aborted, socket descriptor still needs to be freed with Close().

30 Summary

31 References – Books Rudoff, Addison-Wesley, 2004, ISBN 0-13-141155-1
UNIX Network Programming; The Sockets Networking API, Vol. 1, 3rd ed, W. Richard Stevens, Bill Fenner, Andrew M. Rudoff, Addison-Wesley, 2004, ISBN chapter 9: Elementary SCTP Sockets chapter 10: SCTP Client/Server Example chapter 23: Advanced SCTP Sockets


Download ppt "SCTP Client / Server example"

Similar presentations


Ads by Google