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