我有一个客户端,它给了我一个非 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);
}