Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

C#
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);//getting the result and storing it in a string//                
                str = str.Substring(0, no);//checking and removing any unwanted garbage value if any //
                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
Posted
Updated 4-May-16 23:02pm
v9
Comments
Herman<T>.Instance 2-May-16 5:19am    
why not catch Exception and see what exception you have?
tc.close in TRY and in FINALLY. That must lead to errors
And you could do:
using (TcpClient tc = new TcpClient(PORT) { // your code here } . Closing and disposing is than done correctly.
pranav8265 2-May-16 5:28am    
<pre lang="c#">public void TcpClientScale()
{
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);//command send to machine
string stringValue = Char.ConvertFromUtf32(value);//command send to machine
ns = tc.GetStream();
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);//getting the result and storing it in a string//
str = str.Substring(0, no);//checking and removing any unwanted garbage value if any //
Result = str;
}
catch(Exception ex)
{
Result = ex.ToString();
}
finally
{
tc.Close();
Dispose();
}</pre>

I have removed tc.close() from the try block .I am getting the same problem .I tried finding out the error in catch .I am getting timed out error.I have done some modification in IIS setting by increasing the worker process from 1 to 10.Now instead to 30 or 35 request ,my application stops responding after more than 100 request.Is there any setting in IIS that need to be followed while deploying asp.net application?
Dave Kreskowiak 2-May-16 11:36am    
In your Catch block, you're just eating an exceptions and not reporting it. It would appear as though your code is throwing an exception that would help you troubleshoot the problem, but you'll never know it because you're not logging the exception anywhere.
pranav8265 3-May-16 3:18am    
this is sample code that i am using in my project .I am logging the error in a text file.After few successful response the application just hangs and i get the following error.

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


After 10 or 15 mins the application again starts working again.I am not get what is the problem.
Jochen Arndt 4-May-16 2:45am    
You are getting a timeout when trying to read.
So the problem is probably not at the client site but at the server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900