Click to See Complete Forum and Search --> : issues in network programming


cgtalk
October 25th, 2006, 05:55 AM
are there any consideration i have to take when doing a network program.
i mean for example the lenght of the functions,using many classes,not putting the code in the events ,etc..
becouse i have a problem that i couldn't find a solution to it.
i'm using the SNMP protocol in my program,some portion of my code will try to search for the desired ip address in the network and get some information from it,sometimes this works stable , but sometimes my program hangs up then the debugger displays a message about a deadlock happened. and it also displays about MDA error like

The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

and other errors about marshaling

i couldn't find a solution for this , so i thought may be i should change the way that i program this
here is the code that i'm talking about "toooo long"

private void Buttom_Refresh_Click(object sender, EventArgs e)
{

Label_Status.Text = "fetching SNMP OIDs from the Agent.....Please Wait";
LV_Info.Items.Clear();

snmpObject_Main.Port=(int)MonitorDataSet.Tables[1].Rows[rowNumber_M][4];

snmpObject_Main.ProtocolVersion = (int)MonitorDataSet.Tables[1].Rows[rowNumber_M][6];
snmpObject_Main.Initialize();
if(snmpObject_Main.LastError==0)
{
snmpObject_Main.Open(MonitorDataSet.Tables[1].Rows[rowNumber_M][3].ToString(), MonitorDataSet.Tables[1].Rows[rowNumber_M][5].ToString());

if (snmpObject_Main.LastError == 0)
{

if (TreeView_SensorList.SelectedNode.FullPath.Contains("PC\\"))
{
if (CLB_M.GetItemChecked(0))
{

GeneralInfo();
}
if (CLB_M.GetItemChecked(1))
{

SystemInfo();
}
if (CLB_M.GetItemChecked(2))
{
NetworkingInfo();

}
if (CLB_M.GetItemChecked(3))
{
SNMPInfo();
}
}
}
snmpObject_Main.Close();
}
snmpObject_Main.Clear();
snmpObject_Main.Shutdown();
LV_Info.Visible = true;
Label_Status.Text = "Refreshing operation success.";
//ListViewItem ii = LV_Info.Items[2];

//MessageBox.Show(ii.Text);
//ListViewItem.ListViewSubItemCollection aa = ii.SubItems;
//MessageBox.Show(aa[1].Text.ToString());

}

private void GeneralInfo()
{

ListViewItem item1 = new ListViewItem();
string bb = snmpObject_Main.GetNext("1.3.6.1.2.1.1.1");
item1.Text = "Hardware and software";
item1.BackColor = Color.Red;
item1.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item1);

ListViewItem item2 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.1.4");
item2.Text = "Agent Contact";
item2.BackColor = Color.Red;
item2.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item2);

ListViewItem item3 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.1.5");
item3.Text = "Agent Name";
item3.BackColor = Color.Red;
item3.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item3);

ListViewItem item4 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.1.6");
item4.Text = "Agent Location";
item4.BackColor = Color.Red;
item4.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item4);

}

private void SystemInfo()
{
ListViewItem item1 = new ListViewItem();
string bb = snmpObject_Main.GetNext("1.3.6.1.2.1.25.1.1");
item1.Text = "System up time";

item1.BackColor = Color.Chocolate;
Int32 b = Convert.ToInt32(snmpObject_Main.Get(bb)) / 1000;
Int32 month = b / 2592000;
Int32 buffer1 = b % 2592000;
Int32 day = buffer1 / 86400;
Int32 buffer2 = buffer1 % 86400;
Int32 hour = buffer2 / 3600;
Int32 buffer3 = buffer2 % 3600;
Int32 min = buffer3 / 60;
Int32 sec = buffer3 % 60;
item1.SubItems.Add(string.Format("Month: {0}, Day: {1}, Hour: {2}, Minute(s): {3}, Second(s): {4}", month, day, hour, min, sec));
LV_Info.Items.Add(item1);

ListViewItem item2 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.25.1.6");
item2.Text = "Running processes on the Agent";
item2.BackColor = Color.Chocolate;
item2.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item2);

ListViewItem item3 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.25.2.2");
item3.Text = "Agent RAM";
item3.BackColor = Color.Chocolate;
float RAMresult = Convert.ToSingle(snmpObject_Main.Get(bb)) / 1024;
item3.SubItems.Add(RAMresult.ToString()+" MB");
LV_Info.Items.Add(item3);

ListViewItem item4 = new ListViewItem();
int counter = 0;
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.25.2.3.1.1");
snmpObject_Main.Get(bb);
while (snmpObject_Main.GetType() != snmpObject_Main.asASN_OBJECTIDENTIFIER)
{

counter+=1;
bb = snmpObject_Main.GetNext(bb);
snmpObject_Main.Get(bb);

}
item4.Text = "Number of storage elements";
item4.BackColor = Color.Chocolate;
item4.SubItems.Add(counter.ToString());
LV_Info.Items.Add(item4);

ListViewItem[] items = new ListViewItem[counter];
bb = "1.3.6.1.2.1.25.2.3.1.3";
string bb2 = "1.3.6.1.2.1.25.2.3.1.5";
string bb3 = "1.3.6.1.2.1.25.2.3.1.6";
string bb4 = "1.3.6.1.2.1.25.2.3.1.4";
for (int i = 0; i < counter; i++)
{
bb = snmpObject_Main.GetNext(bb);
bb2 = snmpObject_Main.GetNext(bb2);
bb3 = snmpObject_Main.GetNext(bb3);
bb4 = snmpObject_Main.GetNext(bb4);
float pp4 = Convert.ToInt32(snmpObject_Main.Get(bb4));
float pp2 = Convert.ToInt32(snmpObject_Main.Get(bb2));
float pp5 = pp4 * pp2;
float pp3 = Convert.ToSingle(snmpObject_Main.Get(bb3));
float pp6 = pp4 * pp3;
string giga, mega;
if (pp5 >= 1000000000)
{
giga = "GByte";
pp5 = pp5 / 1000000000;
}
else
if (pp5 >= 1000000)
{
giga = "MByte";
pp5 = pp5 / 1000000;
}
else
if (pp5 >= 1000)
{
giga = "KByte";
pp5 = pp5 / 1000;
}
else
giga = "Byte";
if (pp6 >= 1000000000)
{
mega = "GByte";
pp6 = pp6 / 1000000000;
}
else
if (pp6 >= 1000000)
{
mega = "MByte";
pp6 = pp6 / 1000000;
}
else
if (pp6 >= 1000)
{
mega = "KByte";
pp6 = pp6 / 1000;
}
else
mega = "Byte";
int j = i + 1;
items[i] = new ListViewItem();
items[i].Text = "Storage :" + j.ToString();
items[i].BackColor = Color.Chocolate;
items[i].SubItems.Add(snmpObject_Main.Get(bb).ToString() + " ,Total Size:" + pp5.ToString() + giga + " ,Used:" + pp6.ToString() + mega);
LV_Info.Items.Add(items[i]);

}
}

private void NetworkingInfo()
{

ListViewItem item0 = new ListViewItem();
string bb = snmpObject_Main.GetNext("1.3.6.1.2.1.1.3");
item0.Text = "Network uptime";

item0.BackColor = Color.DarkBlue;
Int32 b = Convert.ToInt32(snmpObject_Main.Get(bb)) / 1000;
Int32 month = b / 2592000;
Int32 buffer1 = b % 2592000;
Int32 day = buffer1 / 86400;
Int32 buffer2 = buffer1 % 86400;
Int32 hour = buffer2 / 3600;
Int32 buffer3 = buffer2 % 3600;
Int32 min = buffer3 / 60;
Int32 sec = buffer3 % 60;
item0.SubItems.Add(string.Format("Month: {0}, Day: {1}, Hour: {2}, Minute(s): {3}, Second(s): {4}", month, day, hour, min, sec));
LV_Info.Items.Add(item0);

ListViewItem item3 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.4.1.77.1.4.1");
item3.Text = "Workgroup";
item3.BackColor = Color.DarkBlue;
item3.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item3);

ListViewItem item1 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.2.1");
int counter = Convert.ToInt16(snmpObject_Main.Get(bb));
item1.Text = "Number of system interfaces";
item1.BackColor = Color.DarkBlue;
item1.SubItems.Add(counter.ToString());
LV_Info.Items.Add(item1);

ListViewItem[] items = new ListViewItem[counter];
bb = "1.3.6.1.2.1.2.2.1.2";//name or kind od interface
string bb2 = "1.3.6.1.2.1.2.2.1.5";//speeed of interface
string packet_in = "1.3.6.1.2.1.2.2.1.10";//number of packet input
string packet_out = "1.3.6.1.2.1.2.2.1.16";//number of packet output
int speed = 0;
string bba = bb,giga;
for (int R = 0; R < counter; R++)
{

bb = bba;
bb = snmpObject_Main.GetNext(bb);
bb2 = snmpObject_Main.GetNext(bb2);
packet_in = snmpObject_Main.GetNext(packet_in);
packet_out = snmpObject_Main.GetNext(packet_out);
speed = Convert.ToInt32(snmpObject_Main.Get(bb2));
if (speed >= 1000000000)
{
giga = "Gbps";
speed = speed / 1000000000;
}
else
if (speed >= 1000000)
{
giga = "Mbps";
speed = speed / 1000000;
}
else
giga = "Kbps";
int j = R + 1;
items[R] = new ListViewItem();
items[R].Text = "Interface :" + j.ToString();
items[R].BackColor = Color.DarkBlue;
items[R].SubItems.Add(snmpObject_Main.Get(bb).ToString() + " ,Speed:" + speed + giga);
bba = bb;
LV_Info.Items.Add(items[R]);

ListViewItem item44 = new ListViewItem();
item44.Text = "Number of packets recieved by the Agent";
item44.BackColor = Color.DarkBlue;
float num_Byte1 = (Convert.ToSingle(snmpObject_Main.Get(packet_in)));
if (num_Byte1 >= 1000000000)
{
giga = "GByte";
num_Byte1 = num_Byte1 / 1000000000;
}
else
if (num_Byte1 >= 1000000)
{
giga = "MByte";
num_Byte1 = num_Byte1 / 1000000;
}
else
if (num_Byte1 >= 1000)
{
giga = "KByte";
num_Byte1 = num_Byte1 / 1000;
}
else
giga = "Byte";

item44.SubItems.Add(num_Byte1.ToString() + giga);
LV_Info.Items.Add(item44);

ListViewItem item99 = new ListViewItem();
item99.Text = "Number of packets transmitted by the Agent";
item99.BackColor = Color.DarkBlue;
float num_Byte = Convert.ToSingle(snmpObject_Main.Get(packet_out));
if (num_Byte >= 1000000000)
{
giga = "GByte";
num_Byte = num_Byte / 1000000000;
}// these statments of if and else to detect the unit o
else
if (num_Byte >= 1000000)
{
giga = "MByte";
num_Byte = num_Byte / 1000000;
}
else
if (num_Byte >= 1000)
{
giga = "KByte";
num_Byte = num_Byte / 1000;
}
else
giga = "Byte";
item99.SubItems.Add(num_Byte.ToString() + giga);
LV_Info.Items.Add(item99);
}

ListViewItem item2 = new ListViewItem();

bb = snmpObject_Main.GetNext("1.3.6.1.4.1.77.1.2.26");
counter = Convert.ToInt16(snmpObject_Main.Get(bb));
item2.Text = "Number of shared resources";
item2.BackColor = Color.DarkBlue;
item2.SubItems.Add(counter.ToString());
LV_Info.Items.Add(item2);

ListViewItem[] items1 = new ListViewItem[counter];
bb = "1.3.6.1.4.1.77.1.2.27.1.1";
bb2 = "1.3.6.1.4.1.77.1.2.27.1.2";
for (int i = 0; i < counter; i++)
{
bb = snmpObject_Main.GetNext(bb);
bb2 = snmpObject_Main.GetNext(bb2);
int j = i + 1;
items1[i] = new ListViewItem();
items1[i].Text = "Resource :" + j.ToString();
items1[i].BackColor = Color.DarkBlue;
items1[i].SubItems.Add("Name: " + snmpObject_Main.Get(bb).ToString() + " ,Path: " + snmpObject_Main.Get(bb2).ToString());
LV_Info.Items.Add(items1[i]);
}

}

private void SNMPInfo()
{
ListViewItem item0 = new ListViewItem();
string bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.1");
item0.Text = "Number of SNMP input packets";
item0.BackColor = Color.DarkGreen;
item0.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item0);

ListViewItem item1 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.2");
item1.Text = "Number of SNMP output packets";
item1.BackColor = Color.DarkGreen;
item1.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item1);

ListViewItem item2 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.15");
item2.Text = "Number of Input GET Requests";
item2.BackColor = Color.DarkGreen;
item2.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item2);

ListViewItem item3 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.16");
item3.Text = "Number of Input GETNEXT Requests";
item3.BackColor = Color.DarkGreen;
item3.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item3);

ListViewItem item4 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.18");
item4.Text = "Number of Input GETRESPONSE Requests";
item4.BackColor = Color.DarkGreen;
item4.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item4);

ListViewItem item5 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.17");
item5.Text = "Number of Input SET Requests";
item5.BackColor = Color.DarkGreen;
item5.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item5);

ListViewItem item6 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.19");
item6.Text = "Number of Accepted Input Traps";
item6.BackColor = Color.DarkGreen;
item6.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item6);

ListViewItem item7 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.25");
item7.Text = "Number of Output GET Requests";
item7.BackColor = Color.DarkGreen;
item7.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item7);

ListViewItem item8 = new ListViewItem();
bb = snmpObject_Main.GetNext("1.3.6.1.2.1.11.26");
item8.Text = "Number of Output GETNEXT Requests";
item8.BackColor = Color.DarkGreen;
item8.SubItems.Add(snmpObject_Main.Get(bb).ToString());
LV_Info.Items.Add(item8);

//there are three similar list view items ,couldn't add it becouse it's too long to post it here
}



i

cgtalk
October 26th, 2006, 08:32 AM
if you know any useful links that can help me with this problem ,please post it here