6. Network Programming

In this tutorial, network programming is explained using a simple client server example. For connecting between the client and the server we use windows sockets. 

What is a Socket?

A socket is a communication endpoint — an object through which a Windows Sockets application sends or receives packets of data across a network. A socket has a type and is associated with a running process, and it may have a name. Currently, sockets generally exchange data only with other sockets in the same "communication domain," which uses the Internet Protocol Suite.

Two socket types are available:

  • Stream sockets

    Stream sockets provide a data flow without record boundaries: a stream of bytes. Streams are guaranteed to be delivered and to be correctly sequenced and unduplicated.

  • Datagram sockets

    Datagram sockets support a record-oriented data flow that is not guaranteed to be delivered and may not be sequenced as sent or unduplicated.

"Sequenced" means that packets are delivered in the order sent. "Unduplicated" means that you will get a particular packet only once. 

Both kinds of sockets are bidirectional; they are data flows that can be communicated in both directions simultaneously (full-duplex).

The MFC Classes for socket communication are CAsyncSocket and CSocket.

CAsyncSocket: A CAsyncSocket object represents a Windows Socket — an endpoint of network communication. Class CAsyncSocket encapsulates the Windows Socket Functions API, providing an object-oriented abstraction for programmers who want to use Windows Sockets in conjunction with MFC.

CSocket : Class CSocket derives from CAsyncSocket and inherits its encapsulation of the Windows Sockets API. A CSocket object represents a higher level of abstraction of the Windows Sockets API than that of a CAsyncSocket object. For more information please refer MSDN. The sequence flow for creating a simple client and server using socket is as follows.

Setting Up Communication Between a Server and a Client

Server Client
// construct a socket

CSocket sockSrvr;

// construct a socket

CSocket sockClient;

// create the SOCKET

sockSrvr.Create(nPort);

// create the SOCKET

sockClient.Create( );

// start listening

sockSrvr.Listen( );

 
  // seek a connection

sockClient.Connect(strAddr, nPort);3,4

// construct a new, empty socket

CSocket sockRecv;

// accept connection

sockSrvr.Accept( sockRecv );  

 
// construct file object

CSocketFile file(&sockRecv);

// construct file object

CSocketFile file(&sockClient);

// construct an archive

CArchive arIn(&file,
         CArchive::load);

-or-

CArchive arOut(&file,
         CArchive::store);

– or Both – 

// construct an archive

CArchive arIn(&file,
         CArchive::load);

-or-

CArchive arOut(&file,
         CArchive::store);

 – or Both – 

// use the archive to pass data:

arIn >> dwValue;

-or-

arOut << dwValue;

// use the archive to pass data:

arIn >> dwValue;

-or-

arOut << dwValue;

 Click on the appropriate  link below to see the Video and the source.

 

 

 Network Programming using VC++ and Windows Sockets

 



Download Source Code

   

Site optimized for IE6 and above. Copyright © 2010. Site Developed Using KTS WebCloud