socket服务器部分代码如下:
byte[] byteMessage=new byte[1024];
private void BeginListen()
{
IPAddress ServerIp=GetServerIP();
IPEndPoint iep=new IPEndPoint(ServerIp,8000);
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); this.label1.Text=iep.ToString();
socket.Bind(iep);
socket.Blocking=false; // do
socket.Listen(10);
while(true)
{
try
{
newSocket=socket.Accept();
nThread=new Thread (new ThreadStart(dealSocket));
nThread.Start();
}
catch(Exception ex)
{
label1.Text=ex.Message.ToString();
}
}}
private void ReadCallback(IAsyncResult ar)
{
revc = nSocket.EndReceive(ar);
}
private void dealSocket()
{
bool alive=true;
nSocket=newSocket;
nSocket.Blocking=false;
while(alive)
{
try
{
nSocket.BeginReceive(byteMessage,0,byteMessage.Length,SocketFlags.None
,new AsyncCallback(ReadCallback),nSocket);
mainw(); /*同步方法*/
Thread.Sleep(1000);
}
catch(SocketException ex)
{
this.label1.Text+=ex.ToString();
} }
}
当socket客户端发送超过1024个字节的数据流时,同步方法所要处理的字节数组的数据有错误,
超过1024字节后的数据一直取到已经有数据 的byteMessage字节数组里,
在超过byteMessage字节数组的大小,怎样通过异步的方法处理完数据后再读后面的数据。
byte[] byteMessage=new byte[1024];
private void BeginListen()
{
IPAddress ServerIp=GetServerIP();
IPEndPoint iep=new IPEndPoint(ServerIp,8000);
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); this.label1.Text=iep.ToString();
socket.Bind(iep);
socket.Blocking=false; // do
socket.Listen(10);
while(true)
{
try
{
newSocket=socket.Accept();
nThread=new Thread (new ThreadStart(dealSocket));
nThread.Start();
}
catch(Exception ex)
{
label1.Text=ex.Message.ToString();
}
}}
private void ReadCallback(IAsyncResult ar)
{
revc = nSocket.EndReceive(ar);
}
private void dealSocket()
{
bool alive=true;
nSocket=newSocket;
nSocket.Blocking=false;
while(alive)
{
try
{
nSocket.BeginReceive(byteMessage,0,byteMessage.Length,SocketFlags.None
,new AsyncCallback(ReadCallback),nSocket);
mainw(); /*同步方法*/
Thread.Sleep(1000);
}
catch(SocketException ex)
{
this.label1.Text+=ex.ToString();
} }
}
当socket客户端发送超过1024个字节的数据流时,同步方法所要处理的字节数组的数据有错误,
超过1024字节后的数据一直取到已经有数据 的byteMessage字节数组里,
在超过byteMessage字节数组的大小,怎样通过异步的方法处理完数据后再读后面的数据。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货