嵌入式马邦德 2021-09-29 11:19 采纳率: 100%
浏览 60
已结题

c#socket异步通讯问题


public class AsyncTCP
    {
        readonly S7_telegrasm s7 = new S7_telegrasm();
        public Socket socket;
        /// <summary>
        /// 连接
        /// </summary>
        public void Connect(string ip, string port)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress IPA = IPAddress.Parse(ip);
            IPEndPoint iep = new IPEndPoint(IPA, int.Parse(port));
            socket.BeginConnect(iep, new AsyncCallback(ConnectCallbackj), socket);
            byte[] send;
            try
            {
                //cotp的发送接收
                send = s7.Cotp();
                socket.BeginSend(send, 0, send.Length, 0, new AsyncCallback(tellCallback), socket);  //建立连接时这里报错了
                Thread.Sleep(100);
                byte[] cotp = new byte[22];
                socket.BeginReceive(cotp, 0, cotp.Length, 0, new AsyncCallback(askCallback), socket);
                Thread.Sleep(100);

                //通信建立的发送接收
                send = s7.Comm();

                socket.BeginSend(send, 0, send.Length, 0, new AsyncCallback(tellCallback), socket);
                Thread.Sleep(100);
                byte[] S7comm = new byte[27];
                socket.BeginReceive(S7comm, 0, S7comm.Length, 0, new AsyncCallback(askCallback), socket);
                Thread.Sleep(100);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
}
        /// <summary>
        /// Connect回调函数
        /// </summary>
        void ConnectCallbackj(IAsyncResult iar)
        {
            try
            {
                Socket client = (Socket)iar.AsyncState;
                client.EndConnect(iar);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private void askCallback(IAsyncResult iar)
        {
            try
            {
                Socket ask = (Socket)iar.AsyncState;
                int bytesSent = ask.EndSend(iar);
                Console.WriteLine("Sent {0} bytes to client.", bytesSent);
                ask.Shutdown(SocketShutdown.Both);
                ask.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private void tellCallback(IAsyncResult iar)
        {
            try
            {
                Socket tell = (Socket)iar.AsyncState;
                int bytesSent = tell.EndSend(iar);
                Console.WriteLine("Sent {0} bytes to client.", bytesSent);
                tell.Shutdown(SocketShutdown.Both);
                tell.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        public void Send(Socket handler, byte[] byteSend)
        { 
            handler.BeginSend(byteSend, 0, byteSend.Length, 0, new AsyncCallback(SendCallback), handler);
        }
        private void SendCallback(IAsyncResult iar)
        {
            try
            {
                Socket handler = (Socket)iar.AsyncState;
                int bytesSent = handler.EndSend(iar);
                Console.WriteLine("Sent {0} bytes to client.", bytesSent);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        public void Receive(Socket client,byte[] byteRead)
        {
            try
            {
                if (byteRead.Length > 0)
                {   
                    client.BeginReceive(byteRead, 0, byteRead.Length, 0, new AsyncCallback(ReceiveCallback), client);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private void ReceiveCallback(IAsyncResult iar)
        {
            try
            {
                Socket handler = (Socket)iar.AsyncState;
                int bytesSent = handler.EndSend(iar);
                Console.WriteLine("Sent {0} bytes to client.", bytesSent);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

我本想建立连接时将两个报文发送过去,但是在报文发送哪里出了问题,连接上了,发不过去

img

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 12月3日
      • 已采纳回答 11月25日
      • 创建了问题 9月29日

      悬赏问题

      • ¥15 如何在linux服务器做视频静态资源访问接口
      • ¥15 Python Xpath对<em>标签的爬取结果变成了- -(关键词-Xpath)
      • ¥15 el-cascader懒加载模式选中单选框加载下层无数据问题
      • ¥15 promql 参数怎么忽视大小写查询
      • ¥50 swift使用textkit读取大文本文件速度慢的问题
      • ¥20 统信linux360企业浏览器 如何配置扩展程序?(图形界面我知道)
      • ¥100 c/c++实现DoubleArrayTrie
      • ¥15 在开发/迭代一款企业信息软件时会耗费你们多长时间?
      • ¥15 如果可以为你提供一款定制化的企业软件,您最希望在哪个板块实现定制化?
      • ¥30 关于#网络安全#的问题:非对称加密验证