0% found this document useful (0 votes)
21 views2 pages

Snipets

The document provides code snippets for handling UDP and TCP communications, including multicast setup, JSON serialization and deserialization, and data hashing using SHA256. It demonstrates how to send and receive data over UDP and TCP, as well as how to manage JSON data with specific serialization options. Additionally, it includes a method to retrieve the first IPv4 address for a given hostname.

Uploaded by

amanda.thepond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Snipets

The document provides code snippets for handling UDP and TCP communications, including multicast setup, JSON serialization and deserialization, and data hashing using SHA256. It demonstrates how to send and receive data over UDP and TCP, as well as how to manage JSON data with specific serialization options. Additionally, it includes a method to retrieve the first IPv4 address for a given hostname.

Uploaded by

amanda.thepond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

--MUTLTICAST

var udpClient = new UdpClient(localIPEndPoint); // Initializes a new instance of


the UdpClient class and binds it to the specified local endpoint
var multicastAddress = [Link](MULTICAST_IPADDRESS);
[Link](multicastAddress);

--JSON SERIALIZE (To file)


public static void SerializeToFile<T>(string path, T item, bool verbose)
{
var options = new JsonSerializerOptions() {
WriteIndented = verbose,
PropertyNameCaseInsensitive = true,
Converters = {new
JsonStringEnumConverter([Link])}
};

var jsonString = [Link](item, options);


[Link](path, jsonString);
}

--JSON DESERIALIZE (To file)


public static T DeserializeFromFile<T>(string path)
{
var options = new JsonSerializerOptions {
Converters = {new
JsonStringEnumConverter([Link])
}
};
var jsonString = [Link](path);
return [Link]<T>(jsonString, options) ;
}

[JsonIgnore] -- um variable nicht im json zu speichern

--SEND UDP
UdpClient udpClient = new UdpClient(IpEndPoint or PORT);
var buffer = [Link](jsonString);
var count = [Link](buffer, [Link]);

--READ UDP
var buffer = [Link](ref senderIPEndPoint);

--SEND TCP
TcpClient tcpClient = new TcpClient(HOSTNAME, PORT);
var ns = [Link]();
[Link]([Link](jsonString), 0, [Link]);

--READ TCP
[Link](rcvBuffer);

--HASH DATA SHA256


HMACSHA256 sha = new HMACSHA256(hash_key);
var hash = [Link](buffer);

--GET IP
public static IPAddress GetFirstIpv4Address(string hostname)
{
return [Link](hostname)
.Where(a => [Link] == [Link])
.FirstOrDefault();
}

You might also like