简体   繁体   中英

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. 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

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:

http://msdn.microsoft.com/en-us/library/e160993d.aspx

I also second m0s's Most Excellent suggestion to try WireShark. If you're not already familiar with it - satisfaction guaranteed!

But it sounds like NoDelay (disabling Nagle) might resolve the problem. This link might help clarify:

http://en.wikipedia.org/wiki/Nagle%27s_algorithm

解决的办法是在我的情况下使用正确的编码,即ASCII并将\\ n附加到消息中,如paulsm4所说。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM