C # Real-time application technology

Source: Internet
Author: User
Tags socket thread tostring

Real time application Real-time application technology is used in this article as an example to demonstrate the use of the socket class between a user (TcpClient) request and a server (tcpserver) request. The project also demonstrates how to use ListView control in live projects and how to pass XML format information.

The TcpServer.exe file shows the communication between TCP sockets in a separate thread (rather than in a GUI thread).

The TcpClient.exe file also uses a separate thread to read data from the socket, and then updates the List view control in the form.

The following steps are gathered:

1. TCPServer listens on port 8002 and sends a ray to wait for the client to connect.

Hashtable socketHolder = new Hashtable();
Hashtable threadHolder = new Hashtable();
public Form1()
{
 // Required for Windows Form Designer support
 //
 InitializeComponent();
 tcpLsn = new TcpListener(8002);
 tcpLsn.Start();
 // tcpLsn.LocalEndpoint may have a bug, it only show 0.0.0.0:8002
 stpanel.Text = "Listen at: " + tcpLsn.LocalEndpoint.ToString();
 Thread tcpThd = new Thread(new ThreadStart(WaitingForClient));
 threadHolder.Add(connectId, tcpThd);
 tcpThd.Start() ;
}
2. TcpClient and tcpsrv Connect, send the client information packets to the TCPServer, and then launch the thread, which is used 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 ()


{


miv = new MethodInvoker (this. Updatelistview);


int cnt=0;


string Tmp=null;


byte[] firstb= new byte[1];


while (true)


 {


Try


  {


byte[] receive = new byte[1];


int ret = s.receive (Receive, 1, 0);


if (Ret > 0)


   {


switch (receive[0])


    {


Case One://check Start message


cnt=0;


break;


Case://Check End message


cnt=0;


if (firstb[0] = = ': ')


Handlecommand (TMP);


else if (firstb[0] = = ' < ')


handlexml (TMP);


Else


Handletext (TMP);


Tmp=null;


break;


Default:


if (cnt = 0)


firstb[0] = receive[0];


tmp + System.Text.Encoding


         . Ascii. GetString (receive);


cnt++;


break;


        }


       }


    }


catch (Exception e)


    {


if (!s.connected)


      {


break;


      }


     }


   }


T.abort ();


  }
3.TcpServer receives connection requests from TcpClient, saves the socket instance to the hash table, and then launches the thread to control the communication of the socket while displaying the client information in the ListView control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.