alazhuabc304 2015-09-16 04:42 采纳率: 0%
浏览 2845

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)
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面