public Server_v2(int listeningPort, int sendingPort) { isActive = true; defaultUDPListeningPort = listeningPort; defaultUDPSendingPort = sendingPort; //defaultIpAddress = defaultIpAddress.Trim(); IPSendTo = IPAddress.Parse(defaultIpAddress); ipep = new IPEndPoint(IPAddress.Any, defaultUDPListeningPort); #if CONSOLE_DEBUG Console.WriteLine("Phase 1 \n"); FormCustomConsole.AddText("Phase 1" + Environment.NewLine); #endif //ipep = new IPEndPoint(IPSendTo, defaultUDPListeningPort); socket = new UdpClient(ipep); #if CONSOLE_DEBUG Console.WriteLine("Phase 2 \n"); FormCustomConsole.AddText("Phase 2" + Environment.NewLine); #endif sender = new IPEndPoint(IPSendTo, defaultUDPSendingPort); #if CONSOLE_DEBUG Console.WriteLine("Phase 3 \n"); FormCustomConsole.AddText("Phase 3" + Environment.NewLine); #endif UDPThread = new Thread(new ThreadStart(StartReceive)); UDPThread.IsBackground = true; UDPThread.Start(); } // End constructor
private void button2_Click(object sender, EventArgs e) // Start/Stop button { button2.Enabled = false; if (!ValidateInput()) // Port number formatting { return; } if (Server_v2.isActive) // if server is active, kill it, otherwise start it { Server_v2.Kill = true; Console.WriteLine("Phase Killing server \n"); FormCustomConsole.AddText("Phase Killing server" + Environment.NewLine); button2.Enabled = true; button3.Enabled = false; // Disable commands button, UDP server is down serverIsStoped = true; return; } serverIsStoped = false; // If we get to here, server is manualy resumed; try { startUDPServer2(); } catch (Exception) { SetStatusText("Status: Error"); button3.Enabled = false; // Disable commands button, UDP server is down } button2.Enabled = true; //Save new Port values to settings Properties.Settings.Default.FormMainListeningPort = textBox2.Text; Properties.Settings.Default.FormMainSendingPort = textBox3.Text; Properties.Settings.Default.Save(); // Make them persistant }
} // End constructor public void StartReceive() { try { using (socket) { formMain.SetStatusText("Status: Running"); formMain.ChangeButtonState(true); while (true) { byte[] data = new byte[1024]; if (socket.Available > 0) { IPEndPoint tempasd = new IPEndPoint(IPAddress.Any, 0); data = socket.Receive(ref tempasd); if (data.GetLength(0) == 4) // Is this get status response { MessageProcesser.ProcessStatusResponse(data); } else // For now it is only received Alarm { string xmlData = System.Text.Encoding.Default.GetString(data); senderInfo si = new senderInfo(); // Assign default value si.errorCode = "none"; si = MessageProcesserXML.ProcessAlarmResponseXML(xmlData); if (si.error) { continue; // Something went wrong, abort } si.trueIPAddress = HashtableAndDatabaseClass.FetchIPFromMac(si.mac); if (si.trueIPAddress == null) { continue; // Hashtable doesn't countain that MAC, abort everything (writing in log and display too) } si.locationID = HashtableAndDatabaseClass.FetchLocationIDFromMac(si.mac); // we know that we have that key, so dont need to check if (!formMain.GetDebugStatus()) { // If debug checkbox is not checked try to write to database try { HashtableAndDatabaseClass.InsertMessageInDatabase(si, Storage.ReadConnectionString()); formMain.SetDBStatusText("DB Status: OK"); } catch (Exception) { //WriteOnDisplayAndStorage(si); WriteOnDisplayAndStorageDBDown(si); formMain.SetDBStatusText("DB Status: DB is down"); // Database is down, update status continue; } } // Write log and to display WriteOnDisplayAndStorage(si); // Will use information stored on si structure // Form responce byte[] finalMessage = Encoding.ASCII.GetBytes("ACK " + si.seq); si.trueIPAddress = si.trueIPAddress.Trim(); // Some people use whitespaces infront of IP :( try { sender = new IPEndPoint(IPAddress.Parse(si.trueIPAddress), defaultUDPSendingPort); socket.Send(finalMessage, finalMessage.Length, sender); } catch { Console.WriteLine("IP from database is corrupted \n"); FormCustomConsole.AddText("IP from database is corrupted" + Environment.NewLine); } } // End else from get data length } // End if (socket.Available > 0) if (Kill == true) { if (socket.Available > 0) { Console.WriteLine("More data needs to be read first \n"); continue; } Kill = false; Console.WriteLine("Phase Exit \n"); FormCustomConsole.AddText("Phase Exit" + Environment.NewLine); formMain.SetStatusText("Status: Stopped"); isActive = false; return; } Thread.Sleep(10); } // While } // End using(socket) } // End try catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine("Something went wrong with server \n"); FormCustomConsole.AddText("Something went wrong with server" + Environment.NewLine); formMain.SetStatusText("Status: Error"); isActive = false; } } // StartReceive