Arad
New member
- Joined
- May 17, 2021
- Messages
- 1
- Programming Experience
- Beginner
I am just getting started with C# , i am experimenting with a socket server , socket server is receiving a string from a client application.
this is the string that is being received by the server :
This is the Code that is receiving the data from client :
Problem i am facing is with the state.sb.clear() , for some reason it doest not always work and sometimes it gives me appended string like this :
I am attaching the screenshot of the console as well , notice it was working correctly and then it gave the appended string values all of a sudden and then continued normally again .
What could be the cause of this ? is the implementation of state.sb.clear not correct?
this is the string that is being received by the server :
Value0_Value1_Value2_Value3_Value4_keep5
This is the Code that is receiving the data from client :
partial Socket server code:
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Clear();
string proce0, proce1, proce2, proce3, proce4, proce5 = String.Empty;
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
content = state.sb.ToString();
Console.WriteLine(content);
string[] Splitdata1 = content.Split('_');
int splitelements = Splitdata1.Length;
Console.WriteLine(splitelements);
if (splitelements == 1)
{
proce0 = Splitdata1[0];
Console.WriteLine("Value: " + proce0);
state.sb.Clear();
}
else if (splitelements == 6)
{
proce0 = Splitdata1[0];
proce1 = Splitdata1[1];
proce2 = Splitdata1[2];
proce3 = Splitdata1[3];
proce4 = Splitdata1[4];
proce5 = Splitdata1[5];
Console.WriteLine("Values:\n" + proce0 + "\n" + proce1 + "\n" + proce2 + "\n" + proce3 + "\n" + proce4 + "\n" + proce5 + "\n" + "\n" + "\n");
state.sb.Clear();
}
if (content.IndexOf("END5") > -1)
{
Console.WriteLine(content);
Send(handler, content);
state.sb.Clear();
}
else
{
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
Problem i am facing is with the state.sb.clear() , for some reason it doest not always work and sometimes it gives me appended string like this :
instead of just one string like this :Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5Value0_Value1_Value2_Value3_Value4_keep5
Value0_Value1_Value2_Value3_Value4_keep5
I am attaching the screenshot of the console as well , notice it was working correctly and then it gave the appended string values all of a sudden and then continued normally again .
What could be the cause of this ? is the implementation of state.sb.clear not correct?