[英]c# Socket connection
try
{
_clientSocket.Connect(new IPEndPoint(IPAddress.Parse("169.254.18.196"), 49897));
Debug.Log("Connected to the server.");
}
catch
{
attemps++;
Debug.Log("Connection attempt... " + attemps);
}
我有一台運行在debian中的服務器,使用Monodevelop用c#編程。 代碼來自客戶端。 這是連接部分。
這是我創建套接字的方式:
private readonly Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
消息發送:
try {
SendString("<username>" + userName + "</username>" + "<password>" + password + "</password>");
} catch {
Debug.Log("Error while sending the message");
}
private void SendString(string text) {
byte[] buffer = Encoding.Unicode.GetBytes(text);
_clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
Debug.Log("Message sent: " + text);
}
我收到消息:
Connected to the server.
即使服務器未運行,我的意思是只是debian在運行,但服務器文件在運行。
為什么我在日志中收到此消息? 我該如何解決?
好吧,如果您想知道套接字是否確實已連接,請使用Socket.Connected屬性,並進行記錄,如下所示:
if (_clientSocket.Connected)
{
Debug.Log("Connected to the server.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.