【发布时间】:2015-07-31 03:56:01
【问题描述】:
我与 IBM i(AS/400)建立了连接,该连接通过称为 TN5250 的协议/编码进行通信。我无法将它与任何编码 listed here 匹配;如何将此文本转换为我可以使用的内容? UTF8、ASCII; Windows 友好文本格式的任何内容都可以。不得涉及购买第三方库。
这是我在其他地方找到的一些“工作”代码。 “地址”是一个 IP 地址。
Socket SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(IPAddress.Parse("address"), 23);
SocketClient.Connect(remoteEndPoint);
byte[] buffer = new byte[10];
textBox1.Text += Receive(SocketClient, buffer, 0, buffer.Length, 10000).Trim() + "\r\n";
}
public static string Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
int startTickCount = Environment.TickCount;
int received = 0; // how many bytes is already received
do
{
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try
{
received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
return Encoding.GetEncoding(37).GetString(buffer, 0, buffer.Length);
//byte[] buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buffer);
//return Encoding.GetEncoding("IBM500").GetString(buf, 0, buffer.Length);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably empty, wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (received < size);
return "";
}
这是一个 Telnet 连接。在 Windows telnet 窗口中工作正常。我真正想要的解决方案是一种从 telnet 会话中捕获标准输出的方法,但显然 Telnet 等终端程序不会写入标准输出。
【问题讨论】:
标签: c# encoding ibm-midrange mainframe