A TCP network information conversation that is implemented asynchronously with Socket

xiaoxiao2021-03-06  41

I haven't worried, emptiness, boring, daze ..., always give yourself something to do, I want to go, decide to use C # to do a POP3, SMTP mail to send and receive components, but no Familiar with the POP3, SMTP protocol, online mad, have a little eyebrow, but there is no way to try these orders, can not be, how can I do? Hey, why don't you do a network information to send and check the gadgets of the received information? This can not only try the POP3, SMTP protocol, but also try ftp, http ..., do it, do it, no need to do it, just see a asynchronous thing, in MSDN. Look, I know a probably, then I will do it, I will do it for a long time, I don't understand it. When I don't move, I will check the information.) The following is awkwardly called "TCP NET DIALOG" "Gadget.

HTTP example

FTP example

POP3 example

SMTP example

The source code using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Text;

A summary description of Namespace NetConnect {///

/// form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TextBox IPTextBox; private System.Windows.Forms.TextBox PortTextBox; private System.Windows.Forms.Button ConnectButton; private System.Windows.Forms.TextBox InputBox; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button SendButton; private System.Windows.Forms.RichTextBox ResultTextBox; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button exitButton; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.Button DisconnectButton;

Socket MySocket = New Socket (addressFamily.internetwork, sockettype.stream, protocoltype.tcp); string line = "/ r / n"; // newline, and network data end flag static int recvlength = 1024 * 10; / / Number of bytes per accepted byte [] Recvbf = new byte [recvlength]; string nullstring = system.text.utf8encoding.utf8.getstring (new byte [recvlength]); ///

/// must be Designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#Region Windows Form Designer Generated Code ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.IPTextBox = new System.Windows.Forms.TextBox (); this.PortTextBox = new System.Windows.Forms.TextBox (); this.ConnectButton = new System. Windows.Forms.Button (); this.inputbox = new system.windows.forms.textbox (); this.sendbutton = new system.windows.forms.Button (); this.label1 = new System.Windows.Forms.Label (); This.Label2 = new system.windows.forms.label (); this.label3 = new system.windows.forms.label (); this.disconnectButton = new system.windows.Forms.Button (); this. ResultTextBox = new System.Windows.Forms.RichTextBox (); this.button1 = new System.Windows.Forms.Button (); this.ExitButton = new System.Windows.Forms.Button (); this.statusBar1 = new System. Windows.forms.statusbar (); this.suspendlayout (); // iptextbox // this.iptextbox.location = new system.drawing.point (40, 8); this.iptextBox.name = "iptextbox"; this .Iptextbox.tabindex = 0; this.iptextbox.text = "192.168.72.193"; // // porttextbox // this.PortTextBox.location = new system.drawing.point (176, 8); this.porttextBox.Name = "porttextbox"; this.porttextbox.size = new system.drawing.size (32, 21) ; this.PortTextBox.TabIndex = 1; this.PortTextBox.Text = "80"; // // ConnectButton // this.ConnectButton.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles .Right); this.ConnectButton.Location = new System.Drawing.Point (224, 8); this.ConnectButton.Name = "ConnectButton"; this.ConnectButton.TabIndex = 2; this.ConnectButton.Text = "Connect";

this.ConnectButton.Click = new System.EventHandler (this.ConnectButton_Click); // // InputBox // this.InputBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles. Left) | system.windows.forms.Anchorstyles.right); this.inputbox.location = new system.drawing.point (64, 40); this.inputbox.multiline = true; this.inputbox.name = "inputbox"; THISINPUTBOX.SIZE = New System.drawing.size (232, 104); this.inputbox.tabindex = 3; this.inputbox.text = ""; this.inputbox.keyup = new system.windows.Forms.KeyeventHandler (this.InputBox_KeyUp); // // SendButton // this.SendButton.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right); this.SendButton.Location = new System. Drawing.point (312, 48); this.sendButton.Name = "sendbutton"; this.sendButton.Tabindex = 4; this.sendButton.text = "send"; this.sendButton.Click = new system.eventhandler (this .Sendbutton_click; // // label1 // This.Label1.Location = new system.drawing.point (8, 8); this.label1.name = "label1"; this.label1.size = new system.drawing.size (32, 23); this.label1. Tabindex = 7; this.label1.text = "ip"; /// Label2 // this.label2.location = new system.drawing.point (144, 8); this.label2.name = "label2"; this .label2.size = new system.drawing.size (32, 23); this.label2.tabindex = 8; this.label2.text = "port"; /// Label3 // this.label3.Location = new system .Drawing.point (8, 40); this.label3.name = "label3";

This.label3.size = new system.drawing.size (48, 23); this.label3.tabindex = 9; this.label3.text = "message"; // disconnectButton // this.disconnectButton.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right); this.DisconnectButton.Location = new System.Drawing.Point (312, 8); this.DisconnectButton.Name = "DisconnectButton"; this .DisconnectButton.TabIndex = 10; this.DisconnectButton.Text = "Disconnect"; this.DisconnectButton.Click = new System.EventHandler (this.DisconnectButton_Click); // // ResultTextBox // this.ResultTextBox.Anchor = ((( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ResultTextBox.Location = new System.drawing.point (8, 160); this.ResulttextBox.Name = "resulttextbox"; this.ResulttextBox.readonly = true; this.ResulttextBox.size = new system.drawing.size (384, 3 12); this.ResulttextBox.tabindex = 11; this.ResulttextBox.text = ""; this.resulttextbox.WordWrap = false; // // button1 // this.button1.ancy = (System.Windows.Forms.Anchorstyles. TOP | System.Windows.Forms.Anchorstyles.right; this.button1.location = new system.drawing.point (312, 80); this.button1.name = "button1"; this.button1.tabindex = 12; this .Button1.text = "clear"; this.button1.click = new system.eventhandler (this.button1_click); // // exitButton // this.exitbutton.Anchor =

(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right); this.ExitButton.Location = new System.Drawing.Point (312, 112); this.ExitButton.Name = "ExitButton"; this.ExitButton.TabIndex = 13; this.ExitButton.Text = "Exit"; this.ExitButton.Click = new System.EventHandler (this.ExitButton_Click); // // statusBar1 // this.statusBar1.Location = new System .Drawing.point (0, 477); this.statusbar1.name = "statusbar1"; this.statusbar1.size = new system.drawing.size (400, 16); this.statusbar1.tabindex = 14; this.statusbar1. TEXT = "Programd by xuexplorer mail: xuexplorer@163.com"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (400 , 493); this.controls.addrange (new system.windows.forms.control [] {this.statusbar1, this.exitbutton, this.button1, this.Resulttextbox, This.disconnectButton, this.label3, this.label2, this.label1, this.sendbutton, this.inputbox, this.connectButton, this.portTextBox, this.iptextbox}); this.name = "form1"; this.text = "TCP NET DIALOG"; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

///

/// connection /// /// private void connectionButton_Click (object sender, System.EventArgs e) {try {if (MySocket.Connected) return; IPAddress ip = IPAddress.Parse (IPTextBox.Text); int port = int.Parse (PortTextBox.Text); IPEndPoint ipEP = new IPEndPoint ( ip, port); MySocket.Connect ((EndPoint) ipEP); if (MySocket.Connected) {this.ResultTextBox.Text = "Connect" IPTextBox.Text ":" PortTextBox.Text "Successfully" Line;

MySocket.BeginReceive (RecvBF, 0, RecvBF.Length, 0, new System.AsyncCallback (ReceiveMessage), MySocket);}} catch (Exception ex) {this.ResultTextBox.Text = "Connect" IPTextBox.Text ":" PortTextBox.text "unsuccessful" line;}}

///

/// Disconnect /// /// /// private Void DisconnectButton_Click (Object Sender, System.EventAndargs E) {disconnect ();

///

/// Disconnect /// private void disconnect () {if (mysocket.Connected) {mysocket.shutdown (socketshutdown.both); mysocket.close (); this.ResultTextBox .Text = line "Connection Closed" Line; MySocket = New Socket (AddressFamily.internetwork, Sockettype.Stream, protocoltype.tcp);}}

///

/// callback function, accept information /// /// private void result) {String str = system .Text.UTF8Encoding.Default.GetString (RecvBF); RecvBF = new byte [RecvLength]; if (str = NullString!) {this.ResultTextBox.Text = "Recv:" str Line;} else {DisConnect (); } if (MySocket.Connected) {MySocket.EndReceive (result); MySocket.BeginReceive (RecvBF, 0, RecvBF.Length, 0, new System.AsyncCallback (ReceiveMessage), mySocket);}} /// // / Send information /// private void sendMessage () {if (! Mysocket.connected) {this.resulttextBox.text = "no connection" line; return;} String msg = this.inputbox.text; TRY {Byte [] sendbf = new byte [1024]; sendbf = system.text.utf8encoding.utf8.getBytes (msg); mysocket.send (sendbf); this.ResultTextBox.text = "send: /" this.inputbox .Text "/" SuccessFully " line;} catch {this.resulttextbox.text =" send: / " this.inputbox.text " / " Unsuccessful " line;} finally {this.inputbox.clear ();}}

///

/// send information /// /// /// private void SendButton_Click (Object Sender, System.EventAndargs E) {sendMessage ();

///

/// Clear result /// /// /// private void Button1_click (object sender, system.eventargs e) {this.resulttextbox.clear ();} /// /// exit /// /// /// private void exitButton_Click (Object sender, system.eventargs e) {disconnect (); dispose;}

///

/// Information input box Press CTRL ENTER key button /// /// /// private void inputbox_keyup (object sender, system.windows.Forms.keyeventargs e) {i ((e.keydata & keys.control) == Keys.Control && (E.KEYDATA & Keys.Enter) = = Keys.Enter) {INT i = inputBox.text.lastIndexof (line); this.inputbox.text = this.inputbox.text.substring (0, inputbox.text.lastIndexof ("/ r / n")); sendMessage ();}}}} There is no detailed description, because I don't understand half-know, I am afraid that I mislead the beginner, please call the "asynchronous call" in the MSDN asynchronously. It's just for fun and spending time.

转载请注明原文地址:https://www.9cbs.com/read-51717.html

New Post(0)