tcannon91
tcannon91

Reputation: 217

How can I read from a socket repeatedly?

To start I am coding in C#. I am writing data of varying sizes to a device through a socket. After writing the data I want to read from the socket because the device will write back an error code/completion message once it has finished processing all of the data. Currently I have something like this:

byte[] resultErrorCode = new byte[1];
resultErrorCode[0] = 255;

while (resultErrorCode[0] == 255)
{
    try
    {
        ReadFromSocket(ref resultErrorCode);
    }

    catch (Exception)
    {
    }
}
Console.WriteLine(ErrorList[resultErrorCode[0] - 48]);

I use ReadFromSocket in other places, so I know that it is working correctly. What ends up happening is that the port I am connecting from (on my machine) changes to random ports. I think that this causes the firmware on the other side to have a bad connection. So when I write data on the other side, it tries to write data to the original port that I connected through, but after trying to read several times, the connection port changes on my side.

How can I read from the socket continuously until I receive a completion command? If I know that something is wrong with the loop because for my smallest test file it takes 1 min and 13 seconds pretty consistently. I have tested the code by removing the loop and putting the code to sleep for 1 min and 15 seconds. When it resumes, it successfully reads the completion command that I am expecting. Does anyone have any advice?

Upvotes: 3

Views: 2063

Answers (1)

GETah
GETah

Reputation: 21419

What you should have is a separate thread which will act like a driver of your external hardware. This thread will receive all data, parse it and transmit the appropriate messages to the rest of your application. This portion of code will give you an idea of how receive and parse data from your hardware.

  public void ContinuousReceive(){
    byte[] buffer = new byte[1024];
    bool terminationCodeReceived = false;
    while(!terminationCodeReceived){
      try{
          if(server.Receive(buffer)>0){
             // We got something
             // Parse the received data and check if the termination code
             // is received or not
          }
      }catch (SocketException e){
          Console.WriteLine("Oops! Something bad happened:" + e.Message);
      }
    }
  }

Notes:

  1. If you want to open a specific port on your machine (some external hardware are configured to talk to a predefined port) then you should specify that when you create your socket
  2. Never close your socket until you want to stop your application or the external hardware API requires that. Keeping your socket open will resolve the random port change
  3. using Thread.Sleep when dealing with external hardware is not a good idea. When possible, you should either use events (in case of RS232 connections) or blocking calls on separate threads as it is the case in the code above.

Upvotes: 1

Related Questions