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

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试