1)既然是重写,我建议你用异步把,不要开线程
2)把ReadThread里面异常的ex.message打印出来,不一定是socket错误,有可能是你处理过程错误

解决方案 »

  1.   

    我之前那一个就是异步,感觉不太会用,不是很好理解,所以才换了线程池的。
    异常信息就是这个
    “服务程序接收数据时出错:无法访问已释放的对象。
    对象名:“System.Net.Sockets.Socket”。——01/23/2014 08:43:04”我还发现有时候没有执行catch里面的代码,而是直接执行了finally的了
      

  2.   

    1、设备有没有自动重连机制,
    2、设备使用的有线网络还是无线(这里指类似GPRS)之类的
    ...
    要综合考虑。 程序部分用线程也无可厚非,异步监听一个端口。
      

  3.   

    1,设备是有重连机制的
    2,是GPRS网络
      

  4.   

    1,设备是有重连机制的
    2,是GPRS网络
    既然有重连机制,又使用的是GPRS网络,那在极限情况下,比如信号不好的地方  基站少的地方  出现连不上服务器的情况自动重连很正常。  是GPS 设备吧!
      

  5.   

    这个是太过于频繁了,我用网络调试助手看了,没有那么多重连现象的,所以问题还是出在程序处理上。不是GPS,一般的数据传送设备。
      

  6.   

    这个是太过于频繁了,我用网络调试助手看了,没有那么多重连现象的,所以问题还是出在程序处理上。不是GPS,一般的数据传送设备。
    嗯,改异步监听端口!
      

  7.   

    我上个月写SOCKET群聊 发现也是漏数据或者收不到的情况。采用异步发送和接受  就解决了不少问题。还是用异步吧。
      

  8.   

    能发个服务器程序来看看吗,确实头大得很
    开始
     public  void Start()
            {
                _svrSock = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp); 
                _svrSock.Bind(iep);
                _svrSock.Listen(500);
                BeginAccept();
    }
      

  9.   

      private void 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);
                }
              
            }
      

  10.   

    private void BeginReceive(Session newSession)
            {
                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);        }
      

  11.   

    感谢,但是我发现有多变量定义都没有,这个函数CloseIPEndPoint定义也没有。
    这里所有的client都是存放到session里面的,那就是说你的这个程序是Web上面接收的,是不是这样呢?
      

  12.   

    session 定义
    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);
                }        }
        }
      

  13.   

     protected Hashtable _sessionTable;
    这个用来存放client list
     _sessionTable = Hashtable.Synchronized(new Hashtable());
      

  14.   

     public   void CloseIPEndPoint(IPEndPoint ipe, ExitType exittype)
            {
                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);
               
            }
      

  15.   

    一般耗时的操作我都用backgroundworker来实现
      

  16.   

    不好意思,我又碰到问题了,Session类的没有什么问题,但是我发现你刚开始给出的方法如Start,BeginAccept等应该是定义在不同的类中的吧,而且定义的有些变量也不是Session中的,比如说iep和_svrSock变量,ExitType类型和FindSession方法,如果您有源码,方便发我一份吗,.如果不行的话可不可以给我解释一下这几个问题,麻烦了,我问题太多,请不要介意,多谢。