Is it possible to send datagrams over TCP?

sustrik picture sustrik · Apr 9, 2010 · Viewed 7.3k times · Source

In theory nothing prevents using TCP socket with SOCK_DGRAM. What you'll get is reliable datagram transport. Is this possible using Berkeley sockets?

Answer

Robert S. Barnes picture Robert S. Barnes · Apr 10, 2010

What you want to take a look at is the SCTP Protocol. It provides the ability to send Datagrams over a reliable TCP style connection:

In contrast to TCP, SCTP may be characterized as record-oriented, meaning it transports data in terms of messages, in a similar fashion to the User Datagram Protocol (UDP), so that a group of bytes (message) sent in one transmission operation (record) is read exactly as that group at the receiver application. TCP is stream-oriented, transporting streams of bytes, which it correctly reorders in case of out-of-order delivery. It does not, however, honor message boundaries, i.e., the structure of data in terms of their original transmission units at the sender.

Take a look at SCTP one to one style connections which are probably what you're looking for.

There are implementations for most Unix / Linux OS and there is a third party implementation for Windows. See the end of the Wiki article I linked for details.