|
TCP operates a timeout if no packets are sent for some time, because this is usually a sign that the connection has been lost. The time varies but is usually around 30s to a couple of minutes. If you want to maintain a connection despite no user interaction, you need to send 'keep-alive' packets or 'pings' every few seconds (one every 20s should be enough) to keep the connection open. However, remember that if you do this, idle connections will never time out and you can run out of server sockets if too many people leave your app open, so you then should have some client code that logs out and closes the socket after a longer inactive time (30 mins or so).
|
|
|
|
|
BobJanova wrote: TCP operates a timeout if no packets are sent for some time, because this is
usually a sign that the connection has been lost.
TCP does not do that.
BobJanova wrote: you want to maintain a connection despite no user interaction, you need to send
'keep-alive' packets or 'pings' every few seconds (one every 20s should be
enough) to keep the connection open
You do that because the server or firewall requires it - not TCP.
BobJanova wrote: However, remember that if you do this, idle connections will never time
out and you can run out of server sockets if too many people leave your app
open, so you then should have some client code that logs out and closes the
socket after a longer inactive time (30 mins or so).
That of course is an architecture problem. If you server is going to respect keep-alives then it must exist in an enterprise that doesn't expect an unlimited number of client all with persistent connections.
One alternative to that possibility is to close even connections that are in use after a given time period. Or require a connect/message/close protocol like http.
|
|
|
|
|
TCP does not do that.
I thought I'd read that, but if it is not TCP, then it is the operating system, because if you open a TCP connection to your own server and send nothing in either direction, at some point it will get closed.
|
|
|
|
|
BobJanova wrote: I thought I'd read that, but if it is not TCP, then it is the operating system,
because if you open a TCP connection to your own server and send nothing in
either direction, at some point it will get closed.
No it won't.
And it isn't the "OS".
OSes have what is typically called a 'IP Stack' which implements IP/TCP/UPD/ICMP.
And they implement it based on the RFCs associated with those protocols.
Application use an API to access that. And assume that the stack implementation is actually correctly implementing the RFCs.
If you are seeing something close connections then it is as I said: the server, the client or something in between like a firewall.
|
|
|
|
|
bob hi, im sorry im not going through the right channels but i am trying to ask you a question in regards to the game lobby,,,,, please contact me at lb4262002@yahoo.co.uk thanks alan (member name TOPLOADER)
|
|
|
|
|
Start a question thread on the article.
|
|
|
|
|
i am sorry i am a new member and am not sure how to start a new thread on the subject, if i could see a new thread tab i would do it sorry. please be patient with me as i have tried to send you a message 3 times and although we are not talking about why i want to speak to you at least i am speaking to you which is a step forward. please can you giude me to the new thread link, many thanks alan.
|
|
|
|
|
Hi,
I am trying to code an application which reads some data from a NetworkStream, saves it into a buffer byte array and then appending it to a file.
Here is the part I am writing the data from the byte array into the file:
FileStream fs = new FileStream(last_save_location, FileMode.Append, FileAccess.Write);
fs.Write(message, 8, bytesRead - 8);
fs.Close();
fs.Dispose();
Everything seems to function fine. I can write the data from NetworkStream to the buffer byte array and then into the file for a while. However, after some time, I get the following error:
The process cannot access the file 'C:\Users\Main\Desktop\test.bin' because it is being used by another process.
I am quite sure that no other process is using the file and that I close the handle each and every time. What do you think the problem is?
Regards,
Can
|
|
|
|
|
The error message is misleading: when it says "... in use by another process..." it may well be, and often is, the same process, not another one. It would better say "... in use by some process...".
Now where within your app is the code you have shown? what threads are involved here? could your code be re-entered, e.g. would there be some Application.DoEvents() calls involved?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
The code is only called once and it is in a thread. I haven't used the code Application.DoEvents() in any part of the application. I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded. Do you think that it has something to do with this?
|
|
|
|
|
SimpleData wrote: I've used Control.CheckForIllegalCrossThreadCalls = false when the form is
loaded
Why?
|
|
|
|
|
I am accessing the GUI elements within a thread that I've created and I didn't want to work with delegates because I didn't think it would constitute a problem in a minor project like this.
|
|
|
|
|
SimpleData wrote: Control.CheckForIllegalCrossThreadCalls = false
don't. It does not solve a problem, all it does is suppressing the exception that has been added in .NET 2.0 for a reason; and it does not remedy all the bad things that are bound to happen. Please read this[^] and fix your code accordingly.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Thanks. I will make the necessary changes.
By the way, in which way can this be causing my problem?
|
|
|
|
|
WinForm Controls aren't thread safe and get accessed quite frequently by the main thread (responding to user actions, repaint messages, etc), so operating them from some other thread as well as from the main thread sooner or later will cause problems.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
It doesn't cause the problem, but it could very well mask the cause of the problem so you won't see it.
Ciao,
luker
|
|
|
|
|
You can try this :
using (FileStream fs = new FileStream(last_save_location, FileMode.Append, FileAccess.Write))
{
fs.Write(message, 8, bytesRead - 8);
fs.Close();
}
I know nothing , I know nothing ...
|
|
|
|
|
Almost always, this is because the log is being called twice, from different threads, in a shorter period than the file takes to open and close. Synchronise the file calls (that is, put a lock block around them).
|
|
|
|
|
hi
i have two forms form1 and form2 i have two btns in form1 if i clik btn1 of form1 btn2 of form1 turns green and form2 opns and form1 hides and if i clk the btn from form2 form1 opns but it do not save the previous values means btn2 of form1 is not green this time. how can i save the previous value.
thanks
|
|
|
|
|
It sounds like in form2 you are creating a new instance of form1 rather than unhiding the existing instance.
|
|
|
|
|
yes m creating the new instance but how can i unhide the existing instance?
|
|
|
|
|
See my reply below for an alternative to the one already given and reasons why it may be a better way.
|
|
|
|
|
That's easy , have no worries
Form1 Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form2 form2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2(this);
form2.Show();
}
else
{
form2.Show();
}
this.Hide();
button2.BackColor = Color.Green;
}
}
}
Form2 Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form form1;
public Form2(Form form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (form1 == null)
{
form1 = new Form();
form1.Show();
}
else
{
form1.Show();
}
this.Hide();
}
}
}
Form1 Is The Main Form
I know nothing , I know nothing ...
|
|
|
|
|
thanks u hve solved my problm
|
|
|
|
|
You welcome , and don't forget to vote for me
I know nothing , I know nothing ...
|
|
|
|