/** * This method sends to the client the port that must be used * to contact the server. */ private void sendPort(int port) { string myString = port.ToString() + '\0'; byte[] toSend = System.Text.Encoding.UTF8.GetBytes(myString); s.Send(toSend, toSend.Length, SocketFlags.None); }
private void processImage(Bitmap current, MySocket socket) { //abilito il calcolo motionDetector.MotionLevelCalculation = true; if (lastFrame != null) { lastFrame.Dispose(); } lastFrame = (System.Drawing.Bitmap)current.Clone(); // apply motion detector if (motionDetector != null) { motionDetector.ProcessFrame(ref lastFrame); // check motion level if (motionDetector.MotionLevel >= alarmLevel) { Console.WriteLine("MotionLevel:" + motionDetector.MotionLevel); //funzione che esegue operazioni quando scatta allarme byte[] toSend = System.Text.Encoding.UTF8.GetBytes(ALARM); socket.Send(toSend, toSend.Length, SocketFlags.None); socket.Close(); Console.WriteLine("Allarme Scattato!\n"); isdead = true; keepalive = false; String mac = myMac.Remove(myMac.Length - 1); mDatabase.removeClient(mac); sendMail("Alarm", "The client" + mac + " has detected a sospicious motion, the alarm was thrown", MotionDetector4.lastimage); } else { byte[] toSend = System.Text.Encoding.UTF8.GetBytes(NOALARM); socket.Send(toSend, toSend.Length, SocketFlags.None); socket.Close(); } } return; }
public void start() { Socket handlerClient; IPAddress ip = IPAddress.Parse(Constants.SERVER_IP_ADDR); IPEndPoint localEndPoint = new IPEndPoint(ip, porta); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Boolean ok; mDatabase.OpenConnect(); try { Thread keepalive = new Thread(() => checkKeepAlive()); keepalive.Start(); listener.Bind(localEndPoint); listener.Listen(2); // Start listening for connections. } catch (Exception e) { Console.WriteLine("Unexpected Error"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); mDatabase.CloseConnect(); return; } while (!isdead) { try { Console.WriteLine("Waiting for a connection..." + "from client: " + this.myMac + "on port:" + porta); Console.WriteLine(""); // Program is suspended while waiting for an incoming connection. handlerClient = listener.Accept(); clientsock = new MySocket(handlerClient); String cmd = receiveString(clientsock.s); switch (cmd) { case "keep\0": Console.WriteLine(cmd); String macaddr2 = clientsock.receiveString(); String time = clientsock.receiveString(); if (macaddr2.Equals(this.myMac)) { Console.WriteLine("Received keepalive from: " + macaddr2 + "at: " + time); lastTime = Int64.Parse(time); } byte[] toSend = System.Text.Encoding.UTF8.GetBytes(OK); clientsock.Send(toSend, toSend.Length, SocketFlags.None); clientsock.Close(); break; case "firstImage\0": Console.WriteLine("Received first image from: " + myMac); System.Drawing.Bitmap current1 = receiveFile(lungImage, clientsock.s); long time1 = CurrentTimeMillis(); String strValue = myMac; strValue = Regex.Replace(strValue, @"-", ""); strValue = strValue.Remove(strValue.Length - 1); String pictureFolderName = strValue + "\\firstimage" + time1 + ".jpg"; String picturePath = Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + pictureFolderName; String relativePath = Constants.IMAGES_FOLDER + pictureFolderName; bool exists = System.IO.Directory.Exists(Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + "\\" + strValue); if (!exists) { System.IO.Directory.CreateDirectory(Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + "\\" + strValue); } try { current1.Save(picturePath); String path = relativePath.Replace("\\", "/"); ok = mDatabase.insertSuspiciousPicturePath(myMac, time1, @"./" + path); if (!ok) { Console.WriteLine("Error: Impossible to store picture: " + picturePath + " on the database!\n"); } } catch (Exception ex) { Console.WriteLine("Error! Impossible to store the received picture! Exception: " + ex.ToString() + "\n"); } Thread thread = new Thread(() => processImage(current1, clientsock)); thread.Start(); break; case "manageImage\0": Console.WriteLine("Received image from: " + myMac); System.Drawing.Bitmap current = receiveFile(lungImage, clientsock.s); Thread thread2 = new Thread(() => processImage(current, clientsock)); thread2.Start(); break; case "unbind\0": Console.WriteLine("Received unBind from: " + myMac); isdead = true; keepalive = false; String mac = myMac.Remove(myMac.Length - 1); mDatabase.removeClient(mac); break; default: Console.WriteLine("Error : Unknokwn command"); byte[] toSend2 = System.Text.Encoding.UTF8.GetBytes(UNKNOW_COMMND); clientsock.Send(toSend2, toSend2.Length, SocketFlags.None); clientsock.Close(); break; } } catch (Exception e) { Console.WriteLine("Unexpected Error"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } finally { mDatabase.CloseConnect(); } } }