Ejemplo n.º 1
0
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="tcpClient"></param>
        /// <param name="toSend">发送内容</param>
        /// <param name="encoding">默认UTF-8</param>
        public static void Send(System.Net.Sockets.TcpClient tcpClient, string toSend, System.Text.Encoding encoding = null)
        {
            if (tcpClient == null)
            {
                return;
            }

            if (tcpClient.Connected == false)
            {
                return;
            }

            System.Net.Sockets.NetworkStream ns = tcpClient.GetStream();

            // 内容转换
            byte[] strBuffer = null;
            if (encoding != null)
            {
                strBuffer = encoding.GetBytes(toSend);
            }
            else
            {
                strBuffer = Encoding.UTF8.GetBytes(toSend);
            }

            // 发送
            TcpClientModel socketModel = new Util.Web.TcpClientModel(strBuffer);

            byte[] buffer = socketModel.ToByteArray();
            ns.Write(buffer, 0, buffer.Length);

            buffer = null;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="tcpClient"></param>
        /// <param name="toSend">发送内容</param>
        /// <param name="encoding">默认UTF-8</param>
        public static void Send(System.Net.Sockets.TcpClient tcpClient, byte[] toSend)
        {
            if (tcpClient == null)
            {
                return;
            }

            if (tcpClient.Connected == false)
            {
                return;
            }

            System.Net.Sockets.NetworkStream ns = tcpClient.GetStream();

            // 发送
            TcpClientModel socketModel = new Util.Web.TcpClientModel();

            socketModel.Content = toSend;
            byte[] buffer = socketModel.ToByteArray();
            ns.Write(buffer, 0, buffer.Length);

            buffer = null;
        }