MyNet.cs
using System;
using System.Net.Sockets;
using System.Net;
namespace MyNet
{
/// <summary>
/// MySocket 的摘要说明。
/// </summary>
public class MySocket:Socket
{
public MySocket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType):base(addressFamily,socketType,protocolType)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//申明代理
public delegate void SocketConnect(object sender,EventArgs e);
//申明事件
public event SocketConnect OnConnect;
new public void Connect(System.Net.EndPoint endPoint)
{
//如果连接不成功的话,下面的base.Connect()会出错,所以加上try
try
{
base.Connect(endPoint);
do
{
if(this.Connected)
{
OnConnect(this,new EventArgs());
break;
}
}
while(true);
}
catch(Exception e)
{
}
}
}
}
使用示例:
在Form1上加一个button
......
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);
}
private void socket_Connect(object sender,EventArgs e)
{
MessageBox.Show("Connected");
}
小弟不才,只是想抛砖引玉而已!