[Sample of Mar 21th] Socket communication in C# and VB

 

Homepage image
Sample of the Day RSS Feed

Sample Download
C# version: https://code.msdn.microsoft.com/CSSocketCommunication-089f0e86
VB version: https://code.msdn.microsoft.com/VBSocketCommunication-f1fb4541

Today’s code sample demonstrates the basic socket network communication in C# and VB. 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.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

 

Introduction

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.

There are some limitations on this sample:

1. Due to the socket buffer size, the string message including EOM marker shouldn't bigger than 1024 bytes when encoded to bytes by Unicode.
2. The sample is designed for receiving and sending only one string message.

To overcome the limitations above, the developer need handle message separating and merging operations on socket buffer.

 

Code Logic

1. Create a socket to listen the incoming TCP connection.
2. After get the client connection,asynchronously receive the data and listen the TCP connection again.
3. Finishing receiving data, send the response to client process.
4. If user inputs the word quit to exit the program

 

Running the Sample

Socket server:

image

Socket client:

image

 

More Information

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

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

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