我用一台Gps手持机和pc通过socket连接
pc当主机,手持机当客户端
主机代码如下:            IPAddress ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ServerSocket.Bind(ipEndPoint );
            ListenThread = new Thread(ListenClientMessage);
            ListenThread.Start();
            ServerSocket.Listen(0);
            MessageBox.Show("服务器打开");
            Socket socket = ServerSocket.Accept();
            if (socket != null)
。。客户端代码如下:
 IPAddress ipAddress = IPAddress.Parse("192.168.0.105");
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ClientSocket.Connect(ipEndPoint);//运行到这就提示 主机主动拒绝
            MessageBox.Show("连接服务器成功");
……每次只要 手持机通过数据线连在pc上
运行 连接 就提示 主机主动拒绝
我通过 ipconfig 可以获得 pc 和手持机的ip
并且可以ping 通
 
这个图和解决啊  求助啊

解决方案 »

  1.   

    telnet 192.168.0.105  34567
    试试通不通检查防火墙是否屏蔽了该端口
      

  2.   

    都拒绝你连接了你就应该看你pc的监听程序了
    ip地址最好不要直接指定为第一个,或者你有多个网卡呢
    listen()多个连接看看
    不行单步调试看看那句报错的
      

  3.   

    PC端主要代码,你可以参考下
            Socket s = null;
            TcpClient clientsocket;
            NetworkStream ns;
            TcpListener tcpListener = null;
            TcpClient tcpClient = null;
            private System.Threading.Thread pcThread = null;
            private static bool IsDone = true;
            if (tcpListener == null)
            {
                  int port = 34567;
                  IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);              tcpListener = new TcpListener(localEndPoint);
                  tcpListener.Start(10);              if (pcThread==null)
                         pcThread = new System.Threading.Thread(new System.Threading.ThreadStart(StartServer));
                  pcThread.Start();
            }
           private void StartServer()
            {                
                while (IsDone)
                {
                    string data = null;
                    
                    try
                    {                   
                        tcpClient = tcpListener.AcceptTcpClient();
                        tcpClient.ReceiveTimeout = 240 * 1000;                   
                        
                        ns = tcpClient.GetStream();
                        byte[] recvBytes = new byte[1024];
                        int bytes = ns.Read(recvBytes, 0, recvBytes.Length);//从客户端接受信息  
                        if (bytes > 0)
                        {
                            data = Encoding.GetEncoding("GBK").GetString(recvBytes, 0, bytes);
                            //TO DO
    // //返回响应
                            byte[] bSend = Encoding.GetEncoding("GBK").GetBytes("SUCCESS");
                            ns.Write(bSend, 0, bSend.Length);
                        }
                    }
                    catch (SocketException ex)
                    {
                        //
                    }
                    catch (Exception ex)
                    {
                       //
                    }
                    finally
                    {
                        if (ns != null)
                        {
                            ns.Dispose();
                            ns.Close();
                            ns = null;
                        }
                        if (tcpClient != null)
                        {
                            tcpClient.Close();
                            tcpClient = null;
                        }
                    }
                    System.Threading.Thread.Sleep(500);
                }
            }
      

  4.   

    你的DNS  get的【0】个你看过是什么值么 ?我没错记错的是话IPv6吧 。这个就不对 。