.NET Socket - 1.0
Description
This extension provides support for socket-based network communication in .NET applications using protocols such as TCP and UDP.
In what situation should you use this extension?
If your .NET application source code uses the socket-based network communication using libraries such as System.Net.Sockets, you should install this extension.
Technology support
The following .NET framework / Libraries are supported by this extension:
| Framework / Library name | Version | Supported | Supported Technology | Comments |
|---|---|---|---|---|
| System.Net.Sockets | Upto 4.3.x | ✅ | C# |
Function Point, Quality and Sizing support
- Function Points (transactions): a green tick indicates that OMG Function Point counting and Transaction Risk Index are supported
- Quality and Sizing: a green tick indicates that CAST can measure size and that a minimum set of Quality Rules exist
| Function Points (transactions) | Quality and Sizing |
|---|---|
| ❌ | ❌ |
Dependencies
Some CAST extensions require the presence of other CAST extensions in order to function correctly. The .NET Socket extension requires the following CAST extensions to be installed:
- Universal Linker
- CAST AIP Internal Extension
- The .NET Analyzer is required, but it is not a dependency, this extension will be automatically installed when .NET source code is delivered for analysis.
Any dependent extensions are automatically downloaded and installed. You do not need to do anything.
Download and installation instructions
The extension will not be automatically downloaded and installed in CAST Console. If you need to use it, you should manually install the extension using the Application - Extensions interface:

What results can you expect?
Once the analysis/snapshot generation has completed, you can view the below objects and links created.
Objects
| Icon | Description | Comment |
|---|---|---|
![]() |
.NET Server Socket | An object to represent server socket with known port |
![]() |
.NET Client Socket | An object to represent client socket with known port |
![]() |
.NET Unknown Server Socket | An object to represent server socket when port is not resolved |
![]() |
.NET Unknown Client Socket | An object to represent client socket when port is not resolved |
Links
| Link type | Source and destination of link | Methods supported |
|---|---|---|
| callLink | From the caller .NET Method object to .NET Client Socket object From the .NET Server Socket object to the caller .NET Method object |
Socket class APIs |
Code examples
TCP Server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TcpServer
{
static void Main()
{
IPEndPoint ipEndPoint = new IPEndPoint(
IPAddress.Any,
5000
);
Socket listener = new Socket(
ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
listener.Bind(ipEndPoint);
listener.Listen(100);
Socket handler = listener.Accept();
while (true)
{
// Receive message
var buffer = new byte[1_024];
int received = handler.Receive(buffer, SocketFlags.None);
if (received == 0)
{
break;
}
var response = Encoding.UTF8.GetString(buffer, 0, received);
var eom = "<|EOM|>";
if (response.IndexOf(eom, StringComparison.Ordinal) > -1)
{
Console.WriteLine(
$"Socket server received message: \"{response.Replace(eom, "")}\"");
var ackMessage = "<|ACK|>";
var echoBytes = Encoding.UTF8.GetBytes(ackMessage);
handler.Send(echoBytes, SocketFlags.None);
Console.WriteLine(
$"Socket server sent acknowledgment: \"{ackMessage}\"");
break;
}
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
listener.Close();
}
}

TCP Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TcpClientApp
{
static void Main()
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 5000);
Socket client = new Socket(
ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
client.Connect(ipEndPoint);
while (true)
{
// Send message
var message = "Hi friends 👋!<|EOM|>";
var messageBytes = Encoding.UTF8.GetBytes(message);
client.Send(messageBytes, SocketFlags.None);
Console.WriteLine($"Socket client sent message: \"{message}\"");
// Receive ack
var buffer = new byte[1_024];
int received = client.Receive(buffer, SocketFlags.None);
var response = Encoding.UTF8.GetString(buffer, 0, received);
if (response == "<|ACK|>")
{
Console.WriteLine(
$"Socket client received acknowledgment: \"{response}\"");
break;
}
}
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}

UDP Server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UdpServer
{
static void StartListener(int listenPort)
{
Socket socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, listenPort);
socket.Bind(localEP);
socket.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.Broadcast,
true);
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
try
{
while (true)
{
Console.WriteLine($"Waiting for broadcast on port {listenPort}");
byte[] buffer = new byte[1024];
int received = socket.ReceiveFrom(buffer, ref remoteEP);
Console.WriteLine($"Received broadcast from {remoteEP} :");
Console.WriteLine(
$" {Encoding.ASCII.GetString(buffer, 0, received)}");
}
}
catch (SocketException e)
{
Console.WriteLine(e);
}
finally
{
socket.Close();
}
}
}

UDP Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UdpClientApp
{
static void Main()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
s.EnableBroadcast = true;
IPAddress broadcast = IPAddress.Parse("192.168.1.255");
byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);
IPEndPoint ep = new IPEndPoint(broadcast, 11000);
s.SendTo(sendbuf, ep);
Console.WriteLine("Message sent to the broadcast address");
}
}

Limitations
- Async methods such as
ConnectAsyncare not supported. - Linking between ‘.NET Server Socket’ and ‘.NET Client Socket’ objects is not supported.



