I have currently the problem, that messages are supposed to arrive separated at the server.
private Encoding _encode = Encoding.Default;
public Encoding DataEncoding
{
get
{
return _encode;
}
set
{
_encode = value;
}
}
public void Send(string data)
{
Task.Run(() =>
{
if (this.ConnectionState != ConnectionStatus.Connected)
{
this.ConnectionState = ConnectionStatus.SendFail_NotConnected;
return;
}
var bytes = _encode.GetBytes(data);
SocketError err = new SocketError();
this._client.Client.Send(bytes, 0, bytes.Length, SocketFlags.None, out err);
if (err != SocketError.Success)
{
Action doDCHost = new Action(DisconnectByHost);
doDCHost.Invoke();
}
});
}
The function to call the Socket.Send Method is totaly basic.
A string that needs to be send looks like this
STX-MESSAGESTRING-ETX
However, if there are many strings to be send, the message gets fragmented.
The Server receives a message that looks like this:
STX-MESSAGESTRING-ETXSTX-MESSAGESTRING2-ETXSTX-MESSAGESTRING3-ETX
What is the best way to prevent this from happening? Sadly there is no flag that indicates that the socket is currently empty or something.
Grizzy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.