I finally got the program to work as I wanted is reading the data from the telnet port for every scan it triggers, but now the only problem I am getting is that is reading the data if I display it in the console but I need it display into my main display which will be a text box multiline, can you see why I am getting this
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace BarcodeReceivingApp
{
public partial class BarcodeReceivingForm : Form
{
public BarcodeReceivingForm()
{
InitializeComponent();
}
private Thread _readWriteThread;
private TcpClient _client;
private NetworkStream _networkStream;
private void btn_ConnectToTelnetPort_Click(object sender, EventArgs e)
{
// connection tcp/ip
const string hostname = "myipaddress";
const int port = 23;
ServerSocket(hostname, port);
//Connect();
}
public void ServerSocket(string ip, int port)
{
try
{
_client = new TcpClient(ip, port);
lbl_ConnectionMessage.Text = @"Connected to server.";
}
catch (SocketException)
{
MessageBox.Show(@"Failed to connect to server");
return;
}
//Assign networkstream
_networkStream = _client.GetStream();
//start socket read/write thread
_readWriteThread = new Thread(ReadWrite);
_readWriteThread.Start();
}
private void ReadWrite()
{
var received = "";
//Read first thing givent o us
received = Read();
//Console.WriteLine(received);
txt_BarcodeDisplay.Text = received + Environment.NewLine;
//txt_BarcodeDisplay.Text = recieved.ToString();
//Set up connection loop
while (true)
{
var command = btn_StopConnection.Text;
if (command == "STOP1")
break;
//write(command);
received = Read();
//Console.WriteLine(received);
txt_BarcodeDisplay.Text += received + Environment.NewLine;
}
// possible method to end the port connection
}
public string Read()
{
byte[] data = new byte[1024];
var received = "";
var size = _networkStream.Read(data, 0, data.Length);
received = Encoding.ASCII.GetString(data, 0, size);
return received;
}
private void btn_StopConnection_Click(object sender, EventArgs e)
{
_networkStream.Close();
_client.Close();
}
}
}
Here is I am adding the error and is coming from the ReadWrite method enter image description here