[英]Socket communication failure
I have a client which given me a non ssl url:port to send information(string containing xml data) to their server. 我有一个客户端,给我一个非ssl url:port来向其服务器发送信息(包含xml数据的字符串)。 I have used to Putty(in telnet mode) to successfully communicate with the server,and recieve the reply but when i am using the following code no communication is made 我已经习惯于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);
}
I think the solution here might be to SetSockOpt NoDelay: 我认为这里的解决方案可能是SetSockOpt NoDelay:
http://msdn.microsoft.com/en-us/library/e160993d.aspx http://msdn.microsoft.com/en-us/library/e160993d.aspx
I also second m0s's Most Excellent suggestion to try WireShark. 我还尝试了m0s的“最优秀”建议来尝试WireShark。 If you're not already familiar with it - satisfaction guaranteed! 如果您还不熟悉-保证满意!
But it sounds like NoDelay (disabling Nagle) might resolve the problem. 但这听起来像NoDelay(禁用Nagle)可能会解决问题。 This link might help clarify: 该链接可能有助于阐明:
http://en.wikipedia.org/wiki/Nagle%27s_algorithm http://en.wikipedia.org/wiki/Nagle%27s_algorithm
解决的办法是在我的情况下使用正确的编码,即ASCII并将\\ n附加到消息中,如paulsm4所说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.