Multiple Associations with Stream Control Transmission Protocol

 in
The elegant way SCTP handles multiple streams makes it ideal for things like chat clients.
Lazy Person's Chat

In this section, I discuss a simple example of how to build a simple chat server using SCTP. This isn't meant to be a competitor to the many chat systems around, rather it is to show some of the features of SCTP.

A chat server must listen for messages coming from a probably transient group of clients. When a message is received from any one client, it should send the message back out to all of the other clients.

UDP could be a choice here: a server simply can wait in a read loop, waiting for messages to come in. But, to send them back out, it needs to keep a list of clients, and this is a bit more difficult. Clients will come and go, so some sort of “liveness” test is needed to keep the list up to date.

SCTP is a better choice: it can sit in a read loop too, but it also keeps a list of associations and, better, keeps that list up to date by sending heartbeat messages to the peers. The list management is handled by SCTP.

TCP also could be a choice: each client would start a new client socket on the server. The server then would need to keep a list of the client sockets and do a poll/select between them to see if anyone is sending a message. Again, SCTP is a better choice: in the one-to-many mode, it will keep only a single socket, and there is no need for a poll/select loop.

When it comes to sending messages back to all the connected clients, SCTP makes it even easier—the flag SCTP_SENDALL that can can be set in the sctp_sndrcvinfo field of sctp_send(). So a server simply needs to read a message from any client, set the SCTP_SENDALL bit and write it back out. The SCTP stack then will send it to all live peers! There are only a few lines of code:


nread = sctp_recvmsg(sockfd, buf, SIZE, 
                    (struct sockaddr *) &client_addr, 
                    &len, &sinfo, &flags); 
bzero(&sinfo, sizeof(sinfo)); 
sinfo.sinfo_flags |= SCTP_SENDALL; 
sctp_send(sockfd, buf, nread, &sinfo, 0);

The SCTP_SENDALL flag has been introduced only recently into SCTP and is not in current kernels (up to 2.6.21.1), but it should make it into the 2.6.22 kernels. The full code for client and server is shown in Listings 1 (chat_client.c) and 2 (chat_server.c).

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

help to compile n run

rams's picture

i m currently working in redhat version 5. I read ur article n want to run the program u have provided in SCTP. During run time what r the additional parameter required except ./chat_server.c. Should i reserve the port. Please reply me.

error :: SCTP_SENDALL;

kkk's picture

i am using linux kernel 2.6.25 and as mentation in linux journal that SCTP_SENDALL is not suported by 2.6.21 but still i am getting an error in line

"sinfo.sinfo_flags |= SCTP_SENDALL;"

kaushal

SCTP_SENDALL Related

kaushal's picture

Dear Sir,

I have installed latest 2.6.22 kernels. of sctp but stiil my code is giving error (see below). please help me out

# cc chat_server.c -o chat_server -L /usr/lib -lsctp
chat_server.c: In function ‘main’:
chat_server.c:60: error: ‘SCTP_SENDALL’ undeclared (first use in this function)
chat_server.c:60: error: (Each undeclared identifier is reported only once
chat_server.c:60: error: for each function it appears in.)

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix