Converting TN5250 Encoding in C#
I have a connection to IBM i (an AS/400) that communicates over a protocol/encoding called TN5250. I haven't been able to match it against any of the encodings listed here; how can I convert this text to something I can use? UTF8, ASCII; anything in a Windows-friendly text format will do. It must not involve buying a third-party library.
Here's some "working" code I found elsewhere. "address" is an IP address.
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,开发者_如何学C 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 "";
}
This is a Telnet connection. Works fine in a Windows telnet window. The solution I really want is a way to capture the stdout from the telnet session, but apparently terminal programs like Telnet don't write to stdout.
The TN5250J project is a working TN5250 client written in Java.
TN5250 is the IBM protocol that rides on top of Telnet. It is intended for the IBM midrange family of 'dumb' green screen terminals. This family is block mode, meaning the host sends a full display panel out to the client in one transmission, and the client sends a full display panel back to the host in one single transmission. This, as opposed to a character-by-character transmission.
As a very high level overview, the 5250 protocol describes how to format the display (start/stop field, field attributes like underline and colour) as well as what function keys are acceptable. The client needs to understand these formatting instructions in order to properly render the data coming from the host. Likewise, the client does not send back the full display panel including constants and formatting; instead, it sends back the input-capable fields.
There is no stdout per se; the human readable display panel requires rendering by the client. You may get a good sense for the raw data by using Wireshark to capture the packets and comparing them to an actual TN5250 display showing the same transaction.
From this article, it looks the encoding scheme is EBCDIC character-encoding scheme
.
See How to convert between ASCII and EBCDIC character codes for conversion details. It's in VB, but you should be able to convert it to C#. There is also an implementation on John Skeet's page here
How to convert from EBCDIC to ASCII in C#. From this post, looks like you might be able to use the 37 IBM037 IBM EBCDIC (US-Canada)
encoding from the list you provided: Encoding ebcdic = Encoding.GetEncoding("IBM037");
TN 5250 is not an encoding. It is a highly complex protocol.
The specification can be found here: http://www.ietf.org/rfc/rfc1205.txt (Note that this spec is not even complete)
There is no easy conversion. You have to write thousands of lines of code or use an already existing project like for example this one in C++ for Linux and Windows: http://sourceforge.net/projects/tn5250/files/
I did not find anything in C#
精彩评论