[英]Socket communication failure
我有一個客戶端,給我一個非ssl url:port來向其服務器發送信息(包含xml數據的字符串)。 我已經習慣於Putty(在telnet模式下)成功與服務器通信,並收到答復,但是當我使用以下代碼時,則無法進行通信
outputmsg = string.Empty;
var m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPHostEntry ipAddress = Dns.GetHostEntry("testurlhere");
var ip = new IPEndPoint(ipAddress.AddressList[0], 10121);
m_socListener.Connect(ip);
byte[] tosend = GetBytes(inputmsg);
byte[] buffer = new byte[1024];
m_socListener.Send(tosend); // doesnt sends data and returns immediately
m_socListener.Receive(buffer); // waits forever
m_socListener.Close();
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
我認為這里的解決方案可能是SetSockOpt NoDelay:
http://msdn.microsoft.com/en-us/library/e160993d.aspx
我還嘗試了m0s的“最優秀”建議來嘗試WireShark。 如果您還不熟悉-保證滿意!
但這聽起來像NoDelay(禁用Nagle)可能會解決問題。 該鏈接可能有助於闡明:
解決的辦法是在我的情況下使用正確的編碼,即ASCII並將\\ n附加到消息中,如paulsm4所說。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.