• Soket 连接发送与接收


    ///url:地址     data:数据
     public static string GetContentFromUrl(string url, string data)
        {                               //寻址方案。ip4地址          // 套接字类型
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
                IPEndPoint ip = new IPEndPoint(IPAddress.Parse(url), 端口);
                socket.Connect(ip);//连接
                char[] cmgs = data.ToCharArray(); //字符串转字符数组
                byte[] message = new byte[1024];
                message = Encoding.UTF8.GetBytes(data);
                socket.Send(message);   //发送数据
           
                byte[] bytes = new byte[1024];
                socket.Receive(bytes);   //接收数据
                string messages = Encoding.Default.GetString(bytes, 0, bytes.Length);
    
                return messages;   //返回数据
           
                socket.Close();
          
    
    
        }
    

      

     /// <summary>
        /// 连接服务 接收大数据量
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GetContentFromUrl(string url, string data)
        {
            Socket sokClient = null;
            string Message = "";
            try
            {
               sokClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress address = IPAddress.Parse(url);
                IPEndPoint point = new IPEndPoint(address, 8108);
                sokClient.Blocking = true;   //没有数据接受时挂起等待, 知道有数据时
                sokClient.Connect(point);
    
                char[] cmgs = data.ToCharArray(); //字符串转字符数组
                byte[] message = new byte[1024];
                message = Encoding.Default.GetBytes(data);
                sokClient.Send(message);
    

    byte[] totalBytes = null; int receiveLength = 0; do { byte[] buffer = new byte[1024]; receiveLength = sokClient.Receive(buffer, buffer.Length,0); //接受数据长度 if (totalBytes == null) //第一次接收数据 { totalBytes = new byte[receiveLength]; //声明byte
    //从指定数组的 位置 复制到另一数组的指定位置 Buffer.BlockCopy(buffer,
    0, totalBytes, 0, receiveLength); } else { byte[] appendBytes = new byte[receiveLength]; Buffer.BlockCopy(buffer, 0, appendBytes, 0, receiveLength); byte[] newTotalBytes = new byte[receiveLength + totalBytes.Length]; Buffer.BlockCopy(totalBytes, 0, newTotalBytes, 0, totalBytes.Length); Buffer.BlockCopy(appendBytes, 0, newTotalBytes, totalBytes.Length, appendBytes.Length); totalBytes = newTotalBytes; } } while (receiveLength > 0 && receiveLength == 1024); Message = Encoding.Default.GetString(totalBytes); return Message; } catch (Exception ex) { return ex.Message; } finally { sokClient.Close(); } }
  • 相关阅读:
    GX转账站点无法访问的问题

    .NET易忘备留 ORACLE存储过程调用
    Oracle 字符串函数
    Oracle 数值函数
    AJAX.JSONP 跨域
    机器人部署的注意事项
    IE6、7绝对定位层被遮挡的原因(主要是父层决定的)
    Oracle 新手问答
    字符设备驱动范例
  • 原文地址:https://www.cnblogs.com/Evaniko/p/3779703.html
Copyright © 2020-2023  润新知