I have 3 classes form1.cs and handleClient.cs and GlobalVariable.cs , There is one method in form1.cs which have code to display data in textbox , when called this method from form1.cs itself, it is working fine but if called in handleClient.cs this method doesnt works. I assign data to be display in textbox to static string variable "somedata" declared in GloabalVariable class. and data is display in textbox but after closing connection objects, lets say closing threads of clients. But i want to display data at runtime, How to make this possible ? Please help.
thanks .
here is my code,
in GlobalVariable class
class GlobalVariable
{
public static string SomeData = "";
}
in form1 class
namespace connectSuccess
{
public partial class Form1 : Form
{
private handleClient handleClient;
private bool IsRunning = false;
TcpListener serverSocket;
TcpClient clientSocket;
public Form1()
{
InitializeComponent();
}
public Form1(handleClient handleClient)
{
this.handleClient = handleClient;
}
private void Form1_Load(object sender, EventArgs e)
{
string ipaddre = getip();
IPAddress iP = IPAddress.Parse(ipaddre);
AppendTxtip(iP.ToString());
}
private void btnstart_Click(object sender, EventArgs e)
{
new Thread(startServer).Start();
}
public void startServer()
{
try
{
int port = Convert.ToInt32(txtport.Text);
string ipaddre = getip();
IPAddress iP = IPAddress.Parse(ipaddre);
serverSocket = new TcpListener(iP, port);
clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
AppendTxtdata("Server Started , Waiting for Client Connection" + Environment.NewLine);
IsRunning = true;
while (IsRunning)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
AppendTxtdata(" >> " + "Client No:" + Convert.ToString(counter) + " Connected-- IP --" + clientSocket.Client.RemoteEndPoint.ToString() + "");
AppendTxtdata(Environment.NewLine);
handleClient client = new handleClient();
client.startClient(clientSocket, Convert.ToString(counter));
}
}
catch { }
finally { }
}
public void StopServer()
{
IsRunning = false;
}
public void AppendTxtdata(string myval)
{
try
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTxtdata), new object[] { myval });
return;
}
GlobalVariable.SomeData += myval;
txtdata.Text = GlobalVariable.SomeData;
}
catch
{ }
}
I am calling this method in handleClient.cs in startCLient method in this way,
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
inClientSocket.NoDelay = true;
this.clNo = clineNo;
new Thread(GetData).Start();
}
public void GetData()
{
con.Open();
Form1 f = new Form1();
try
{
NetworkStream networkStream = clientSocket.GetStream();
for (int p = 0; p >= 0; p++)
{
byte[] b = new byte[100];
int k = networkStream.Read(b, 0, b.Length);
f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." + Environment.NewLine);
for (int i = 0; i < k; i++)
{
f.AppendTxtdata("" + Convert.ToChar(b[i]));
}
f.AppendTxtdata(Environment.NewLine);
var j = b.Length - 1;
while (b[j] == 0)
{
--j;
}
var temp = new byte[j + 1];
Array.Copy(b, temp, j + 1);
string res = System.Text.Encoding.ASCII.GetString(temp);