Endless loop somehow not endless!
I have a program that kind of acts as a server. It works for a while, but after some time has passed, it just...stops. It uses an endless loop, but it almost seems as if the program just freezes and stops doing anything. It doesn't even stop "responding," it just stops doing anything. I don't know when exactly it does this, either, the intervals seem random. When I end task the program, and try to start it up again, it says that "Address is in use" and I have to reboot the computer to get it to work.
while (program_run) {
code here
}
I'm using the Mono C# Just-In-Time Compiler, and here's my complete code, I'd really like to know if I made an error in it that would cause this!
Code:
using System;
using System.Net;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using System.IO;
public static class PsiSystem {
public static string SP;
public static int P;
public static string SFP;
public static string LD;
public static void GrabData() {
LD=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
LD=LD.Replace("file:///","");
string Path=LD+"config.ini";
Console.WriteLine(Path);
if (File.Exists(LD+"config.ini")) {
StreamReader sr = new StreamReader(LD+"config.ini");
string ini = sr.ReadToEnd();
ini=ini.Replace(Convert.ToString(Convert.ToChar(13)),"");
string[] Lines = ini.Split(Convert.ToChar(Convert.ToChar(10)));
string[] Elements;
for (int i=0;i<Lines.Length;i++) {
Elements=Lines[i].Split(Convert.ToChar("="));
if (Elements[0]=="ScriptPath") {
SP=Elements[1];
}
if (Elements[0]=="Port") {
P=Convert.ToInt32(Elements[1]);
}
if (Elements[0]=="ScriptFilesPath") {
SFP=Elements[1];
}
}
} else {
Console.WriteLine("config.ini not found, using defaults.");
SP="C:\\Program Files\\PHP\\php.exe";
P=23;
SFP=LD+"\\scripts";
}
Console.WriteLine("\n\nScriptPath: "+SP);
Console.WriteLine("Port: "+P);
Console.WriteLine("ScriptFilesPath: "+SFP);
if (File.Exists(SP)) {
Console.WriteLine("\n\nScript file exists.");
} else {
Console.WriteLine("\n\nScript file does NOT exist! SPLength:"+SP.Length);
}
}
public static void ScriptRun(string ScriptFileName, string ScriptEvent, string Data) {
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = SP;
p.StartInfo.Arguments = SFP+ScriptFileName+".php "+ScriptEvent+" "+Data.Replace(" ","");
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Dispose();
Console.WriteLine(output);
DoCommands(output);
}
public static void DoCommands(string Commands) {
string[] Lines = Commands.Split('');
string[] Params;
int cid;int s;
for (int i=1;i<Lines.Length;i++) {
Params=Lines[i].Split(' ');
if (Params[0]=="0") {
cid=Convert.ToInt32(Params[1]);
if (RealClient.socket[cid].Connected) {
byte[] Data = Encoding.UTF8.GetBytes(Params[2].Replace(""," "));
Console.WriteLine("Sending to {0}: {1}",cid,Data);
RealClient.socket[cid].Send(Data);
}
} else if (Params[0]=="1") {
Console.WriteLine("Breaking connection to {0}",Params[1]);
s=Convert.ToInt32(Params[1]);
RealClient.socket[s].Close();
RealClient.LoggedOn[s]=false;
} else if (Params[0]=="2") {
Console.WriteLine("Message from script: {0}",Params[1].Replace(""," "));
} else {
Console.WriteLine("Line {0} has an incorrect syntax!",i);
}
}
}
}
public static class Psi {
public static void Main() {
PsiSystem.GrabData();
RealClient.Begin();
}
}
public static class Time {
public static int oSecond = 0;
public static int oMinute = 0;
public static int oHour = 0;
public static void Check() {
DateTime d = DateTime.Now;
if (d.Second!=oSecond) {
if (d.Minute!=oMinute) {
if (d.Hour!=oHour) {
oHour=d.Hour;
PsiSystem.ScriptRun("hour",Convert.ToString(oHour),"");
}
oMinute=d.Minute;
Console.WriteLine("It is now: {0}:{1}:0",oHour,oMinute);
PsiSystem.ScriptRun("minute",Convert.ToString(oMinute),"");
}
oSecond=d.Second;
PsiSystem.ScriptRun("second",Convert.ToString(oSecond),"");
}
}
}
public static class RealClient {
public static Socket[] socket;
public static bool[] LoggedOn;
public static string[] IP;
public static void Begin() {
int port = PsiSystem.P;
TcpListener listener = new TcpListener(port);
listener.Start();
Console.WriteLine("\nListening on port {0}.", port);
bool program_run = true;
int i = 0;
int stream_len;
byte[] bytes = new byte[1024];
socket=new Socket[300];
LoggedOn=new bool[300];
IP=new string[300];
for (i=0;i<300;i++) {
LoggedOn[i]=false;
}
string dat=" ";
Console.WriteLine("\nPsi Server Started!\n");
dat="****cock";
PsiSystem.ScriptRun("startup","","");
while (program_run) {
Time.Check();
if (listener.Pending()) {
i=0;
while (LoggedOn[i]) {
i++;
}
socket[i] = listener.AcceptSocket();
Console.WriteLine("Connection Pending...");
LoggedOn[i] = true;
IP[i]=((IPEndPoint)socket[i].RemoteEndPoint).Address.ToString();
Console.WriteLine ("Connected to: {0} on UID {1}",IP[i],i);
PsiSystem.ScriptRun("connect",Convert.ToString(i),IP[i]);
}
i=0;
while (i<=299) {
if (LoggedOn[i]) {
if (socket[i].Available>0) {
stream_len = socket[i].Receive(bytes);
dat = Encoding.ASCII.GetString(bytes, 0, stream_len);
Console.WriteLine("Received Data: {0}", dat);
PsiSystem.ScriptRun("send",Convert.ToString(i),dat);
}
if (!socket[i].Connected) {
IP[i]="";
LoggedOn[i]=false;
PsiSystem.ScriptRun("disconnect",Convert.ToString(i),"");
Console.WriteLine("Lost connection to {0}.",IP[i]);
}
}
i++;
}
}
}
}
Re: Endless loop somehow not endless!
Add some code to your app to keep a log so you know what it's been up to and where it stopped doing what you expect it to.
Re: Endless loop somehow not endless!
I've actually been doing this.
You see, it reports when the minute has passed, when a connection has arrived, etc..
It will work fine for a while, then suddenly stop!