i have connection to the server. After connected successfully, I can see the connection via CMD netstat command netstat -a -n. But after about 21 seconds, the connection changing from ESTABLISHED to CLOSE_WAIT . I want to the connection always is ESTABLISHED . How can i do it ?
This is from netstat command, the result in the first time and second time.
TCP 192.168.10.12:50148 10.10.10.120:3000 ESTABLISHED
TCP 192.168.10.12:50148 10.10.10.120:3000 CLOSE_WAIT
This is my snipped code in C#
private void _connect()
{
var IpEndPoint = new IPEndPoint(IPAddress.Parse("10.10.10.120"), 3000);
var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
client.SendTimeout = 60;
client.ReceiveTimeout = 60;
try
{
client.Connect(IpEndPoint);
int byteCount = 0;
byte[] bytes = new byte[256];
while (true)
{
byteCount = client.Receive(bytes);
if (byteCount == 0)
{
byte[] data_send = Encoding.ASCII.GetBytes("Hi, dummy data");
client.Send(data_send);
}
else
{
byte[] byte_rec = new byte[byteCount];
Buffer.BlockCopy(bytes, 0, byte_rec, 0, byteCount);
txt_log.Text = Encoding.ASCII.GetString(byte_rec);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I try debug more time, but it only can be run few seconds, after that the connection will be changed from ESTABLISHED to CLOSE_WAIT.
So far, I found some option and enhancement my code. However, when I debug, I can see stream with properties DataAvailable is true at the first time loop. After that, the second time loop, it's always is false. If don't put break point and run with this code, it's can keep connection about more than 10min. But when I put break point at this line numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);, it only run at the first time loop, without next time because stream is false. The time delay when run first time to second time loop about 52s. After that, the connection is break. I get this exception : 'Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.' . Can you tell me why stream changed from true to false after first time loop ?. This way stuck and let my code is wrong.
private async void _connect()
{
var serverEndpoint = new IPEndPoint(IPAddress.Parse(_ip), _port);
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
client.Connect(serverEndpoint);
client.NoDelay = true;
client.SendTimeout = 60000;
client.ReceiveTimeout = 60000;
byte[] myReadBuffer = new byte[1024];
int numberOfBytesRead = 0;
bool _flag = false;
while (true)
{
stream = new NetworkStream(client);
if (stream.CanRead)
{
do
{
if (_flag)
{
numberOfBytesRead = 0;
goto receiveskip;
}
if (numberOfBytesRead == 0)
{
byte[] data_dummy = Encoding.ASCII.GetBytes("dummy_abc");
stream.Write(data_dummy, 0, data_dummy.Length);
stream.Flush();
}
numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
byte[] bytes_rec = new byte[numberOfBytesRead];
Buffer.BlockCopy(myReadBuffer, 0, bytes_rec, 0, numberOfBytesRead);
if(bytes_rec.Length == 14)
{
txt_log.Text += Encoding.ASCII.GetString(bytes_rec);
}
if (bytes_rec.Length > 14)
{
byte[] data_rep = Encoding.ASCII.GetBytes("Ok, thank server");
stream.Write(data_rep, 0, data_rep.Length);
stream.Flush();
_flag = true;
continue;
}
}
while (stream.DataAvailable);
}
else
{
MessageBox.Show("Cannot read from this NetworkStream.");
}
}
}
catch (Exception ex)
{
stream.Close();
client.Close();
MessageBox.Show(ex.Message);
}
}
What I have tried:
I try debug more time, but it only run about 21 seconds, after that the connection will be changed from ESTABLISHED to CLOSE_WAIT