public void StartClient(ClientMessage clientMessage) { byte[] buffer = new byte[0x8000]; try { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x2af8); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", socket.RemoteEndPoint.ToString()); byte[] bytes = Encoding.UTF8.GetBytes("{\"Name\":\"Martin\",\"messages\":[]}"); socket.Send(bytes); socket.Shutdown(SocketShutdown.Send); int count = socket.Receive(buffer); Console.WriteLine("Server response = {0}", Encoding.UTF8.GetString(buffer, 0, count)); socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException: {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException: {0}", se.ToString()); } catch (Exception e) { Console.WriteLine("Unhandled exception: " + e.ToString()); } } catch (Exception e) { Console.WriteLine("Unhandled exception: " + e.ToString()); } }
private static void NewMessage() { BaseMessage message = new BaseMessage(); message.Time = DateTime.Now.ToString(); message.Sender = settings.Sender; FormatText(new string[] { "Choose Receiver" }); int key = contacts.returnContactKey(contacts); if (contacts.Get(key).Server) { ClientMessage clientMessage = new ClientMessage(); clientMessage.Name = message.Receiver; clientMessage.messages = new BaseMessage[messages.Count()]; for (int i = 0; i < messages.Count(); i++) clientMessage.messages[i] = messages.Get(i); Client client = new Client(); client.StartClient(clientMessage); Console.ReadKey(); } else { message.Receiver = contacts.Get(key).Name; FormatText(new string[] { "Input the Subject" }); message.Subject = Console.ReadLine(); FormatText(new string[] { "Input the Message" }); message.Message = Console.ReadLine(); message.Read = false; FormatText(new string[] { "Time: " + message.Time, "Sender: " + message.Sender, "Receiver: " + message.Receiver, "Subject: " + message.Subject, "Message: " + message.Message }); messages.Add(message); msgCount++; Console.ReadKey(true); } }