Network Programming with Windows Sockets : Datagrams

10/10/2010 3:18:11 PM
Datagrams are similar to mailslots and are used in similar circumstances. There is no connection between the sender and receiver, and there can be multiple receivers. Delivery to the receiver is not ensured with either mailslots or datagrams, and successive messages will not necessarily be received in the order they were sent.

The first step in using datagrams is to specify SOCK_DGRAM in the type field when creating the socket with the socket function.

Next, use sendto and recvfrom, which take the same arguments as send and recv, but add two arguments to designate the partner station. Thus, the sendto function is as follows.

int sendto (
LPSTR lpBuffer,
int nBufferLen,
int nFlags,
int nAddrLen);

lpAddr points to an sockaddr address structure where you can specify the name of a specific machine and port, or you can specify that the datagram is to be broadcast to multiple computers; see the next section.

When using recvfrom, you specify the computers (perhaps all) from which you are willing to accept datagrams; also see the next section.

As with mailslots, datagram messages should be short; MSDN recommends 512 as the length limit for the data portion, as that limit avoids having the message sent in fragments.

Datagram Broadcasting

Several steps are necessary to broadcast sendto messages to multiple computers. Here are the basic steps; see MSDN for complete details:

  • Set the SOCK_DGRAM socket options by calling setsockopt, specifying the SO_BROADCAST option. Also, set this option for sockets that are to receive broadcast messages.

  • Set the client’s lpAddr sin_addr_in.s_addr value to INADDR_BROADCAST.

  • Set the port number as in the preceding examples.

  • The broadcasts will be sent to and received by all computer interfaces (that is, all computers with a datagram socket with the SO_BROADCAST option) to that port.

Using Datagrams for Remote Procedure Calls

A common datagram usage is to implement RPCs. Essentially, in the most common situation, a client sends a request to a server using a datagram. Because delivery is not ensured, the client will retransmit the request if a response, also using a datagram, is not received from the server after a wait period. The server must be prepared to receive the same request several times.

The important point is that the RPC client and server do not require the overhead of a stream socket connection; instead, they communicate with simple requests and responses. As an option, the RPC implementation ensures reliability through time-outs and retransmissions, simplifying the application program. Alternatively, the client and server are frequently implemented so as to use stateless protocol (they do not maintain any state information about previous messages), so each request is independent of other requests. Again, application design and implementation logic are greatly simplified.

  •  Network Programming with Windows Sockets : An Alternative Thread-Safe DLL Strategy
  •  Network Programming with Windows Sockets : A Thread-Safe DLL for Socket Messages
  •  Network Programming with Windows Sockets : In-Process Servers
  •  Network Programming with Windows Sockets : A Socket-Based Server with New Features
  •  Network Programming with Windows Sockets : A Socket-Based Client
  •  Network Programming with Windows Sockets : A Socket Message Receive Function
  •  Exchange Server 2010 : Operating Without Traditional Point-in-Time Backups
  •  Exchange Server 2010 : Performing Backup and Recovery for Mailbox Server Roles
  •  Exchange Server 2010 : Performing Backup and Recovery for Non-Mailbox Server Roles
  •  Exchange Server 2010 : Backup and Disaster Recovery Planning
  •  Changes to Backup and Restore in Exchange Server 2010
  •  Programming Windows Azure : Using the SDK and Development Storage
  •  Programming Windows Azure : Building a Storage Client
  •  Working with the REST API
  •  Excel Programmer : Fix Misteakes
  •  Excel Programmer : Change Recorded Code
  •  Excel Programmer : Record and Read Code
  •  Configuring Server Roles in Windows 2008 : New Roles in 2008
  •  Windows Server 2003 : Creating and Configuring Application Directory Partitions
  •  Windows Server 2003 : Configuring Forest and Domain Functional Levels
    Top 10
    Fujifilm XF1 - The Stylish Shooter
    Nikon 1 V2 - Still Fast and Handles Better
    Asustor AS-604T 4-Bay NAS Review (Part 3)
    Asustor AS-604T 4-Bay NAS Review (Part 2)
    Asustor AS-604T 4-Bay NAS Review (Part 1)
    Toshiba Satellite U925t Review (Part 3)
    Toshiba Satellite U925t Review (Part 2)
    Toshiba Satellite U925t Review (Part 1)
    iBall Andi 4.5H - Pretty In White
    The HTC Butterfly - Full HD In 5 Inches Only
    Most View
    Windows 7 : Command-Line and Automation Tools - Batch Files, Windows PowerShell
    IIS 7.0 : Managing Configuration - Backing Up Configuration, Using Configuration History & Exporting and Importing Configuration
    SharePoint 2010: Business Connectivity Services - The Secure Store Service (part 2) - Creating a Secure Store Service Application for Impersonating
    Reporting Services with SQL Azure : Starting a SQL Azure–Based Report
    Windows Server 2008 : Transport-Level Security - Active Directory Rights Management Services
    Who’s Watching You? (Part 4)
    Automating Blind SQL Injection Exploitation
    Partitioning Disks and Preparing Them for Use in Vista
    AMD Radeon HD 7850 2GB vs. Nvidia GeForce GTX 660 2GB vs. AMD Radeon HD 7870 2GB (Part 1)
    SharePoint 2010 : Workflow Modeling and Development Tools (part 1) - Microsoft Visio 2010 & SharePoint Designer 2010
    Sony VAIO Duo 11 - The Notebook-Tablet Hybrid
    Programming .NET Components : Serialization Events (part 3) - Type-Version Tolerance
    All You Need To Know About iOS 6 (Part 3)
    Windows Phone 8 Operating System Review – Part2
    Transferring Bookmarks With Internet Usernames And Passwords To A New PC
    Advanced ASP.NET : Data Caching (part 1) - Adding Items to the Cache & A Simple Cache Test
    Asus Rog Tytan CG8565 - Clash Of The Tytan
    2012 - The Year to Come (Part 2)
    Compact System Cameras : Samsung NX200, Nikon 1J1, Olympus PEN E-PM1, Panasonic GX1, Sony NEX-5N
    Nokia Lumia 620 Review - Basic Smartphone With Good Performance And Stunning Design (Part 1)