1)既然是重写,我建议你用异步把,不要开线程
2)把ReadThread里面异常的ex.message打印出来,不一定是socket错误,有可能是你处理过程错误
2)把ReadThread里面异常的ex.message打印出来,不一定是socket错误,有可能是你处理过程错误
解决方案 »
- 如何在gridview前加一个序号,要求添加数据时编号自动加1,删除数据时编号减1,无论从那儿删除序号都是顺序排列!
- C# 中怎样判断一个类的属性是否被赋值??
- 有哪个大虾帮我做这些题目啊!求求你们了!急等啊!在线等!
- js单击treeview节点!
- 请教:如何将word文档转换为PDF格式的文档?
- 求助银联支付接口问题
- ASP.NET和PHP哪个简单,易学,上手快,好找工作???/
- datagrid 的列标题怎么得到?
- 关于一个 算法 的问题,请高手帮忙,谢谢!
- 请问如何让我的Rose2002能够导出C#代码?
- 在web brower 嵌套的网页中启动过本地程序
- 急需高手解决,uploadify遇到上传比较诡异问题,报Security Error
异常信息就是这个
“服务程序接收数据时出错:无法访问已释放的对象。
对象名:“System.Net.Sockets.Socket”。——01/23/2014 08:43:04”我还发现有时候没有执行catch里面的代码,而是直接执行了finally的了
2、设备使用的有线网络还是无线(这里指类似GPRS)之类的
...
要综合考虑。 程序部分用线程也无可厚非,异步监听一个端口。
2,是GPRS网络
2,是GPRS网络
既然有重连机制,又使用的是GPRS网络,那在极限情况下,比如信号不好的地方 基站少的地方 出现连不上服务器的情况自动重连很正常。 是GPS 设备吧!
嗯,改异步监听端口!
开始
public void Start()
{
_svrSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_svrSock.Bind(iep);
_svrSock.Listen(500);
BeginAccept();
}
{
try
{
if (_isRun)
_svrSock.BeginAccept(new AsyncCallback(EndAccept), _svrSock);
}
catch
{ }
}private void EndAccept(IAsyncResult iar)
{
Socket client = null;
try
{
client = _svrSock.EndAccept(iar);
}
catch
{
}
BeginAccept();
try
{
Session newSession = null;
newSession = new Session(client);
_sessionTable.Add(newSession.ID, newSession);
BeginReceive(newSession);
}
catch (Exception ex)
{
Console.WriteLine("EndAccept " + ex.Message);
}
}
{
try
{
if (IsRun)
{
newSession.ClientSocket.BeginReceive(newSession.Buffer, 0, newSession.Buffer.Length, SocketFlags.None,
new AsyncCallback(ReceiveData), newSession);
}
}
catch (Exception ex)
{
//Log.TestAddLog("tcpsvr BeginReceive" + ex.Message);
CloseIPEndPoint(newSession.ID, ExitType.ExceptionExit);
}
} protected virtual void ReceiveData(IAsyncResult iar)
{
//Socket client=null ;;
//IPEndPoint ipe=null ;
Session sendDataSession = null;
try
{
sendDataSession = (Session)iar.AsyncState;
if (!_sessionTable.ContainsKey(sendDataSession.ID))
{
sendDataSession.Close();
return;
}
}
catch (Exception ex)
{
return;
}
int recv = 0;
try
{
recv = sendDataSession.ClientSocket.EndReceive(iar);
if (recv == 0)
{
//正常的关闭
CloseIPEndPoint(sendDataSession.ID, ExitType.NormalExit);
return;
}
sendDataSession.LastDt = DateTime.Now;
}
catch (Exception ex)
{
//Log.TestAddLog("tcp_EndReceive " + ex.Message);
CloseIPEndPoint(sendDataSession.ID, ExitType.ExceptionExit);
return;
}
try
{
byte[] buff = new byte[recv];
Array.Copy(sendDataSession.Buffer, 0, buff, 0, recv);
reciveData(this, sendDataSession.ID, buff);
}
catch (Exception ex)
{
Log.TestAddLog("tcp_ReceiveData reciveData" + ex.Message);
} BeginReceive(sendDataSession); }
这里所有的client都是存放到session里面的,那就是说你的这个程序是Web上面接收的,是不是这样呢?
public class Session
{
#region 属性
private Socket _cliSock;
private IPEndPoint _id;
private byte[] _buffer;
public IPEndPoint ID
{
get
{
return _id;
}
}
public Socket ClientSocket
{
get
{
return _cliSock;
}
}
public byte[] Buffer {
get {
return _buffer;
}
}
public override int GetHashCode()
{
return (int)_cliSock.Handle;
}
public override bool Equals(object obj)
{
Session rightObj = (Session)obj; return (int)_cliSock.Handle == (int)rightObj.ClientSocket.Handle; }
public override string ToString()
{
string result = string.Format("Session:{0},IP:{1}",
_id, _cliSock.RemoteEndPoint.ToString()); //result.C
return result;
}
public DateTime LastDt
{
get;
set;
}
#endregion 属性
private object SendObj = new object();
private bool _isRun = false;
public Session(Socket cliSock)
{
_cliSock = cliSock;
_id = (IPEndPoint)cliSock.RemoteEndPoint;
_buffer = new byte[256];
LastDt = DateTime.Now;
_isRun = true;
}
public void SendByte(byte[] buff)
{
ThreadPool.QueueUserWorkItem(new WaitCallback( obj=>{
try
{
lock (SendObj)
{
if (_cliSock != null && _cliSock.Connected)
{
_cliSock.Send(buff);
}
}
}
catch (Exception ex)
{
//Log.TestAddLog("Session SendByte " + ex.Message);
}
}
) );
}
protected virtual void SendDataEnd(IAsyncResult iar)
{
try
{
if (iar == null || iar.AsyncState == null) return;
Socket client = (Socket)iar.AsyncState;
int sent = client.EndSend(iar); }
catch (Exception ex)
{
Log.TestAddLog("Session SendDataEnd " + ex.Message);
}
finally
{
}
}
public void Close()
{
_isRun = false;
try
{
if (_cliSock == null) return;
//关闭数据的接受和发送
if (_cliSock.Connected)
{
_cliSock.Shutdown(SocketShutdown.Both);
//清理资源
}
_cliSock.Close();
}
catch(Exception ex)
{
Log.TestAddLog("Session Close :" + ex.Message);
} }
}
这个用来存放client list
_sessionTable = Hashtable.Synchronized(new Hashtable());
{
try
{
//Console.WriteLine("CloseIPEndPoint " + ipe.ToString());
Session recvDataClient = FindSession(ipe);
if (recvDataClient == null) return;
_sessionTable.Remove(recvDataClient.ID);
recvDataClient.Close();
recvDataClient = null;
}
catch (Exception ex)
{
Log.TestAddLog("tcp_CloseIPEndPoint " + ex.Message);
}
}
public override void CloseIPEndPoint(IPEndPoint ipe)
{
CloseIPEndPoint(ipe,ExitType.NormalExit);
}