The TCP/IP socket web application is not responding after few request .
What I have tried:
Our company manufactures weighing scales.I have developed a programmed that communicates with the scale and get the current weight data back.The scale has a LAN card that enable us to communicate using TCP/IP socket Programming.The Scale is been provided an ip address and a port number (acts as a server).I have developed a web application that communicate with the device connect in a lan network and get the weighing data back.
public void TcpClientScale()
{
if (!System.IO.Directory.Exists("c://EssaeLog"))
{
System.IO.Directory.CreateDirectory("c://EssaeLog");
}
string path ="c://EssaeLog//"+ DateTime.Now.ToString("dd-MM-yyyy") + ".txt";
TcpClient tc = new TcpClient();
try
{
NetworkStream ns = null;
tc.Connect(IP, int.Parse(PORT));
byte[] myArray = new byte[100];
int value = Convert.ToInt32("5", 16);
string stringValue = Char.ConvertFromUtf32(value);
ns = tc.GetStream();
tc.SendTimeout = 2000;
tc.ReceiveTimeout = 2000;
byte[] tosendbytes = Encoding.ASCII.GetBytes((stringValue));
ns.Write(tosendbytes, 0, tosendbytes.Length);
int no = ns.Read(myArray, 0, 100);
string str = System.Text.Encoding.ASCII.GetString(myArray);
str = str.Substring(0, no);
Result = str;
File.AppendAllText(path, "-------------> Recieved Successfully @" + DateTime.Now.ToString() + Environment.NewLine );
}
catch(Exception ex)
{
File.AppendAllText(path, Environment.NewLine + "-------------> Error @" + DateTime.Now.ToString() + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine);
}
finally
{
tc.Close();
Dispose();
}
}
I am using IIS as server and publishing the web app.The problem that I am facing is ,after few requests the machine is not responding back and the webpages seems to be hanging .I have checked the machine using Hyper-terminal there is no problem with the machine.I think there is some problem with the code .Please help me what I am doing wrong.
UPDATE :
I have update the code with proper try catch .Now I am getting the following error in the log file.
System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at Scale.GetWeight.TcpClientScale() in C:\Users\Demo\Desktop\Pranav\LandMark Issue\VolumetricScale\Source\Scale\Scale\GetWeight.aspx.cs:line 92
UPDATE :
Another error that i am getting after few request .
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 172.21.201.229:4321
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Scale.GetWeight.TcpClientScale() in C:\Users\Demo\Desktop\Pranav\LandMark Issue\VolumetricScale\Source\Scale\Scale\GetWeight.aspx.cs:line 84
after few minutes the application starts workiing again for sometimes and then i am getting the same error again .Please help me out that what is causing this problem