TcpClient connect to TcpSrv and sends Client information data packet to TcpServer then spawns a thread, which waits to receive data through the Socket. private void menuConn_Click(object sender, System.EventArgs e)
{
ConnectDlg myDlg = new ConnectDlg();
myDlg.ShowDialog(this);
if( myDlg.DialogResult==DialogResult.OK)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
IPAddress hostadd = IPAddress.Parse(myDlg.IpAdd);
int port=Int32.Parse(myDlg.PortNum);
IPEndPoint EPhost = new IPEndPoint(hostadd, port);
Try
{
s.Connect(EPhost);
if (s.Connected)
{
Byte[] bBuf;
string buf;
buf = String.Format("{0}:{1}", myDlg.UserName,
myDlg.PassWord);
bBuf=ASCII.GetBytes(buf);
s.Send(bBuf, 0 , bBuf.Length,0);
t = new Thread(new ThreadStart(StartRecieve));
t.Start();
sbar.Text="Ready to recieve data";
}
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
}
}
private void StartRecieve()
{
MethodInvoker miv = new MethodInvoker(this.UpdateListView);
while (true)
{
Byte[] receive = new Byte[38] ;
Try
{
string tmp=null;
int ret = s.Receive(receive, receive.Length, 0);
if (ret>0)
{
tmp = System.Text.Encoding.ASCII.GetString(receive);
if(tmp.Length > 0)
{
isu.symbol= Mid(tmp, 0, 4);
isu.bid = Mid(tmp, 4, 5);
isu.offer = Mid(tmp, 9, 5);
isu.volume = Mid(tmp, 16, tmp.Length-16);
this.BeginInvoke(miv);
Thread.Sleep(300);
}
}
}
catch (Exception e)
{
if( !s.Connected )
{
break;
}
}
}
t.Abort();
}
Click Load Data Menu to spawns a thread to load the information from a file then sends the information to all the clients that were connected to the TcpServer and update its own listview.
In both TcpServer and TcpClient, they get the data from a working thread, and then update the Listview control in the Main thread. Here use the MethodInvoker to work it out.
public void LoadThread()
{
MethodInvoker mi = new MethodInvoker(this.UpdateListView);
string tmp = null;
StreamReader sr = File.OpenText("Issue.txt");
while((tmp = sr.ReadLine()) !=null )
{
if (tmp =="")
break;
SendDataToAllClient(tmp);
isu.symbol= Mid(tmp, 0, 4);
isu.bid = Mid(tmp, 4, 5);
isu.offer = Mid(tmp, 9, 5);
isu.volume = Mid(tmp, 16, tmp.Length-16);
this.BeginInvoke(mi);
Thread.Sleep(200);
JobDone.WaitOne();
}
sr.Close();
fThd.Abort();
}
private void SendDataToAllClient(string str)
{
foreach (Socket s in socketHolder.Values)
{
if(s.Connected)
{
Byte[] byteDateLine=ASCII.GetBytes(str.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
}
}
}
Following function demonstrate how to dynamically set BackColor and Forecolor properties of the Listview in TcpClient.
private void UpdateListView()
{
int ind=-1;
for (int i=0; i<this.listView1.Items.Count;i++)
{
if (this.listView1.Items[i].Text == isu.symbol.ToString())
{
ind=i;
break;
}
}
if (ind == -1)
{
ListViewItem newItem new ListViewItem(isu.symbol.ToString());
newItem.SubItems.Add(isu.bid);
newItem.SubItems.Add(isu.offer);
newItem.SubItems.Add(isu.volume);
this.listView1.Items.Add(newItem);
int i=this.listView1.Items.IndexOf(newItem);
setRowColor(i, System.Drawing.Color.FromArgb(255, 255, 175));
setColColorHL(i, 0, System.Drawing.Color.FromArgb(128,0,0));
setColColorHL(i, 1, System.Drawing.Color.FromArgb(128,0,0));
this.listView1.Update();
Thread.Sleep(300);
setColColor(i, 0, System.Drawing.Color.FromArgb(255, 255,175));
setColColor(i, 1, System.Drawing.Color.FromArgb(255, 255, 175));
}
else
{
this.listView1.Items[ind].Text = isu.symbol.ToString();
this.listView1.Items[ind].SubItems[1].Text = (isu.bid);
this.listView1.Items[ind].SubItems[2].Text = (isu.offer);
this.listView1.Items[ind].SubItems[3].Text = (isu.volume);
setColColorHL(ind, 0, System.Drawing.Color.FromArgb(128,0,0));
setColColorHL(ind, 1, System.Drawing.Color.FromArgb(128,0,0));
this.listView1.Update();
Thread.Sleep(300);
setColColor(ind, 0, System.Drawing.Color.FromArgb(255,255,175));
setColColor(ind, 1, System.Drawing.Color.FromArgb(255,255,175));
}
JobDone.Set();
}
private void setRowColor(int rowNum, Color colr )
{
for (int i=0; i<this.listView1.Items[rowNum].SubItems.Count;i++)
if (rowNum%2 !=0)
this.listView1.Items[rowNum].SubItems[i].BackColor = colr;
}
private void setColColor(int rowNum, int colNum, Color colr )
{
if (rowNum%2 !=0)
this.listView1.Items[rowNum].SubItems[colNum].BackColor=colr;
else
this.listView1.Items[rowNum].SubItems[colNum].BackColor =
System.Drawing.Color.FromArgb(248, 248,248);
if (colNum==0)
{
this.listView1.Items[rowNum].SubItems[colNum].ForeColor =
System.Drawing.Color.FromArgb(128, 0, 64);
this.listView1.Items[rowNum].SubItems[colNum].BackColor =
System.Drawing.Color.FromArgb(197, 197, 182);
}
else
this.listView1.Items[rowNum].SubItems[colNum].ForeColor =
System.Drawing.Color.FromArgb(20, 20,20);
}
private void setColColorHL(int rowNum, int colNum, Color colr )
{
this.listView1.Items[rowNum].SubItems[colNum].BackColor = colr;
this.listView1.Items[rowNum].SubItems[colNum].ForeColor =
System.Drawing.Color.FromArgb(255,255,255);
}