zoukankan      html  css  js  c++  java
  • socket 由于目标机器积极拒绝,无法连接的解决办法

            客户最近提出一个需求,要在WEB上远程管理客户端软件。那我们就仿路由器那种模式用SOCKET来解决吧。做了个DEMO,本机测试OK,拿到别的机器上做服务器,提示由于目标机器积极拒绝,无法连接。查询各种资料,有的说是端口没开,有的说是服务没开。各种雾水啊!仔细一想,问题可能出在本机在局域网IP上,而不是用127.0.0.1。更正代码后,问题解决。下面演示服务器端代码的关键部分。

     protected void Listen()
            {
                
                MessageBox.Show("start listening");
                string ip = "";
                System.Net.IPHostEntry IpEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());


                for (int i = 0; i != IpEntry.AddressList.Length; i++)
                {
                    if (!IpEntry.AddressList[i].IsIPv6LinkLocal)
                    {
                       ip= IpEntry.AddressList[i].ToString();
                    }
                }
                IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(ip), 8000);
                Socket sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sc.Bind(ipend);
               Socket acc;
                while (true)
                { sc.Listen(1);
                 acc= sc.Accept();
                   
                        byte[] buff = new byte[1024];
                        int recbyte = acc.Receive(buff, buff.Length, 0);
                        if (recbyte == 0)
                            break;
                        string reciveval = "";
                        reciveval += Encoding.GetEncoding("gb2312").GetString(buff, 0, recbyte);
                        string returnval = "开始升级";
                        byte[] returnBy = Encoding.GetEncoding("gb2312").GetBytes(returnval);
                        acc.Send(returnBy, returnBy.Length, 0);
          
                }
                acc.Close();
                sc.Close();
            }

    客户端

    public string sendMessage()
        {
            IPEndPoint ipend = new IPEndPoint(IPAddress.Parse("192.168.XXX.XXX"),8000);
            Socket sc = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            sc.Connect(ipend);
            string message = "请升级软件";
            byte[] bt = Encoding.GetEncoding("gb2312").GetBytes(message);
            sc.Send(bt,bt.Length,0);

            byte[] rebuff = new byte[1024];
            int recive = sc.Receive(rebuff, rebuff.Length, 0);
            string returnval = "";
            returnval += Encoding.GetEncoding("gb2312").GetString(rebuff, 0, recive);
            sc.Close();

            return returnval;
           
        }
  • 相关阅读:
    topcoder SRM 592 DIV2 LittleElephantAndBooks
    codeforces round #201 Div2 A. Difference Row
    Codeforces Round #199 (Div. 2) A Xenia and Divisors
    objective-c "performSelector may cause a leak because its selector is unknown".
    ccrendertexture to uiimage
    TopCoder SRM 588 DIV2 KeyDungeonDiv2
    ios clang: error: linker command failed with exit code 1 (use -v to see invocation)解决方法
    c++ for_each()与仿函数
    c++ map删除元素
    c++ map和mutimaps 插入值
  • 原文地址:https://www.cnblogs.com/wishbay/p/2411180.html
Copyright © 2011-2022 走看看