WebService和winform在同一电脑上,如何实现WebService向winform发送字符串数据?我试了SendMessage,Web不能正确获取WinForm窗口句柄(FindWindow)。
什么方式最简便、快捷?最好有示例代码。

解决方案 »

  1.   

    不知道用Socket可不可以,我不太懂 
      

  2.   

    这个不是反过来了么?成了winform向webservice发送消息了。这个用socket通信最简单了,以后分开部署只要联网也没问题的。贴部分socket通信的代码给你,
    byte[] bytes = new Byte[1024];
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1003);
                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(10);
                    while (true)
                    {
                        Console.WriteLine("Waiting for a connection");
                        Socket handler = listener.Accept();
                        data = null;
                        while (true)
                        {
                            bytes = new byte[1024];
                            int bytesRec = handler.Receive(bytes);
                            data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                            if (data.IndexOf("<EOF>") > -1)
                            {
                                break;
                            }
                        }
                        Console.WriteLine("Text received : {0}", data);
                        byte[] msg = Encoding.ASCII.GetBytes(data);
                        handler.Send(msg);
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
      

  3.   

    api啊 sendmessage或postmessage 应该可以的 你准时写的类或者窗体标题不对.
      

  4.   


    不行,WebService调用sendmassage无效果。窗口句柄已正确获取
      

  5.   

    用过winform向webservice申请数据,没有用过从webservice主动发送数据到winform
      

  6.   

    已经改用Socket,webservice作为客户端,winform作为服务端,单机上调试成功。需求有了些变化,二者不在同一电脑上了。WebService在公网上,winform在可连接公网的局域网上。winform端socket的ip地址该如何写?应该是公网ip,但通讯如何到达局域网上的指定电脑?
    也就是说,winform的网络节点是: 222.241.12.45-->192.168.0.11,socket如何实现这个节点的通讯?