我對(duì)網(wǎng)絡(luò)編程相當(dāng)陌生,我需要通過 TLS 連接 TCP 客戶端的幫助。我得到了一個(gè)已經(jīng)編碼的項(xiàng)目,它附帶了證書和提供的公鑰。我已經(jīng)在我的本地機(jī)器上安裝了 pfx 證書,并編寫了一個(gè)指向本地主機(jī)的新 TCP 偵聽器/服務(wù)器和 TCP 客戶端,如下所示:客戶端static void Main(string[] args) { string server = "localhost"; TcpClient client = new TcpClient(server, 5997); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //client.Connect(server, 5997);//connection without TLS - authenticating properly using (SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null)) { try { var servername = "myAuthenticatingServerName";//the server name must be the same as the one on the server certificate sslStream.AuthenticateAsClient(servername); try { Console.WriteLine("Client Connected..."); // Encode a test message into a byte array. // Signal the end of the message using the "<EOF>". byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>"); // Send hello message to the server. sslStream.Write(messsage); sslStream.Flush(); // Read message from the server. string serverMessage = ReadMessage(sslStream); Console.WriteLine("Server says: {0}", serverMessage); // Close the client connection. client.Close(); Console.WriteLine("Client closed."); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
1 回答

拉莫斯之舞
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
在服務(wù)器端,您正在偵聽端口 8080,在客戶端,您正在連接到端口 5997。更具體地說,您的服務(wù)器正在托管 [YourIP]:8080并且您的客戶端正在嘗試連接到 [YourIP]:5997
此外,Tls 和 SSL 通常用于端口 443??蛻舳撕头?wù)器端口需要相同,以便它們可以相互連接。
我也不確定c#是否將'localhost'識(shí)別為'YourIP'最好打開你的cmd(如果你使用的是Windows)輸入'ipconfig'點(diǎn)擊回車并查找并使用你的IPv4地址。
- 1 回答
- 0 關(guān)注
- 190 瀏覽
添加回答
舉報(bào)
0/150
提交
取消