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.)

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState