Using VS2010 on Windows 7.
I am having a problems receiving data (Byte[] array) using Socket.Send() and Socket.Receive(), so they are blocking calls. The code can work fine for 30 minutes, but eventually the Byte[] being returned will be all zeros, or corrupt. The code works fine calling just one of the Synchronous DoXX() methods.
Each of the DOXX() methods send 6 bytes, then receives 625 bytes. So socket does send and receive.
Notice that I am using: Task.WaitAll ( tasks ); This is necessary so that all 4 calls are completed at one time, (need to send to SQL DB). Also, since the application is designed to run nonstop I never close the Socket. I was Opening and closing Socket connection inside DOXX() methods, then changed to leaving the socket open, thinking that might solve the problem. No change problem still occurs with sockets open constantly.
NOTE: The Update() function executes once every 5 seconds from a Console application using a Timer. The four calls never take more than 1.5 seconds to complete, usually 500 milliseconds.
public void Update( ) { // Called every 5 seconds from Timer in Console application. Task[ ] tasks = new Task[ 4 ]; tasks[ 0 ] = Task.Factory.StartNew ( ( ) => Do12() ); tasks[ 1 ] = Task.Factory.StartNew ( ( ) => Do13 ( ) ); tasks[ 2 ] = Task.Factory.StartNew ( ( ) => Do14 ( ) ); tasks[ 3 ] = Task.Factory.StartNew ( ( ) => Do15 ( ) ); Task.WaitAll ( tasks ); }
private void Do12( ) { byte[ ] requestBytes = Encoding.ASCII.GetBytes ( Command ); m_Socket12.Send ( requestBytes );
byte[ ] bytes = new byte[ NumReceivedBytes ]; m_Socket12.Receive(bytes, bytes.Length, SocketFlags.None); Bin48NetTime packet = new Bin48NetTime ( newBytes ); Console.WriteLine ( packet.SerialNumberString ); } private void Do13() { Same as Do12 using a different IP address } private void Do14() { Same as Do12 using a different IP address } private void Do15() { Same as Do12 using a different IP address }