public void StartClient(string hostIp, int hostPort) { // Connect to a remote device. try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". IPHostEntry ipHostInfo = Dns.GetHostEntry(hostIp); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, hostPort); // Create a TCP/IP socket. client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); TheMsg msg = new TheMsg(); msg.Name = "apple"; msg.Num = 1; MessageText.SetMessage("Send protocol buffer data : " + msg.ToString()); byte[] byteData; using (MemoryStream ms = new MemoryStream()) { msg.WriteTo(ms); byteData = ms.ToArray(); } // Send test data to the remote device. Send(byteData); sendDone.WaitOne(); // Receive the response from the remote device. Receive(client); receiveDone.WaitOne(); // Write the response to the console. //Console.WriteLine("Response received : {0}", response); MessageText.SetMessage("Response received : " + response); } catch (Exception e) { //Console.WriteLine(e.ToString()); MessageText.SetMessage(e.ToString()); } }
private void _TestProtobuff2() { //1. new a message. TheMsg message_1 = new TheMsg(); message_1.name = "steve jobs"; message_1.num = 20111005; Debug.Log("message_1: " + message_1.ToString()); string filePath = "C:\\D\\message.txt"; { //2. write byte buffer to file. ProtobufSerilize.Serialize <TheMsg>(message_1, filePath); } { //3. read content. TheMsg message_2 = ProtobufSerilize.DeSerialize <TheMsg>(filePath); Debug.Log("message_2: " + message_2.ToString()); } }
private void _TestProtobuff() { //1. new a message. TheMsg message_1 = new TheMsg(); message_1.Name = "steve jobs"; message_1.Num = 19550224; Debug.Log("message1: " + message_1.ToString()); //2. write byte buffer to file. byte[] byte_buffer; using (MemoryStream memoryStream = new MemoryStream()) { message_1.WriteTo(memoryStream); byte_buffer = memoryStream.ToArray(); } //3. read message from byte buffer. TheMsg message_2 = TheMsg.Parser.ParseFrom(byte_buffer); Debug.Log("message2: " + message_2.ToString()); }