IndexOutOfRangeExceoption while parsing a Netstat process
-
Hi Eddy, Usually it does divide. "UDP 0.0.0.0:445 *:* 4" put through
if (s.Length == 6)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
}becomes s[0]=UDP s[1]=0.0.0.0 s[2]=445 s[3]=* s[4]=* s[5]=0 s[6]=4 if I remove the line "insns.Parameters["@State"].Value = "0";" the output reports "Listening" even though there is no value in the netstat cmd-line output. Sorry this is taking a while, worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine So, I did
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ano";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
Ns.WaitForExit();
for (int i = 0; i < 4; i++)
Ns.StandardOutput.ReadLine();
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
{
Line = Line.Trim();
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(":", " ");
Line = Line.Replace('\r', ' ');
Line = Line.Replace('\n', ' ');
s = Line.Split(' ');
if (s[0] == "TCP")
{
if (s[1] != "0.0.0.0")
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value =CCodeNewbie wrote:
becomes
s[0]=UDP s[1]=0.0.0.0 s[2]=445 s[3]=* s[4]=* s[5]=0 s[6]=4That's strange; if s[] contains six elements, how did we get seven items here?
s[5]
should be 4 IMHO, ands[6]
shouldn't be there.CCodeNewbie wrote:
worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine
Aw, my apologies; I tend to mix them up outside of the IDE. It should list all the words in the array in the output-window of Visual Studio.
CCodeNewbie wrote:
when the timer triggered my CPU ramped up to 99% usage by my app and I had to End-Task it. Nothing was written to the Output or the Event log.
Could it be that it's reading a lot of empty lines?
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))Bastard Programmer from Hell :suss:
-
CCodeNewbie wrote:
pardon my ignorance but why is it ignoring the
if (s.Length == 6)
instruction?
It shouldn't, if the string can be divided into six different words. The line that has been logged, would be broken down like this;
- UDP
- 0.0.0.0
- 445
- *
- *
- 4
You can verify the result using the code below;
foreach (string someWord in s)
{
System.Diagnostics.Debugger.WriteLine("'{0}'", someWord);
}Also, can you post your (updated) code again?
Bastard Programmer from Hell :suss:
tried
if (s.Length == 6)
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = string.Empty;
insns.Parameters["@PID"].Value = s[5];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close()
};same thing - service starts, netstat launches, CPU 99%
-
CCodeNewbie wrote:
becomes
s[0]=UDP s[1]=0.0.0.0 s[2]=445 s[3]=* s[4]=* s[5]=0 s[6]=4That's strange; if s[] contains six elements, how did we get seven items here?
s[5]
should be 4 IMHO, ands[6]
shouldn't be there.CCodeNewbie wrote:
worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine
Aw, my apologies; I tend to mix them up outside of the IDE. It should list all the words in the array in the output-window of Visual Studio.
CCodeNewbie wrote:
when the timer triggered my CPU ramped up to 99% usage by my app and I had to End-Task it. Nothing was written to the Output or the Event log.
Could it be that it's reading a lot of empty lines?
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))Bastard Programmer from Hell :suss:
from a cmd-line netstat -ano you can see that the field usually used by "State" tends to be empty under the UDP protocol unless there is some stream coming in in which case it will show "Established" or whatever. Because I am writing the values to a SQL table I need to keep s[6] in the PID column.
Quote:
Could it be that it's reading a lot of empty lines?
don't think so, usually it works fine until it hits a null/empty value somewhere in the s[] array. What would be ideal is a way of saying: if (s[whatever].isNullorEmpty){s[whatever] = "0";}
-
from a cmd-line netstat -ano you can see that the field usually used by "State" tends to be empty under the UDP protocol unless there is some stream coming in in which case it will show "Established" or whatever. Because I am writing the values to a SQL table I need to keep s[6] in the PID column.
Quote:
Could it be that it's reading a lot of empty lines?
don't think so, usually it works fine until it hits a null/empty value somewhere in the s[] array. What would be ideal is a way of saying: if (s[whatever].isNullorEmpty){s[whatever] = "0";}
Select the menu "Debug" and then "Toggle Breakpoint", and use
F10
to step through your code.CCodeNewbie wrote:
What would be ideal is a way of saying:
Something like this?
string FetchFromArray(string[] source, int idx)
{
if (idx <= source.Length)
return source[idx];
else
return String.Empty;
}
public Form1()
{
InitializeComponent();
string[] s = "Hello world from Mars!".Split(' ');
Text = FetchFromArray(s, 7);
}Bastard Programmer from Hell :suss:
-
Select the menu "Debug" and then "Toggle Breakpoint", and use
F10
to step through your code.CCodeNewbie wrote:
What would be ideal is a way of saying:
Something like this?
string FetchFromArray(string[] source, int idx)
{
if (idx <= source.Length)
return source[idx];
else
return String.Empty;
}
public Form1()
{
InitializeComponent();
string[] s = "Hello world from Mars!".Split(' ');
Text = FetchFromArray(s, 7);
}Bastard Programmer from Hell :suss:
Trying to work out why mu CPU is going mad at the moment, I'll get back to this in a bit..
-
Select the menu "Debug" and then "Toggle Breakpoint", and use
F10
to step through your code.CCodeNewbie wrote:
What would be ideal is a way of saying:
Something like this?
string FetchFromArray(string[] source, int idx)
{
if (idx <= source.Length)
return source[idx];
else
return String.Empty;
}
public Form1()
{
InitializeComponent();
string[] s = "Hello world from Mars!".Split(' ');
Text = FetchFromArray(s, 7);
}Bastard Programmer from Hell :suss:
Sorted out the CPU thing, and so far haven't had any exceptions (after about an hour of testing). So far so good although I do need to ass a couple of modules back into the service. Now for my next problem...http://www.codeproject.com/Messages/4214946/Re-Getting-values-from-tasklist-exe-v-update.aspx[^]
-
Sorted out the CPU thing, and so far haven't had any exceptions (after about an hour of testing). So far so good although I do need to ass a couple of modules back into the service. Now for my next problem...http://www.codeproject.com/Messages/4214946/Re-Getting-values-from-tasklist-exe-v-update.aspx[^]
-
Nice :) I was kind of busy with a very tasty rabbit :D
Bastard Programmer from Hell :suss:
Had a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
-
Had a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
-
CCodeNewbie wrote:
Had a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
Sounds like an equal festive meal - today, life's good :D
Bastard Programmer from Hell :suss:
tell it like it is fellow gastronaut... and thanks for looking in on the other thing that's making me tear my hair out... Why do so may seemingly simple things have to be so complicated...