//接收和发送信息 private void ReceiveData() { bool keepalive = true; TcpClient s = clientTcp; NetworkStream ns = s.GetStream(); //可以获取网络流对象,以此来发送和接收信息 byte[] buffer = new byte[s.Available]; //根据收到的客户端套接字向客户端发送消息 //IPEndPoint clientiep = (IPEndPoint)s.Client.RemoteEndPoint; //获取远程终结点 //string welcome = "已收到!"; //byte[] data = new byte[1024]; //data = Encoding.ASCII.GetBytes(welcome); //ns.Write(data, 0, data.Length); screenStr = ""; //暂存一次截屏的内容 while(keepalive) { //在套接字上接收客户端发送的消息 int bufLen = 0; try { bufLen = s.Available; ns.Read(buffer, 0, bufLen); //来到这里出错:超出有效值范围size screenStr += Encoding.ASCII.GetString(buffer); if (bufLen == 0) continue; } catch(Exception ex) { MessageBox.Show("server Receive Error:" + ex.Message); return; } } //接收完处理显示截屏 //screenByte = Encoding.ASCII.GetBytes(screenStr); showImg simg = new showImg(showScreen); isInvoke(simg); }
ns.Read(buffer, 0, bufLen); //来到这里出错:超出有效值范围size
客户端是截屏将bitmap转换为byte[],大概二十万左右字节,发送到服务端一次好像是接收到8192字节,然后就报错了。
刚自学socket编程,请高手指点迷津,怎样解决?
你的ns的最大范围是多少? 是不是截屏的byte[]超标了 。。。