C# TcpListener没有';无法检测连接
这几天我一直在做客户机-服务器应用程序。我根据示例制作了一个服务器部件来启动TcpListener,它似乎工作得很好。 但是,它无法检测客户端连接,而客户端表示它已连接到给定的ip和端口 以下是我的服务器端代码:C# TcpListener没有';无法检测连接,c#,tcpclient,C#,Tcpclient,这几天我一直在做客户机-服务器应用程序。我根据示例制作了一个服务器部件来启动TcpListener,它似乎工作得很好。 但是,它无法检测客户端连接,而客户端表示它已连接到给定的ip和端口 以下是我的服务器端代码: internal static void RunServer() { // Create a TCP/IP (IPv4) socket and listen for incoming connections. ServerLogger.L
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!");
}
}
客户端:
private SSL(string ip, int port)
{
UnityEngine.Debug.Log("Test");
if (!PrivacyHelper.IsInvocationAllowed())
{
Terminate();
}
UnityEngine.Debug.Log("Test2 " + ip + " - " + port);
UnityEngine.Debug.Log("Test3 " + IsLocalIpAddress(ip));
client = new TcpClient(ip, port);
//client.Connect(ip, port);
UnityEngine.Debug.Log("Test4 " + client.Connected); // <- This is true
var stream = client.GetStream();
UnityEngine.Debug.Log("Test5");
Byte[] cert = new Byte[65535];
UnityEngine.Debug.Log("Test6");
Int32 bytes = stream.Read(cert, 0, cert.Length);
UnityEngine.Debug.Log("Test7");
byte[] intBytes = BitConverter.GetBytes(bytes);
UnityEngine.Debug.Log("Test8");
if (BitConverter.IsLittleEndian) { Array.Reverse(intBytes);}
UnityEngine.Debug.Log("Test9");
byte[] result = intBytes;
UnityEngine.Debug.Log("Cert: " + result);
certificate1 = new X509Certificate2(result);
//client.GetStream().Read()
}
私有SSL(字符串ip,int端口)
{
UnityEngine.Debug.Log(“测试”);
如果(!PrivacyHelper.isInvocationLowed())
{
终止();
}
UnityEngine.Debug.Log(“Test2”+ip+“-”+port);
UnityEngine.Debug.Log(“Test3”+IsLocalIpAddress(ip));
客户端=新的TCP客户端(ip、端口);
//client.Connect(ip,端口);
UnityEngine.Debug.Log(“Test4”+client.Connected);//什么是Server.PORT+2?为什么是+2?Server.PORT是游戏服务器的端口。假设28015是端口,28016是查询端口。这就是为什么我做+2在你询问new SSL()之前,我已经添加了+2。因此它连接到28017端口。