Add an onConnect event to Socket

zhaozj2021-02-11  213

Mynet.cs

Using system.net.sockets; using system.net;

Namespace MyNet {///

/// mysocket summary description. /// public class MySocket: Socket {public MySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType): base (addressFamily, socketType, protocolType) {// // TODO: Add constructor logic here // } // State the agent public delegate Void SocketConnect (Object sender, Eventargs E); // Stateing the event Public Event SocketConnect OnConnect;

New public void connect (system.net.endpoint endpoint) {// If the connection is unsuccessful, the following base.connect () will be wrong, so add try try {base.connect (endpoint); do {if (this. "{OnConnect (this, new evenetargs ()); break;}} while (true);} catch (exception e) {}}

}

Example: Add a Button on Form1

...... using mynet; .......

private void button1_Click (object sender, System.EventArgs e) {MySocket socket = new MySocket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.OnConnect = new MySocket.SocketConnect (this.socket_Connect); IPEndPoint endPoint = New IpendPoint (iPaddress.Parse ("192.168.75.74"), 80); socket.connect (endpoint);

The younger brother is not talented, just want to throw a brick!

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

New Post(0)