Re: multithreaded tcp/ip monitoring application
- From: Punit Kaur <PunitKaur@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jul 2007 06:54:01 -0700
The second method that I adopted looks like this:
private void btnConnect_Click(object sender, EventArgs e)
{
IPAddr = tbxIPAddress.Text;
//Validate IP Address
if (!IsValidIPAddress(IPAddr))
{
MessageBox.Show("Invalid IP Address", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Storing the IP Address in file for next time
// FileStream file = new FileStream("C:\\IPAddress.txt",
FileMode.Create, FileAccess.Write);
// StreamWriter sw = new StreamWriter(file);
// sw.Write(IPAddr);
// sw.Close();
//Connecting to the Network Recorder
this.lblConnStatus.Text = "Trying to Establish Connection...";
Application.DoEvents();
System.Threading.Thread.Sleep(3000);
port = 1008;
try
{
server = new TcpClient(IPAddr, port);
state = new StateObject();
state.workSocket = server.Client;
server.Client.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0, new AsyncCallback((new
RecorderMonitor()).OnReceive), state);
status = true;
this.Invoke(this.m_DelegateSetAppParam);
}
catch (SocketException)
{
status = false;
RetryConnect(this);
//return;
}
}
RetryConnect functions looks like this
public void RetryConnect(RecorderMonitor m_Form)
{
//this.lblConnStatus.Text = "Retrying to Establish Connection...";
Application.DoEvents();
System.Threading.Thread.Sleep(5000);
try
{
server = new TcpClient(IPAddr, port);
state = new StateObject();
state.workSocket = server.Client;
server.Client.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0, new AsyncCallback((new
RecorderMonitor()).OnReceive), state);
}
catch (SocketException)
{
status = false;
if (this.lblConnStatus.InvokeRequired)
{
RetryConnectCallBack d = new
RetryConnectCallBack(RetryConnect);
this.Invoke(d, new object[] { m_Form });
}
else
{
m_Form.lblConnStatus.Text = "Network Recorder is down";
// m_Form.btnConnect.BackColor = System.Drawing.Color.Red;
Application.DoEvents();
}
return;
}
In the btnconnect event... I just tryt o connect to the remote application
only once and repeatedly try connecting to it unless the connection is
established. I am not having any trouble in doing that. The trouble comes
when I am connected to the server and start receiving the Data using the
Begin Receive which calls the following function:
public void OnReceive(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead;
if (handler.Connected)
{
// Read data from the client socket.
try
{
bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data
received so far.
state.sb.Remove(0, state.sb.Length);
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Display Text in Rich Text Box
// content = state.sb.ToString();
// Console.WriteLine(content);
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
}
}
In the above code, it repeatedly keeps calling BeginReceive to ensure it is
continuously r eceiving data from server... But now I need to handle a
situation where if the server goes down.. the catch block tries to retry
connecting as it was doing in the previous case. Here whenever I call
RetryConnect function, it says invalid operationexception after executing
that function. Basicallu its not able to update the UI ... may be bcause in
this case RetryConnect belongs to a different thread. How should update the
UI just like I was doing before from btnConnect event.. in this case. I even
tried to declare RetryConnect as a Delegate but it doesnt work.
Please suggest.
.
- References:
- Re: multithreaded tcp/ip monitoring application
- From: Peter Duniho
- Re: multithreaded tcp/ip monitoring application
- From: Punit Kaur
- Re: multithreaded tcp/ip monitoring application
- Prev by Date: Re: c# 2 ftpwebrequest list -r
- Next by Date: Re: execute reader
- Previous by thread: Re: multithreaded tcp/ip monitoring application
- Next by thread: Re: multithreaded tcp/ip monitoring application
- Index(es):