各位,我现在在做一个客户端程序,用于接收从服务器发送过来的数据并进行解析保存等操作。。但经常会出现接收不到的问题,先把主要代码贴下来求指导class ClientCommunication
{ public static List<byte> tempAllDataBytes = new List<byte>();
//一轮接收完后共接收到的所有待处理数据
private Socket muClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] muReceiveBytes = new byte[2048];
private IPAddress muIPAddress;
private IPEndPoint muIPEndPoint;
public static bool isConnected = false; public event EventHandler GetData;/// 接收到数据事件 public ClientCommunication(string ipAddress, int iport)
{
try
{
muIPAddress = IPAddress.Parse(ipAddress);
muIPEndPoint = new IPEndPoint(muIPAddress, iport);
}
catch (Exception e)
{略 }
}
public void RaiseGetData()
{
if (GetData != null)
{
GetData(this, new EventArgs());
}
}
public bool ConnectServer()
{
try
{
muClientSocket.Connect(muIPEndPoint);
return true;
}
catch
{
return false;
}
}
public void ShutDownConnect()
{
if (muClientSocket.Connected)
{
muClientSocket.Close();
}
}
public bool SendMessage(byte[] osMessage)
{省略 }
public void ReceiveBegin()/// 开始接收消息
{
muClientSocket.BeginReceive(muReceiveBytes, 0, muReceiveBytes.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallBack), muClientSocket);
}
private void ReceiveMessageCallBack(IAsyncResult ar)
{
Socket tvSocket = ar.AsyncState as Socket;
string receiveString = null;//存放"CloseSocket"字符串
if (tvSocket != null)
{
try
{
int ReceiveCount = tvSocket.EndReceive(ar);
byte[] buffer = new byte[ReceiveCount];
Array.Copy(muReceiveBytes, 0, buffer, 0, ReceiveCount);
receiveString = Encoding.UTF8.GetString(muReceiveBytes,0,ReceiveCount);
if (receiveString == "CloseSocket")
{
Array.Clear(buffer, 0, buffer.Length);
Array.Clear(muReceiveBytes, 0, muReceiveBytes.Length);
}
else
{
tempAllDataBytes.AddRange(buffer); Array.Clear(buffer, 0, buffer.Length);
Array.Clear(muReceiveBytes, 0, muReceiveBytes.Length);
RaiseGetData();
tempAllDataBytes.Clear();
}
tvSocket.BeginReceive(muReceiveBytes, 0, muReceiveBytes.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallBack), tvSocket);
}
catch
{
//无法接收消息很大可能是网络中断.
//mucSocket.Remove(tcSocket);
tvSocket.Close();
return;
}
}
}
以上是接收到数据,然后通过
muClientCommunication.GetData += new EventHandler(muClientCommunication_GetData);转到muClientCommunication_GetData()进行相应的数据解析,保存和显示。。但是收到一些数据之后,该客户端收不到数据了,我检查过服务器,服务器确实成功的发送了。。
{ public static List<byte> tempAllDataBytes = new List<byte>();
//一轮接收完后共接收到的所有待处理数据
private Socket muClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] muReceiveBytes = new byte[2048];
private IPAddress muIPAddress;
private IPEndPoint muIPEndPoint;
public static bool isConnected = false; public event EventHandler GetData;/// 接收到数据事件 public ClientCommunication(string ipAddress, int iport)
{
try
{
muIPAddress = IPAddress.Parse(ipAddress);
muIPEndPoint = new IPEndPoint(muIPAddress, iport);
}
catch (Exception e)
{略 }
}
public void RaiseGetData()
{
if (GetData != null)
{
GetData(this, new EventArgs());
}
}
public bool ConnectServer()
{
try
{
muClientSocket.Connect(muIPEndPoint);
return true;
}
catch
{
return false;
}
}
public void ShutDownConnect()
{
if (muClientSocket.Connected)
{
muClientSocket.Close();
}
}
public bool SendMessage(byte[] osMessage)
{省略 }
public void ReceiveBegin()/// 开始接收消息
{
muClientSocket.BeginReceive(muReceiveBytes, 0, muReceiveBytes.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallBack), muClientSocket);
}
private void ReceiveMessageCallBack(IAsyncResult ar)
{
Socket tvSocket = ar.AsyncState as Socket;
string receiveString = null;//存放"CloseSocket"字符串
if (tvSocket != null)
{
try
{
int ReceiveCount = tvSocket.EndReceive(ar);
byte[] buffer = new byte[ReceiveCount];
Array.Copy(muReceiveBytes, 0, buffer, 0, ReceiveCount);
receiveString = Encoding.UTF8.GetString(muReceiveBytes,0,ReceiveCount);
if (receiveString == "CloseSocket")
{
Array.Clear(buffer, 0, buffer.Length);
Array.Clear(muReceiveBytes, 0, muReceiveBytes.Length);
}
else
{
tempAllDataBytes.AddRange(buffer); Array.Clear(buffer, 0, buffer.Length);
Array.Clear(muReceiveBytes, 0, muReceiveBytes.Length);
RaiseGetData();
tempAllDataBytes.Clear();
}
tvSocket.BeginReceive(muReceiveBytes, 0, muReceiveBytes.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallBack), tvSocket);
}
catch
{
//无法接收消息很大可能是网络中断.
//mucSocket.Remove(tcSocket);
tvSocket.Close();
return;
}
}
}
以上是接收到数据,然后通过
muClientCommunication.GetData += new EventHandler(muClientCommunication_GetData);转到muClientCommunication_GetData()进行相应的数据解析,保存和显示。。但是收到一些数据之后,该客户端收不到数据了,我检查过服务器,服务器确实成功的发送了。。
解决方案 »
- C# 编写exe程序管理器!!!
- winform选中excel
- 求 .net 的语法高亮控件
- 使用DevComponents.DotNetBar出现问题,请求解决....
- 推荐一个好网站 http://www.yiya.org/?r=xhjdxx
- c#中调用c++dll时c#怎么定义结构,谢谢?
- 用代码实现还原数据库,该数据库处于脱机状态,请高手请教。
- 如何在asp中调用.net写的类
- 关键词:GDI、OnPaint、AutoScroll、滚动、重绘
- 如何在页面上实现象excel那样左边固定栏不动,横向滚动条变动时指标字段也在变动!
- System.Windows.Forms里的TIMER
- C#程序题,快,急
{
//无法接收消息很大可能是网络中断.
//mucSocket.Remove(tcSocket);
tvSocket.Close();
return;
}
有可能是报异常的时候 把Socket处理掉了