The Socket API, Part 5: SCTP
2013-12-10 18:33
274 查看
转:http://www.linuxforu.com/2011/12/socket-api-part-5-sctp/
By Pankaj Tanwar on December 29, 2011 in Coding, Developers · 11 Comments
This article on socket programming deals with the Stream Control Transmission Protocol (SCTP).
Similar to TCP and UDP, SCTP provides some features of both. It is message-oriented, and provides a reliable sequenced delivery of messages. SCTP supports multi-homing, i.e., multiple IPs on both sides of the connection. So it is called an association instead of a connection, as a connection involves communication between two IPs, while an association refers to communication between two systems that may have multiple IPs.
SCTP can provide multiple streams between connection endpoints, and each stream will have its own reliable sequenced delivery of messages, so any lost message will not block the delivery of messages in any of the other streams. SCTP is not vulnerable to SYN flooding, as it requires a 4-way handshake.
We will discuss the science later, and now jump to the code, as we usually do. But first you need to know the types of SCTP sockets:
A one-to-one socket that corresponds to exactly one SCTP association (similar to TCP).
A one-to-many socket, where many SCTP associations can be active on a socket simultaneously (similar to UDP receiving datagrams from several endpoints).
One-to-one sockets (also called TCP-style sockets) were developed to ease porting existing TCP applications to SCTP, so the difference between a server implemented using TCP and SCTP is not much. We just need to modify the call to
Now let’s jump to the one-to-many or UDP-style socket, and write a server using multiple streams that the client follows.
First, here is the code for the server,
And here’s the code of the client,
The server is sending three messages on three different streams, and the client is just receiving the messages and printing them on the screen (see Figures 1 and 2).
Figure 1: Server output
Figure 2: Client output
The code is similar to the TCP client, as we are again making calls to the same functions [refer toParts 1, 2, 3 and 4 of this series.]. The difference is that we are creating an iterative server, similar to the one for UDP, but we have an
We are using this function to send a message from a socket while using the advanced features of SCTP. The first argument to the function is
The
Next is the
This function does the reverse of the
This function is used to set the options for the socket
Here, the first two lines tell us that the output and input streams available are three, and the maximum attempts will be two. The same options are set in the client program. Other options are set for the events. This structure will be filled when an event like “message received” occurs, and our program is notified. Its counterpart function is
Now compile and run the program; make sure you have installed
I’m wrapping up the series for now, but will keep adding more to the topic, on and off.
By Pankaj Tanwar on December 29, 2011 in Coding, Developers · 11 Comments
This article on socket programming deals with the Stream Control Transmission Protocol (SCTP).
Similar to TCP and UDP, SCTP provides some features of both. It is message-oriented, and provides a reliable sequenced delivery of messages. SCTP supports multi-homing, i.e., multiple IPs on both sides of the connection. So it is called an association instead of a connection, as a connection involves communication between two IPs, while an association refers to communication between two systems that may have multiple IPs.
SCTP can provide multiple streams between connection endpoints, and each stream will have its own reliable sequenced delivery of messages, so any lost message will not block the delivery of messages in any of the other streams. SCTP is not vulnerable to SYN flooding, as it requires a 4-way handshake.
We will discuss the science later, and now jump to the code, as we usually do. But first you need to know the types of SCTP sockets:
A one-to-one socket that corresponds to exactly one SCTP association (similar to TCP).
A one-to-many socket, where many SCTP associations can be active on a socket simultaneously (similar to UDP receiving datagrams from several endpoints).
One-to-one sockets (also called TCP-style sockets) were developed to ease porting existing TCP applications to SCTP, so the difference between a server implemented using TCP and SCTP is not much. We just need to modify the call to
socket()to
socket(AF_INET, SOCK_STREAM, and IPPROTO_SCTP)while everything else stays the same — the calls to
listen(),
accept()for the server, and
connect()for the client, with
read()and
write()calls for both.
Now let’s jump to the one-to-many or UDP-style socket, and write a server using multiple streams that the client follows.
First, here is the code for the server,
smtpserver.c:
sctpclient.c:
Figure 1: Server output
Figure 2: Client output
The code is similar to the TCP client, as we are again making calls to the same functions [refer toParts 1, 2, 3 and 4 of this series.]. The difference is that we are creating an iterative server, similar to the one for UDP, but we have an
accept()call here. The client does the reverse, and receives the messages coming from the server to the client. Now let’s try to understand the functions that we used:
sd, the socket descriptor, from which the message
msgof length
lenis sent. The fourth argument is to give the destination address —
tolenspecifies the length of the destination address, while
stream_noidentifies the stream number to send this message to. The
flagsparameter is used to send some options to the receiver. You can check out the manual pages for
sctp_sendmsg().
The
timetoliveparameter is time in milliseconds after which the message will expire if not sent by then; the zero here indicates that no time-out is set. The context is the value passed to the upper layer along with the undelivered message, if an error occurs while sending the message. When successful, it will return the number of bytes sent, or -1 on error.
Next is the
stcp_recvmsg()function:
sctp_sendmsgfunction and is used to receive a message. The parameters are similar. The socket
sdreceives the
msgof length
lenfrom the address
*fromwith a length
*fromlen, and
*sinfois a pointer to the address, which will be filled upon receipt of the message.
mag_flagsis a pointer to an integer with flags like
MSG_NOTIFICATIONor
MSG_EOR. It returns the number of bytes received, or -1 on error.
sockfd. The next argument is the level at which the option resides. To manipulate options at the sockets API level, the level is specified as
SOL_SOCKET.
optnameand any specified options are passed uninterpreted to the appropriate protocol module for interpretation. The
leveland
optnameare defined in
sys/sockets.h. The arguments
optvaland
optlenare used to access option values for
setsockopt()that are stored in the structure. The options we set in the server are:
getsockopts()(look up the man pages for help). The rest of the code is simple to understand.
Now compile and run the program; make sure you have installed
sctp-toolsso that you’ll have
sctp.hat
netinet/. To compile, use
gcc sctpserver.c -lsctp -o server && gcc sctpclient.c -lsctp -o client; and to run, use the following code:
相关文章推荐
- Creating Your Own Server: The Socket API, Part 1
- Creating Your Own Server: The Socket API, Part 2
- The Socket API, Part 3: Concurrent Servers
- The Socket API, Part 4: Datagrams
- The WebSocket API
- iOS-Core Bluetooth Overview(API Reference) of the second part
- iOS-Core Bluetooth Background Processing for iOS Apps(API Reference) the fifth part
- iOS-Performing Common Central Role Tasks(API Refrence) of the third part
- An Introduction to the Java Logging API (part)
- iOS-Performing Common Peripheral Role Tasks(API Reference)the fourth part
- iOS-Best Practices for Interacting with a Remote Peripheral Device(API Reference) the sixth part
- iOS-Best Practices for Setting Up Your Local Device as a Peripheral(API Reference) the seventh part
- Opening Doors: Notes On the Delphi ToolsAPI by its Creator - Part 2 - by Allen Bauer
- The API Gateway Pattern: Angular JS and Spring Security Part IV
- LINUX SOCKET PART 2: THE SERVER SIDE ISSUES
- Getting Started With the Java Rule Engine API (JSR 94): Toward Rule-Based Applications
- 20170304#cs231n#6.Neural Networks Part 2: Setting up the Data and the Loss
- 1. [SDL-API]Initializing the Library
- Building the Build System – Part 1 – Abandoning the Build Panel