Server not receiving write() here and there

M

Michael Kjeldsen

Hi guys,

I have a hard time figuring out weather it's the servers fault, or a
bug in my application somewhere. If I look in Wireshark it looks as if
it's sending the data to the server; but how can I be sure it's
received?

In regards to the server's fault.. it probably isn't, as it's a Java
app running in the browser (I Just make my own client for it) and I
can execute the commands just fine through the browser all the time.
It also works in my app, but after 2-3 commands to the server (still
connected), it stops working - like something is blocking the actual
sending.

Hope for some guidance. Maybe the loop has something to do with it? If
I restart the application, the commands work fine again, so it's after
some time it stops working. Actually had this bug for over 5 years
now; but just compiled the app again and it's still a problem :)

Thanks for reading.

I should probably mention that inside the HandleIncomingMessages()
function I have this thread running sending a keep-alive to the server
(character "g").

// Create send-alive thread.
Thread aliveThread = new Thread((ThreadStart)delegate
{
do
{
Connection.Send("g");
Thread.Sleep(100000);
}
while (true);
});

aliveThread.Name = "Send-alive Thread";
aliveThread.Start();
aliveThread.IsBackground = true;


Here's some snippets of how it works.

client = new TcpClient(server, port);
//client.NoDelay = true; // both has same result

using (stream = client.GetStream())
using (reader = new StreamReader(stream, Encoding.ASCII))
{
do
{
try
{
// Read the stream into the buffer.
while ((readBuffer = reader.ReadLine()) != null)
{
ConnectionHelper.HandleIncomingMessage(readBuffer);
}
}
catch (Exception E)
{
WriteLine("CreateConnection() Exception: " + E.Message);
WriteLine("CreateConnection() Exception: StackTrace: " +
E.StackTrace);

// Exit on error.
Environment.Exit(1);
}
} // do
while (stream.DataAvailable);

// Clean up.
reader.Close();
stream.Close();
client.Close();

WriteLine("Disconnected.");
}

HandleIncomingMessage() just parses the data and sends commands to the
Send() function below.


internal static void Send(string message)
{
if (stream.CanWrite)
{
byte[] byteData =
Encoding.ASCII.GetBytes(message +
Environment.NewLine);

Encoding enc = Encoding.ASCII;
WriteLine("Send(): " + enc.GetString(byteData));

// Send <message>
stream.Write(byteData, 0, byteData.Length);
stream.Flush();
return;
}

WriteLine("Send(): Exception: Cannot write to stream!");
}

As I wrote, the first couple of requests goes through just fine.. but
after some socket activity, I cannot get the write() to do anything.
Wireshark tells me it's being sent, but somehow the server is not
receiving it.

Thanks for reading!
 
A

Arne Vajhøj

I have a hard time figuring out weather it's the servers fault, or a
bug in my application somewhere. If I look in Wireshark it looks as if
it's sending the data to the server; but how can I be sure it's
received?

In regards to the server's fault.. it probably isn't, as it's a Java
app running in the browser (I Just make my own client for it) and I
can execute the commands just fine through the browser all the time.
It also works in my app, but after 2-3 commands to the server (still
connected), it stops working - like something is blocking the actual
sending.

Hope for some guidance. Maybe the loop has something to do with it? If
I restart the application, the commands work fine again, so it's after
some time it stops working. Actually had this bug for over 5 years
now; but just compiled the app again and it's still a problem :)

Thanks for reading.

I should probably mention that inside the HandleIncomingMessages()
function I have this thread running sending a keep-alive to the server
(character "g").

// Create send-alive thread.
Thread aliveThread = new Thread((ThreadStart)delegate
{
do
{
Connection.Send("g");

Does it flush?
Thread.Sleep(100000);
}
while (true);
});

aliveThread.Name = "Send-alive Thread";
aliveThread.Start();
aliveThread.IsBackground = true;


Here's some snippets of how it works.

client = new TcpClient(server, port);
//client.NoDelay = true; // both has same result

using (stream = client.GetStream())
using (reader = new StreamReader(stream, Encoding.ASCII))
ASCII??

{
do
{
try
{
// Read the stream into the buffer.
while ((readBuffer = reader.ReadLine()) != null)
{
ConnectionHelper.HandleIncomingMessage(readBuffer);
}
}
catch (Exception E)
{
WriteLine("CreateConnection() Exception: " + E.Message);
WriteLine("CreateConnection() Exception: StackTrace: " +
E.StackTrace);

// Exit on error.
Environment.Exit(1);
}
} // do
while (stream.DataAvailable);

// Clean up.
reader.Close();
stream.Close();
client.Close();

WriteLine("Disconnected.");
}

HandleIncomingMessage() just parses the data and sends commands to the
Send() function below.


internal static void Send(string message)
{
if (stream.CanWrite)
{
byte[] byteData =
Encoding.ASCII.GetBytes(message +
Environment.NewLine);

Encoding enc = Encoding.ASCII;
WriteLine("Send(): " + enc.GetString(byteData));

// Send<message>
stream.Write(byteData, 0, byteData.Length);
stream.Flush();
Flush!

return;
}

WriteLine("Send(): Exception: Cannot write to stream!");
}

If you use StreamReader for reading then why not StreamWriter for
writing?
As I wrote, the first couple of requests goes through just fine.. but
after some socket activity, I cannot get the write() to do anything.
Wireshark tells me it's being sent, but somehow the server is not
receiving it.

I would look for error messages at the server.

Maybe you have misunderstood something in the protocol and
the server simply drops everything after detecting that.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top