Transmit and receiving end based on TCP protocol
TCP protocol receiving end
Using system.net.sockets; // Used to TCPListen
Using system.threading; // Using thread
Using system.io; // Use the StreamReader class
INT port = 8000; // Defines the listening port number
Private thread thisreadread; // Create a thread to listen to the port number, receive information
Private tcplistener tltcplisten; // Listening port number
Private bool blistener = true; // Set the marking bit, determine the listening state
Private networkStream nsstream; // Create a receiving basic data stream
Private streamreader srread;
Private system.windows.Forms.statusbar statusbar1;
Private system.windows.Forms.Button button1;
Private system.windows.forms.listbox listbox1; // read data from the network base data stream
PRIVATE TCPCLIENT TCCLIENT;
Private void listen ()
{
Try
{
TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number
TLTCPListen.Start (); // Start listening
STATUSBAR1.TEXT = "Listening ...";
Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection
nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data
SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream
Statusbar1.text = "It has been connected!";
While (blistener) // loop listening
{
String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream
IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected
{
TLTCPLISTEN.STOP (); // Turn the listener
NSSTream.close (); // Release Resources
Srread.close ();
Statusbar1.text = "The connection is closed!";
THTHREADREAD.ABORT (); // Abort thread
Return;
}
String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data
ListBox1.Items.add (Stime " SMESSAGE);
}
}
Catch (System.Security.securityException)
{
Messagebox.show ("Listening Failed!", "Error");
}
}
// Start listening
Private void Button1_Click (Object Sender, System.Eventargs E)
{
ThthreadRead = New Thread (New ThreadStart (Listen));
THTHREADREAD.START (); // Start thread button button1.enabled = false;
}
// Clean all the resources being used.
Protected Override Void Dispose (Bool Disposing)
{
Try
{
TLTCPLISTEN.STOP (); // Turn the listener
NSSTream.close ();
SRREAD.CLOSE (); // Release Resources
THTHREADREAD.ABORT (); // Abort thread
}
Catch {}
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
TCP protocol
Using system.net.sockets; // Used to TCPListen
Using system.threading; // Using thread
Using system.io; // Using the streamwriter class
Using system.net; // use iPaddress class, iPhostentry class, etc.
Private streamwriter swwriter; // to send data to network basic data
Private networkStream nsstream; // Create a network base data of sending data
PRIVATE TCPCPCLIENT TCPCLIENT;
Private system.windows.Forms.Button button1;
Private system.windows.Forms.TextBox textBox1;
Private system.windows.Forms.Button Button2;
Private system.windows.Forms.TextBox textBox2;
Private system.windows.Forms.statusbar statusbar1;
Private system.windows.Forms.Label label1;
Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it
Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.
//connection
Private void Button1_Click (Object Sender, System.Eventargs E)
{
Ipaddress ipremote;
Try
{
ipremote = ipaddress.parse (TextBox1.text);
}
Catch // judges the legality of a given IP address
{
Messagebox.show ("Enter IP address is not legal!", "Error Tip!");
Return;
}
IPHOSTENTRY iphost;
Try
{
iPhost = DNS.Resolve (TextBox1.text);
}
Catch // Determine whether the IP address corresponds to whether the host is online
{
Messagebox.show ("Remote Host is not online!", "Error Tip!");
Return;
}
String Shostname = iphost.hostname;
Try
{
TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host
nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data
Swriter = new streamwriter (nsstream); // Initialize the streamwriter instance button1.enabled = false using the obtained network base data stream;
Button2.enabled = True;
TcpConnect = true;
Statusbar1.text = "It has been connected!";
}
Catch
{
MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");
Return;
}
}
//send
Private void button2_click (Object Sender, System.Eventargs E)
{
IF (TextBox2.text! = ")
{
SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream
SWRITER.FLUSH ();
}
Else
{
Messagebox.show ("Send information cannot be empty!", "Error Tip!");
}
}
// Clean all the resources being used.
Protected Override Void Dispose (Bool Disposing)
{
IF (TCPConnect)
{
SWRITER.WRITELINE ("STOP"); // Send control code
SWRITER.FLUSH (); // Refresh the data in the current data stream
NSSTream.close (); // Remove resources
SWRITER.CLOSE ();
}
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}