// create protocol to send to server static void createProtocol(CreateProtocol protocolObject) { // Retrive Date and time DateTime dateTime = new DateTime(); dateTime = DateTime.Now; String S_DateTime = dateTime.ToString(); // Get my own IP-ADRESS automatically string hostName = Dns.GetHostName(); // Retrive the Name of HOST string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString(); // Add data to protcol object protocolObject.date = S_DateTime; protocolObject.senderIP = myIP; protocolObject.nickname = "Linda"; }
// send messages to server static void SendThread(string serverIP, TcpClient client, NetworkStream stream) { // protocol object, used to send right protocol CreateProtocol protocolObject = new CreateProtocol(); while (true) { // read from console Console.Write("'linda' says > "); string message = Console.ReadLine(); // clear console if (message == "clear") { Console.Clear(); } else { // Add data to protcol object createProtocol(protocolObject); protocolObject.recipientIP = serverIP; protocolObject.message = message; // Create JSON format of protocol object string protocol = JsonConvert.SerializeObject(protocolObject); try { // Encoding int byteCount = Encoding.UTF8.GetByteCount(protocol); byte[] sendDataa = new byte[byteCount]; sendDataa = Encoding.UTF8.GetBytes(protocol); // write message to stream stream.Write(sendDataa, 0, sendDataa.Length); // flush when done stream.Flush(); } catch (Exception) { Console.WriteLine("Message could not be sent.."); } } } }