C# app receives message on socket (CSSocketServer)

The VC# code sample demonstrates creating a socket object to listen to the incoming connections. CSSocketClient and CSSocketServer show the basic socket programming.

C# (52.4 KB)
 
 
 
 
 
3.7 Star
(6)
12,172 times
Add to favorites
2/20/2013
E-mail Twitter del.icio.us Digg Facebook
========================================================================
    CONSOLE APPLICATION : CSSocketClient Project Overview
========================================================================

/////////////////////////////////////////////////////////////////////////////
Use:

Sockets are an application programming interface (API) in an operating system,
used for in inter-process communication. Sockets constitute a mechanism for 
delivering incoming data packets to the appropriate application process or 
thread, based on a combination of local and remote IP addresses and port 
numbers. Each socket is mapped by the operational system to a communicating
application process or thread.


.NET supplies a Socket class which implements the Berkeley sockets interface.
It provides a rich set of methods and properties for network communications. 
The Socket class allows you to perform both synchronous and asynchronous data
transfer using any of the communication protocols listed in the ProtocolType
enumeration. It supplies the following types of socket:

Stream: Supports reliable, two-way, connection-based byte streams without 
the duplication of data and without preservation of boundaries.

Dgram:Supports datagrams, which are connectionless, unreliable messages of
a fixed (typically small) maximum length. 

Raw: Supports access to the underlying transport protocol.Using the 
SocketTypeRaw, you can communicate using protocols like Internet Control 
Message Protocol (Icmp) and Internet Group Management Protocol (Igmp). 

Rdm: Supports connectionless, message-oriented, reliably delivered messages, 
and preserves message boundaries in data. 

Seqpacket:Provides connection-oriented and reliable two-way transfer of 
ordered byte streams across a network.

Unknown:Specifies an unknown Socket type.



/////////////////////////////////////////////////////////////////////////////
Prerequisites:

Silverlight 3 Tools for Visual Studio 2008 SP1
http://www.microsoft.com/downloads/details.aspx?familyid=9442b0f2-7465-417a-88f3-5e7b5409e9dd&displaylang=en

Silverilght 3 runtime:
http://silverlight.net/getstarted/


/////////////////////////////////////////////////////////////////////////////
Project Relation:

CSSocketClient <--> CSSocketServer
CSSocketServer is the server process to communicate the client process
via the Socket.


/////////////////////////////////////////////////////////////////////////////
Code Logic:

1. Create a socket to connect the TCP Socket endpoint.

2. After get the connection, send the data to the server process.

3. Receive the response of the server process.

4. Shut down the TCP connection and release the Socket resource.


/////////////////////////////////////////////////////////////////////////////
References:

Chapter4: Using Sockets of Professional .NET Network Prgromming
http://www.amazon.com/Professional-Network-Programming-Srinivasa-Sivakumar/dp/1861007353

Socket Class
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

Asynchronous Server Socket Example
http://msdn.microsoft.com/en-us/library/fx6588te.aspx


/////////////////////////////////////////////////////////////////////////////