TcpListener未检测到连接

时间:2015-12-26 23:52:22

标签: c# tcpclient

这些天我一直在做客户端 - 服务器应用程序。我根据示例创建了一个服务器部分来启动TcpListener,它似乎运行良好。 但是,它无法检测客户端连接,而客户端则说它连接到给定的IP和端口。

这是我的服务器端代码:

class SkryptPolicy < Struct.new(:user, :skrypt)
  class Scope < Scope
    def resolve
      user.skrypts
    end
  end

  def index?
    not user.nil?
  end
  alias_method :create?, :index?

  def show?
    user && (skrypt.user == user)
  end
  alias_method :update?, :show?
  alias_method :destroy?, :show?
end

客户方:

    internal static void RunServer()
    {
        // Create a TCP/IP (IPv4) socket and listen for incoming connections.
        ServerLogger.LogStatus("Starting listener...");
        listener = new TcpListener(IPAddress.Any, Server.PORT + 2);
        listener.Start();
        ServerLogger.LogStatus("Starting thread listener...");
        tlisten = new Thread(Listen);
        tlisten.Start();
        ServerLogger.LogStatus("Starting receiver...");
        treceiver = new Thread(Receiver);
        treceiver.Start();
        ServerLogger.LogStatus("All done!");
    }

    internal static void Listen()  // Listen to incoming connections.
    { 
       while (running)
        {
            ServerLogger.LogStatus("Waiting for a connecion....");
            TcpClient tcpClient = listener.AcceptTcpClient();
            // The below code never runs on client connection.
            ServerLogger.LogStatus("Accepting and Creating cert!");
            // Create a new certification
            byte[] serverCertificatebyte = Certificate.CreateSelfSignCertificatePfx("Test" + IP + RandomString(5),
                new DateTime(2015, 12, 26),
                new DateTime(DateTime.Today.Year, DateTime.Today.Month + 1, DateTime.Today.Day), RandomString(20));
            ServerLogger.LogStatus("Created certificate!");
            X509Certificate serverCertificate = new X509Certificate(serverCertificatebyte);
            tcpclientbytecerts[tcpClient] = serverCertificatebyte;
            tcpclientcerts[tcpClient] = serverCertificate;
            ServerLogger.LogInfo("Sending Certificate!");
            // Send SSL certification in bytes
            tcpClient.GetStream().Write(serverCertificatebyte, 0, serverCertificatebyte.Length);
            ServerLogger.LogInfo("Certificate sent to the client!");
            Logger.Log(serverCertificatebyte.ToString());
            // process it
            ServerLogger.LogInfo("Processing");
            ProcessClient(tcpClient, serverCertificate);
            ServerLogger.LogInfo("Processed!");
        }
    }

客户端似乎能够连接,它甚至返回true并说它已连接。服务器端虽然没有检测到它。有什么想法吗?

0 个答案:

没有答案