Getting multicast messages with UDPClient - c#

I've got a product plugged directly into the ethernet port of my computer, sending multicast packets to 224.224.10.10 and UDP port 24588. I've set up my UDPclient in the code below, and I feel like I have it set up correctly, but I still don't receive any packets. I always catch an exception saying that I timed out waiting for a response. Any thoughts or glaring mistakes?
I looked at a lot of questions before posting this, but I couldn't get a resolution, and I couldn't find anyone who had the same type of setup that I have.
public class ReceiverClass
{
private UdpClient m_UDPClient = null;
private Thread m_UDPReceiverThread = null;
private bool m_ContinueReceiving = false;
private readonly object m_sync = new object();
private const int UDP_PORT = 24588;
public ReceiverClass()
{
m_ContinueReceiving = true;
m_UDPClient = new UdpClient(UDP_PORT);
m_UDPClient.Client.ReceiveTimeout = 20000;
m_UDPReceiverThread = new Thread(ReceiveData) { IsBackground = true };
m_UDPReceiverThread.Start();
}
private void ReceiveData()
{
bool Continue;
byte[] ReceiveBuffer;
IPEndPoint defaultIP = new IPEndPoint(IPAddress.Any, 0);
m_UDPClient.JoinMulticastGroup(IPAddress.Parse("224.224.10.10"));
m_UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
lock (m_sync)
{
Continue = m_ContinueReceiving;
}
while (Continue)
{
try
{
ReceiveBuffer = m_UDPClient.Receive(ref defaultIP);
if (null != ReceiveBuffer)
{
// Do stuff with received...
}
}
catch (Exception e)
{
// ooo eee kill stream
Dispose(false);
return;
}
finally
{
lock (m_sync)
{
Continue = m_ContinueReceiving;
}
}
}
}
}

I can't see anything immediately wrong with your code so I don't have a proper answer for you. However, here's a piece of code I'm using to get UDP broadcast messages. Hopefully it will work for you, or give you some new ideas:
class UdpHandler {
public UdpHandler(int portNo) {
Thread t = new Thread(ListenThread);
t.IsBackground = true;
t.Start(portNo);
}
public void ListenThread(object portNo) {
UdpClient client = new UdpClient { ExclusiveAddressUse = false };
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, (int)port);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
client.Client.Bind(localEp);
while (true) {
byte[] data = client.Receive(ref localEp);
DataReceived(data);
}
}
private void DataReceived(byte[] rawData) {
// Handle the received data
}
}

What I ended up doing was going to the Sockets class and looking at the raw packets and picking out what I needed. I bind the socket to my lan interface, and sniff stuff out. I had to run it as an administrator in order to get it to work, but thats fine. I went this route because I could see the packets in wireshark, but they weren't getting to my udpclient. This ended up being the fastest way to get what I wanted.
Socket ReceiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
EndPoint DefaultIPEndpoint = new IPEndPoint(IPAddress.Parse("10.0.2.0"), 0);
ReceiveSocket.ReceiveTimeout = 5000;
ReceiveSocket.Bind(DefaultIPEndpoint);
ReceiveSocket.IOControl(IOControlCode.ReceiveAll, new byte[4] { 1, 0, 0, 0 }, null);
while (true)
{
byte[] ReceiveBuffer = new byte[512];
int ByteCount = 0;
ByteCount = ReceiveSocket.ReceiveFrom(ReceiveBuffer, ref DefaultIPEndpoint);
// Handle packets ...
}

Related

lua socket communication from c# receiving nil

I'm trying to use lua script to extract some data from a program, and then send that data to a c# script which then processed it and returns some simple messages for the lua script to then pass along to the program.
The lua script is as follows
-- Socket decls
local socket = require("socket")
local host, port = "127.0.0.1", 3000
local tcp = assert(socket.tcp())
tcp:connect(host,port)
tcp:settimeout(0)
tcp:send("Stream starting...")
function sendEventInfoToServer(string)
tcp:send(string)
end
function processEventInfo()
local received, status = tcp:receive()
if received ~= nil then
print(received, status);
end
end
while true do
-- unrelated logic
processEventInfo();
end
and on the C# end
public class SimpleTCPListener
{
private TcpListener tcpListener;
private TcpClient _tcpClient;
private Thread listenThread;
private NetworkStream clientStream;
List<string> eventsWaiting;
public List<string> EventsWaiting {
get {
List<string> tempList = new List<string>();
for (int i = 0; i < eventsWaiting.Count; i++)
tempList.Add(eventsWaiting[i]);
EventsWaiting = new List<string>();
return tempList;
}
private set { eventsWaiting = value; }
}
public SimpleTCPListener()
{
EventsWaiting = new List<string>();
this.tcpListener = new TcpListener(IPAddress.Any, 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while(true)
{
// blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
// create a thread to handle communication
// with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom));
clientThread.Start(client);
clientStream = client.GetStream();
}
}
public void SendSavestateLoadRequest()
{
if(clientStream != null)
{
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("HelloLua!\n");
Console.WriteLine("Sending data back to lua");
if (clientStream.CanWrite)
clientStream.Write(buffer, 0, buffer.Length);
else
Console.WriteLine("Connection close!");
//clientStream.Flush();
}
}
private void HandleClientCom(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while(true)
{
bytesRead = 0;
try
{
// blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
// a socket error has occured
break;
}
if(bytesRead == 0)
{
// the client disconnected
break;
}
// message has been recieved
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
string s = encoder.GetString(message, 0, bytesRead);
Console.WriteLine(s);
if (!string.IsNullOrEmpty(s))
eventsWaiting.Add(s);
}
tcpClient.Close();
}
}
Now, the Lua talks to the C# thread no problem, and the C# doesn't report any errors when sending data back to the lua, but on the lua side it's constantly receiving nil, however if I remove the nil check it sometimes receives and prints the string, but with the nil check it never seems to find 'received' as not nil. I'm really new with lua script so hopefully it's something simple I'm not understanding about the syntax but I can't seem to find lot of documentation.
Any suggestions would be hugely appreciated.

udp client bind

I have UDP client chat, which sends messages to the server and get the responses back. I have two threads, one for sending, one for receiving messages. I get the exception with ReceiveFrom() method: "You must call the Bind method before performing this operation". But this is a client, I don't want to bind anything. For example this client works fine:
byte[] data = new byte[30];
string input, stringData;
IPEndPoint servIPEP = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 9050);
EndPoint servEP = (EndPoint)servIPEP;
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
client.SendTo(data, data.Length, SocketFlags.None, servEP);
data = new byte[30];
int recv = client.ReceiveFrom(data, ref servEP); //works fine!
There is no bind for receiving. But when I create two threads error is thrown:
public ChatClient()
{
clientSock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
servIPEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 32000);
servEP = (EndPoint)servIPEP;
}
public void ReceiveThread()
{
Thread receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.Start();
}
public void ReceiveData()
{
while(true){
clientSock.ReceiveFrom(buf, ref servEP); //Here I get ERROR
string msg = Encoding.ASCII.GetString(buf).Trim();
Console.WriteLine("New message: {0}",msg);
}
}
public void SendThread()
{
Thread sendThread = new Thread(new ThreadStart(SendData));
sendThread.Start();
}
public void SendData()
{
while (true)
{
Console.WriteLine("Enter message to send: ");
string msg = Console.ReadLine();
buf = Encoding.UTF8.GetBytes(msg);
clientSock.SendTo(buf, servEP);
}
}
}
static void Main(string[] args)
{
ChatClient client = new ChatClient();
client.SendThread();
client.ReceiveThread();
}
}
Thank you for your time.
Clearly SendTo() does an implicit bind, and ReceiveFrom() doesn't.
It doesn't make much sense to start receiving without a prior bind to at least set the port number you're receiving via. Otherwise how would you expect to get any data?

Receiving and processing continous packets using UDP

This is my current setup (using UDP):
void OnDataReceived(IAsyncResult result)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = socket.EndReceive(result, ref ep);
Packet p = new Packet(Encoding.ASCII.GetString(buffer, 0, buffer.Length));
//process packet
socket.BeginReceive(new AsyncCallback(OnDataReceived), socket);
}
I was wondering what would happen if I immediately call socket.BeginReceive after calling EndReceive and then process the packet to obtain a continous packet flow like this:
void OnDataReceived(IAsyncResult result)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = socket.EndReceive(result, ref ep);
socket.BeginReceive(new AsyncCallback(OnDataReceived), socket);
Packet p = new Packet(Encoding.ASCII.GetString(buffer, 0, buffer.Length));
//process packets
}
If a packet is received as soon as I call BeginReceive, would this conflict with the current packet processing somehow?
Also if this would not conflict would changing to TCP make this disfunctional?
Looks like you are creating some sort of recursive handler there. I am unsure how that will work, probably not in a good way. I usually go for a separate reader thread that listens to incoming data and passes it on to an event. This has served me well in the past. I have not looked into using async for this though.
Here is some example code on how to use a separate thread to handle incoming UDP data. It is not complete but should give you an idea of how to set it up.
private Thread _udpReadThread;
private volatile bool _terminateThread;
public event DataEventHandler OnDataReceived;
public delegate void DataEventHandler(object sender, DataEventArgs e);
private void CreateUdpReadThread()
{
_udpReadThread = new Thread(UdpReadThread) { Name = "UDP Read thread" };
_udpReadThread.Start(new IPEndPoint(IPAddress.Any, 1234));
}
private void UdpReadThread(object endPoint)
{
var myEndPoint = (EndPoint)endPoint;
var udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// Important to specify a timeout value, otherwise the socket ReceiveFrom()
// will block indefinitely if no packets are received and the thread will never terminate
udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
udpListener.Bind(myEndPoint);
try
{
while (!_terminateThread)
{
try
{
var buffer = new byte[1024];
var size = udpListener.ReceiveFrom(buffer, ref myEndPoint);
Array.Resize(ref buffer, size);
// Let any consumer(s) handle the data via an event
FireOnDataReceived(((IPEndPoint)(myEndPoint)).Address, buffer);
}
catch (SocketException socketException)
{
// Handle socket errors
}
}
}
finally
{
// Close Socket
udpListener.Shutdown(SocketShutdown.Both);
udpListener.Close();
}
}
public class DataEventArgs : EventArgs
{
public byte[] Data { get; private set; }
public IPAddress IpAddress { get; private set; }
public DataEventArgs(IPAddress ipaddress, byte[] data)
{
IpAddress = ipaddress;
Data = data;
}
}

C# TCP Chat Application Threading

I am in desperate need of help. I have essentially created a program that (will use) encryption to send messages back and forth. The encryption part is working fine, however I am fairly new to Threads and I can not for the life of me get my Client/Server pieces of the application to line up. The chat program is direct IP connection using TCP, so each host is a client and a server. The issue I appear to be having, when I debug, is that the server thread is either not ready when the Client tries to connect to it, or if it is ready it will not relinquish the Thread so the Client can connect! I have been working on this for hours and it is very frustrating.. I hope someone can help! I've included my code below.
This is my code snippet from my MainForm, which constructs the Client and Server aspects:
private void InitializeComponent() {
server = new ServerSide("127.0.0.1",7865);
servThread = new Thread(new ThreadStart(server.begin));
client = new ClientSide("127.0.0.1",7865);
clientThread = new Thread(new ThreadStart(client.begin));
servThread.Start();
clientThread.Start();
//servThread.Join();
//clientThread.Join();
}
This is my ServerSide code:
public class ServerSide
{
String IpString;
int tcpPort;
bool goodToGo = false;
System.Net.IPAddress ipAddress = null;
public ServerSide(String ip, int tcpPort)
{
IpString = ip;
bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
if (isValidIp == true) // check if the IP is valid, set the bool to true if so
{
goodToGo = true;
}
else
{
goodToGo = false;
}
}
public void begin()
{
try
{
IPAddress ipAd = IPAddress.Parse(IpString);
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, tcpPort);
Socket s = null;
/* Start Listening at the specified port */
while (true)
{
myList.Start();
if (myList.Pending())
{
s = myList.AcceptSocket();
break;
}
}
String toReceive = "";
while (true)
{
byte[] b = new byte[4096];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
toReceive += Convert.ToString((Convert.ToChar(b[i])));
// ASCIIEncoding asen = new ASCIIEncoding();
var form = MainForm.ActiveForm as MainForm;
if (form != null)
{
form.messageReceived(toReceive);
}
toReceive = "";
}
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
}
ClientSide code:
public class ClientSide
{
private String IpString;
private int tcpPort;
private TcpClient tcpInt;
private static Stream stm;
private System.Net.IPAddress ipAddress = null;
private bool goodToGo = false;
public ClientSide(String ip, int tcpPort)
{
IpString = ip;
this.tcpPort = tcpPort;
bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
if (isValidIp == true)
{
goodToGo = true;
}
else
{
goodToGo = false;
}
}
public void begin()
{
try
{
tcpInt = new TcpClient();
// Console.WriteLine("Connecting.....");
tcpInt.Connect(IpString, tcpPort);
// use the ipaddress as in the server program
// Console.WriteLine("Connected");
// String str = Console.ReadLine();
stm = tcpInt.GetStream();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
public void sendMessage(String str)
{
// stm = tcpInt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
/** byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));*/
}
}
}
Once again.. what typically happens is my Client receives an error that the host actively refused the connection.. but I do not know how to time them. I am looking for how to have my server listening for TCP Connections, but still manage to go to my other thread to create the TCP Connecion to send the server (I am testing this on localhost currently).
Thanks.
In the constructor of the ServerSide class, you forgot to initialize the tcpPort member. Because you forgot, the server will try to listen to port 0 and the client will try to connect to port 7865, and it will fail.
Try adding this code to the constructor in the ServerSide class.
this.tcpPort = tcpPort;
As for the threads, you might have a problem if the client thread tries to connect before the server thread has started listening. You can use a loop to try to connect a number of times (let's say 10), with a timeout (let's say 1 second) if you don't succeed. This way, you will retry 10 times and give up after that.

Reading off a socket until end of line C#?

I'm trying to write a service that listens to a TCP Socket on a given port until an end of line is recived and then based on the "line" that was received executes a command.
I've followed a basic socket programming tutorial for c# and have come up with the following code to listen to a socket:
public void StartListening()
{
_log.Debug("Creating Maing TCP Listen Socket");
_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, _port);
_log.Debug("Binding to local IP Address");
_mainSocket.Bind(ipLocal);
_log.DebugFormat("Listening to port {0}",_port);
_mainSocket.Listen(10);
_log.Debug("Creating Asynchronous callback for client connections");
_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
_log.Debug("OnClientConnect Creating worker socket");
Socket workerSocket = _mainSocket.EndAccept(asyn);
_log.Debug("Adding worker socket to list");
_workerSockets.Add(workerSocket);
_log.Debug("Waiting For Data");
WaitForData(workerSocket);
_log.DebugFormat("Clients Connected [{0}]", _workerSockets.Count);
_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (ObjectDisposedException)
{
_log.Error("OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
_log.Error("Socket Exception", se);
}
}
public class SocketPacket
{
private System.Net.Sockets.Socket _currentSocket;
public System.Net.Sockets.Socket CurrentSocket
{
get { return _currentSocket; }
set { _currentSocket = value; }
}
private byte[] _dataBuffer = new byte[1];
public byte[] DataBuffer
{
get { return _dataBuffer; }
set { _dataBuffer = value; }
}
}
private void WaitForData(Socket workerSocket)
{
_log.Debug("Entering WaitForData");
try
{
lock (this)
{
if (_workerCallback == null)
{
_log.Debug("Initializing worker callback to OnDataRecieved");
_workerCallback = new AsyncCallback(OnDataRecieved);
}
}
SocketPacket socketPacket = new SocketPacket();
socketPacket.CurrentSocket = workerSocket;
workerSocket.BeginReceive(socketPacket.DataBuffer, 0, socketPacket.DataBuffer.Length, SocketFlags.None, _workerCallback, socketPacket);
}
catch (SocketException se)
{
_log.Error("Socket Exception", se);
}
}
public void OnDataRecieved(IAsyncResult asyn)
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
try
{
int iRx = socketData.CurrentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
_log.DebugFormat("Created Char array to hold incomming data. [{0}]",iRx+1);
System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
int charLength = decoder.GetChars(socketData.DataBuffer, 0, iRx, chars, 0);
_log.DebugFormat("Read [{0}] characters",charLength);
String data = new String(chars);
_log.DebugFormat("Read in String \"{0}\"",data);
WaitForData(socketData.CurrentSocket);
}
catch (ObjectDisposedException)
{
_log.Error("OnDataReceived: Socket has been closed. Removing Socket");
_workerSockets.Remove(socketData.CurrentSocket);
}
catch (SocketException se)
{
_log.Error("SocketException:",se);
_workerSockets.Remove(socketData.CurrentSocket);
}
}
This I thought was going to be a good basis for what I wanted to do, but the code I have appended the incoming characters to a text box one by one and didn't do anything with it. Which doesn't really work for what I want to do.
My main issue is the decoupling of the OnDataReceived method from the Wait for data method. which means I'm having issues building a string (I would use a string builder but I can accept multiple connections so that doesn't really work.
Ideally I'd like to look while listening to a socket until I see and end of line character and then call a method with the resulting string as a parameter.
What's the best way to go about doing this.
Try using asynch sockets. The code below will listening to a socket, and if the new line char through telnet is recieved it will echo it back out to the incomming socket. It seems like you would just need to redirect that input to your text box.
private string _hostName;
private const int _LISTENINGPORT = 23;
private Socket _incomingSocket;
byte[] _recievedData;
//todo: do we need 1024 byte? the asynch methods read the bytes as they come
//so when 1 byte typed == 1 byte read. Unless its new line then it is two.
private const int _DATASIZE = 1024;
public ConnectionServer()
{
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
_hostName = Dns.GetHostName();
_recievedData = new byte[_DATASIZE];
_incomingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(localAddr, _LISTENINGPORT);
_incomingSocket.Bind(endPoint);
_incomingSocket.Listen(10);
}
~ConnectionServer()
{
}
public void StartListening()
{
_incomingSocket.BeginAccept(new AsyncCallback(OnAccept), _incomingSocket);
}
private void OnAccept(IAsyncResult result)
{
UserConnection connectionInfo = new UserConnection();
Socket acceptedSocket = (Socket)result.AsyncState;
connectionInfo.userSocket = acceptedSocket.EndAccept(result);
connectionInfo.messageBuffer = new byte[_DATASIZE];
//Begin acynch communication with target socket
connectionInfo.userSocket.BeginReceive(connectionInfo.messageBuffer, 0, _DATASIZE, SocketFlags.None,
new AsyncCallback(OnReceiveMessage), connectionInfo);
//reset the listnening socket to start accepting
_incomingSocket.BeginAccept(new AsyncCallback(OnAccept), result.AsyncState);
}
private void OnReceiveMessage(IAsyncResult result)
{
UserConnection connectionInfo = (UserConnection)result.AsyncState;
int bytesRead = connectionInfo.userSocket.EndReceive(result);
if (connectionInfo.messageBuffer[0] != 13 && connectionInfo.messageBuffer[1] != 10)
//ascii for newline and line feed
//todo dress this up
{
if (string.IsNullOrEmpty(connectionInfo.message))
{
connectionInfo.message = ASCIIEncoding.ASCII.GetString(connectionInfo.messageBuffer);
}
else
{
connectionInfo.message += ASCIIEncoding.ASCII.GetString(connectionInfo.messageBuffer);
}
}
else
{
connectionInfo.userSocket.Send(ASCIIEncoding.ASCII.GetBytes(connectionInfo.message), SocketFlags.None);
connectionInfo.userSocket.Send(connectionInfo.messageBuffer, SocketFlags.None);
connectionInfo.message = string.Empty;
connectionInfo.messageBuffer = new byte[_DATASIZE];
}
{
public class UserConnection
{
public Socket userSocket { get; set; }
public Byte[] messageBuffer { get; set; }
public string message { get; set; }
}
}
You seem to have several issues:
You have an asynchronous method called WaitForData. That's very confusing, as methods with the word Wait in their names generally block the currently executing thread until something happens (or, optionally, a timeout expires). This does the exact opposite. Are you intending for this to be a synchronous or asynchronous operation?
There's also no need to instantiate the Decoder object, nor do you need the char array for (it seems) anything; just call System.Text.Encoding.UTF8.GetString(socketData.DataBuffer, 0, iRx).
You also don't appear to be doing anything with lines...which is why it doesn't do anything with lines.
Your approach with using a StringBuilder is what I would do. I would add a StringBuilder to the SocketData class and call it Builder. As you capture string data, do something like this:
string[] data = System.Text.Encoding.UTF8.GetString(
socketData.DataBuffer, 0, iRx).Split(Environment.NewLine);
socketData.Builder.Append(data[0]);
for(int i = 1; i < data.Length; i++)
{
// the socketData.Builder variable now contains a single line, so do
// something with it here, like raise an event
OnLineReceived(builder.ToString());
socketData.Builder = new StringBuilder(data[i]);
}
The one caveat here is that UTF8 is a multi-byte encoding, meaning that you could potentially grab a chunk of data that cuts off mid-character. It's generally a better idea to do this sort of preprocessing on the other side of the communication, then send the data in an appropriately length-prefixed format.

Categories

Resources