none
TcpClient - receives unasked messages, concept RRS feed

  • Question

  • Hello,
    I have a client that needs to respond to messages.
    The message are XML structure.
    It seems to work, the problem where I see it, I read the list and then it will be filled.
     List<string> listMessages = new List<string>();
     
    I need your expert knowledge.
    Do I have to use a thread-safe list, if so, how?
    BlockingCollection<string> bc = new BlockingCollection<string>()
    How do I delete the entries?
    STEP1
    public void Execute()
    {
    	MyTokenSource = new CancellationTokenSource();
    	MyToken = MyTokenSource.Token;
    	try
    	{
    		Task.Run(() =>
    		{
    			Workingloop(MyToken);  // besser so verwenden
    		});
    		
    		
    		
    //STEP2
    public async void Workingloop(CancellationToken token)
    {
    	int value1 = 0;
    	int value2 = 0;
    	string value3 = "";
    	try
    	{
    		while (!token.IsCancellationRequested)
    		{
    			List<string> recList = await SocketClient.ReceiveSocket(SocketDataEchange, token);
    
    			if (recList != null && recList.Count == 1)
    			{
    				if (recList[0] == "ConnectionReset")
    				{
    					recList.Clear();
    					Stop();
    					Connect(IP, Port);
    					Execute();
    				}
    				else
    				{
    					if (recList[0].Length > 0 && recList[0].Substring(0, 1) != "<")
    						recList.Clear();
    				}
    			}
    			
    			
    //STEP3
    internal class SocketsSendReceive
    {
    	byte[] Buffer = new byte[1000];
    	String ReceiveBuffer = string.Empty;
    	int SizeReceive = -1;
    	internal async Task<List<string>> ReceiveSocket(Socket sck, CancellationToken myToken)
    	{
    		return await Task.Run(() =>
    		 {
    			 List<string> listMessages = new List<string>();
    			 while (SizeReceive != 0 && !myToken.IsCancellationRequested)
    			 {
    				 if (sck.Connected == false)
    				 {
    					 return null;
    				 }
    
    				 try
    				 {
    				    // Sync
    					 SizeReceive = sck.Receive(Buffer, 0, Buffer.Length, SocketFlags.None);
    				 }
    				 catch (SocketException eS)
    				 {
    					 Log.Error($"Socket Exception {eS.Message}");
    
    					 listMessages = new List<string>();
    
    					 switch ((SocketError)eS.ErrorCode)
    					 {
    						 default:
    							 listMessages.Add(((SocketError)eS.ErrorCode).ToString());
    							 break;
    					 }
    					 return listMessages;
    				 }
    				 catch (Exception e1)
    				 {
    					 break;
    				 }
    
    				 ReceiveBuffer += Encoding.ASCII.GetString(Buffer, 0, SizeReceive).Replace('\r', ' ').Replace('\n', ' '); 
    				 int lengthFirstTag = ReceiveBuffer.IndexOf(">");
    				 if (lengthFirstTag <= 0)
    					 continue;
    Best regards, Markus
    Thursday, February 14, 2019 5:49 PM

Answers


  • Hi  Markus,    

    >>The high-level version of C# adds async and await methods to ensure thread safety.
    So in this case it is not needed, async, await makes thread safety.

    The meaning of this sentence is that the high version provides some techniques that can be thread safer, but you need to use it.

    You can also use other thread-safe techniques to ensure thread safety.

    Besides, a long-term test is essential. To ensure the stability of the program.

    Best Regards,

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 18, 2019 2:51 AM
    Moderator

All replies


  • Hi    Markus Freitag,

    >>Do I have to use a thread-safe list, if so, how?

    As far as I know, the most Thread safety issues are caused by global variables and static variables. You need to take care about synchronization and mutual exclusion.

    The high-level version of C# adds async and await methods to ensure thread safety.

    So, you have use some technology to keep Thread safety. You should make a run test for a long time on your side.

    >>BlockingCollection<string> bc = new BlockingCollection<string>()
    How do I delete the entries?

    To remove an item from a BlockingCollection, you can use the Take or the TryTake method.
     
    Best Regards,

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 15, 2019 9:43 AM
    Moderator
  • Hello,
    Thanks for response.
    >The high-level version of C# adds async and await methods to ensure thread safety.
    So in this case it is not needed, async, await makes thread safety.
    For my simulation it works without blocking list.
    Maybe someone else can say something, leave the thread still open.
    Hope ok.
    Greetings, Markus
    Friday, February 15, 2019 5:27 PM

  • Hi  Markus,    

    >>The high-level version of C# adds async and await methods to ensure thread safety.
    So in this case it is not needed, async, await makes thread safety.

    The meaning of this sentence is that the high version provides some techniques that can be thread safer, but you need to use it.

    You can also use other thread-safe techniques to ensure thread safety.

    Besides, a long-term test is essential. To ensure the stability of the program.

    Best Regards,

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 18, 2019 2:51 AM
    Moderator