C#基于.net framewrok2.0 socket的使用

winform服务端程序

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Net.Sockets;

using System.Windows.Forms;

using System.Net;

 

 

1.开启监听服务

 

            IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 10010);

            Server = new Socket(iPEndPoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);

            Server.Bind(iPEndPoint);

            Server.Listen(30);

            ThisMessageShow("开始监听...");

            accp_async();

 

2.开启处理连接请求

 

 

 void accp_async()

 {

            var accp = new SocketAsyncEventArgs();

            accp.Completed += Accp_Completed;

            Server.AcceptAsync(accp);

 }

 private void Accp_Completed(object sender, SocketAsyncEventArgs e)

        {

            accp_async();

           

            Socket Client = e.AcceptSocket;

            IPEndPoint endPoint = (IPEndPoint)Client.RemoteEndPoint;

            string key = endPoint.Address + ":" + endPoint.Port;

 

            InsertConn(Client);

 

            ThisMessageShow(key+"已连接...");

 

            e.Completed -= Accp_Completed;

            e.Completed += Recive_Completed;

            var buffer = new byte[1024];

 

            e.SetBuffer(buffer, 0, buffer.Length);

 

            Client.ReceiveAsync(e);

 }

 

 //处理接受消息

 

private void Recive_Completed(object sender, SocketAsyncEventArgs e)

{

            if (e.SocketError != SocketError.Success)

            {

                RemoveClosedConn(e.AcceptSocket);

                ThisMessageShow("通讯异常:" + e.SocketError.ToString());

            }

            else {

                string reviveMsg = Encoding.UTF8.GetString(e.Buffer);

                ThisMessageShow("接收到客户端消息:"+reviveMsg);

                e.AcceptSocket.ReceiveAsync(e);            

}        

}

 

//利用事件模型发送消息

 SocketAsyncEventArgs send = new SocketAsyncEventArgs();

 send.Completed += Send_Completed;

 var buffer = Encoding.UTF8.GetBytes("服务器消息:"+DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));

 send.SetBuffer(buffer,0,buffer.Length);

 sc.Value.SendAsync(send);

 

 private void Send_Completed(object sender, SocketAsyncEventArgs e)

 {

            ThisMessageShow("消息发送成功.");

 }

 

winform客户端程序

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Net.Sockets;

 

1.开始连接服务器

 

  Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            IPAddress iPAddress = IPAddress.Parse("154.211.5.131");

            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress,10010);

            client.Connect(iPEndPoint);

            ThisMessageShow("连接成功.");

 

            ReviceAsyn();

 

2.注册接受消息事件

   void ReviceAsyn()

        {

            SocketAsyncEventArgs _reciveMsg = new SocketAsyncEventArgs();

            _reciveMsg.Completed += _reciveMsg_Completed;

            var buffer = new byte[1024];

            _reciveMsg.SetBuffer(buffer, 0, buffer.Length);

            client.ReceiveAsync(_reciveMsg);

        }

3.利用事件模型处理事件

 private void _reciveMsg_Completed(object sender, SocketAsyncEventArgs e)

        {

            if (e.SocketError!= SocketError.Success)

            {

                //e.AcceptSocket.Close();

                ThisMessageShow("通讯异常:"+ e.SocketError.ToString());

                client.Close();

            }

            else

            {

                string reviveMsg = Encoding.UTF8.GetString(e.Buffer);

                ThisMessageShow("接收到服务器消息:" + reviveMsg);

                ReviceAsyn();

            }

        }

4.客户端发消息

//注册事件并发送消息

            SocketAsyncEventArgs _SocketAsyncEventArgs = new SocketAsyncEventArgs();

            _SocketAsyncEventArgs.Completed += _SocketAsyncEventArgs_Completed;            var sendbuffer = Encoding.UTF8.GetBytes("当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));

            _SocketAsyncEventArgs.SetBuffer(sendbuffer,0,sendbuffer.Length);

            client.SendAsync(_SocketAsyncEventArgs);

//事件回调

 private void _SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)

        {

 

            ThisMessageShow("消息发送成功.");

        }