bool ExecuteServerCommand(C2ServerMessage Command = C2ServerMessage.GET_CONFIG_DATA) { try { // Creation TCP/IP Socket using Socket Class Costructor Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { // Connect Socket to the remote endpoint using method Connect() sender.Connect(localEndPoint); //Debug.Log(string.Format("Socket connected to -> {0} ", sender.RemoteEndPoint.ToString() )); // Creation of messagge that we will send to Server byte[] txBuffer = { (byte)Command }; // TODO APPEND COMMAND PARAMETERS int byteSent = sender.Send(txBuffer); // Data buffer byte[] rxBuffer = new byte[2048]; // We receive the messagge using the method Receive(). This method returns number of bytes received, that we'll use to convert them to string int byteRecv = sender.Receive(rxBuffer); byte[] rxData = new byte[byteRecv]; Array.Copy(rxBuffer, rxData, byteRecv); //Debug.Log(_Util.DumpHex(rxData) ); _C2Data = _Util.DeSerializedConfigData(rxData); // Close Socket using the method Close() sender.Shutdown(SocketShutdown.Both); sender.Close(); return(true); } // Manage of Socket's Exceptions catch (System.ArgumentNullException ane) { Debug.Log(string.Format("ArgumentNullException : {0}", ane.ToString())); } catch (SocketException se) { Debug.Log(string.Format("SocketException : {0}", se.ToString())); } catch (System.Exception e) { Debug.Log(string.Format("Unexpected exception : {0}", e.ToString())); } } catch (System.Exception e) { Debug.Log(e.ToString()); } return(false); }
public byte[] SerializedConfigData(CSAFE_Fitness.CSAFE_ConfigurationData Data) { IFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, Data); return(stream.ToArray()); } }