Esempio n. 1
0
 public void SayToClient(string msg)
 {
     try
     {
         foreach (TTransport trans in TransportCollection)
         {
             TBinaryProtocol protocol = new TBinaryProtocol(trans);
             HelloWorldBidirectionService.Client client = new HelloWorldBidirectionService.Client(protocol);
             //Thread.Sleep(1000);
             client.SayHello(msg);
             Console.WriteLine("发给了客户端哟");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
        public void ConnectAndListern(int port, string ip = "127.0.0.1")
        {
            //Tsocket: TCP/IP Socket接口
            TSocket tSocket = new TSocket(ip, port);
            //消息结构协议
            TProtocol protocol = new TBinaryProtocol(tSocket);

            try
            {
                if (client == null)
                {
                    client = new global::HelloWorldBidirectionService.Client(protocol);
                    tSocket.Open();        //建立连接
                    StartListern(tSocket); //启动监听线程
                }
                //cli.SayHello(msg);
                //tSocket.Close();
                //cli.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }