socket如何实现其中的发送回调函数和完成发送的代码功能

c#socket异步通信中的SendCallback类包含在静态类中如何实现其中的发送回调函数和完成发送的代码功能 以及错误如何解决![图片说明](https://img-ask.csdn.net/upload/201509/16/1442378500_213820.jpg)图片说明
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace DAL
{
///
/// Socket接口
///

static class socket
{
    //Socket
    public static Socket clientSocket;

    /// <summary>
    /// socket连接
    /// </summary>
    /// <returns>
    /// true    成功
    /// false   失败
    /// </returns>

    public static bool SocketConnect()
    {
          byte[] receive_buff = new byte[256];
          ManualResetEvent connectDone = new ManualResetEvent(false); //连接的信号
          ManualResetEvent readDone = new ManualResetEvent(false); //读信号
          ManualResetEvent sendDone = new ManualResetEvent(false); //发送结束

        //从配置文件获取IP
        string SocketIP = DAL.Common.ReadConfigString("Recover", "IP");
        //从配置文件获取端口
        int SocketPort = Convert.ToInt32(DAL.Common.ReadConfigString("Recover", "Port"));
        //创建IP地址
            IPAddress IP = IPAddress.Parse(SocketIP);

        try
        {

            //创建socket实例
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //创建网络端点
            IPEndPoint ipEnd = new IPEndPoint(IP, SocketPort);
            //EndPoint ipEnd = (EndPoint)remotepoint;
            //与目标终端连接
            clientSocket.BeginConnect(ipEnd,

            new AsyncCallback(ConnectCallback), clientSocket);//调用回调函数


            connectDone.WaitOne();
             if (clientSocket.Connected)
            {
                return true;
            }
            else
            {
                return false;
            }




         }
         catch (Exception e)
        {
            string strError = "";
            strError += "\r\n SocketIP = " + SocketIP.ToString();
            strError += "\r\n SocketPort = " + SocketPort.ToString();
            DAL.Common.WriteErrorLog(e, strError);
            return false;
        }
    }
    public static void ConnectCallback(IAsyncResult ar)
    {
        bool connect_flag = false;
        ManualResetEvent connectDone = new ManualResetEvent(false); //连接的信号
        Socket client = (Socket)ar.AsyncState;
        client.EndConnect(ar);
        connect_flag = true;
        connectDone.Set();
    }

    /// <summary>
    /// Socket发送数据
    /// </summary>
    /// <param name="strSend">
    /// 数据的内容
    /// </param>
    public static void SocketSend(string strSend)
    {
        Socket tcpsend; //发送创建套接字
        ManualResetEvent sendDone = new ManualResetEvent(false);//发送结束
        int length = strSend.Length;
        Byte[] Bysend = new byte[length];
        try
        {

        Bysend = System.Text.Encoding.Default.GetBytes(strSend); //将字符串指定到指定Byte数组
        tcpsend.BeginSend(Bysend, 0, Bysend.Length, 0, new AsyncCallback(SendCallback), tcpsend); //异步发送数据
        sendDone.WaitOne();
          }
        catch (Exception e)
        {
            string strError = "";
            DAL.Common.WriteErrorLog(e, strError);
        }
    }
    private void SendCallback(IAsyncResult ar) //发送的回调函数
    {
        Socket client = (Socket)ar.AsyncState;
        int bytesSend = client.EndSend(ar); //完成发送
        sendDone.Set();
    }![图片说明](https://img-ask.csdn.net/upload/201509/16/1442378304_186857.jpg)
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问