none
SOCKET PROBLEM? RRS feed

  • 問題

  • 各位好,

    本人寫了一個利用tcpclient的socket程式,但不知為什麼每當連線到server後進行到receiveSocket() function 之後就出現下面的exception,希望有朋友可以幫忙解決。

    Exception:

    A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
    Receive Socket Error: System.Threading.ThreadAbortException: Thread was being aborted.
       at System.IO.__ConsoleStream.WriteFile(SafeFileHandle handle, Byte* bytes, Int32 numBytesToWrite, Int32& numBytesWritten, IntPtr mustBeZero)
       at System.IO.__ConsoleStream.WriteFileNative(SafeFileHandle hFile, Byte[] bytes, Int32 offset, Int32 count, Int32 mustBeZero, Int32& errorCode)
       at System.IO.__ConsoleStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
       at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at System.IO.TextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
       at System.Console.WriteLine(String value)
       at Xilath.GameObject.GameSocket.receiveSocket() in C:\WindowsApplication1\WindowsApplication1\GameObject\GameSocket.cs:line 93
    Thread - caught ThreadAbortException - resetting.
    Exception message: Thread was being aborted.

     

    Coding

    NetworkStream ns;
    StreamReader sr;
    TcpClient clientsocket;
    bool connected;
    Thread receive;
    string serveraddress = "127.0.0.1";
    int serverport = 4002;
    Form form;
    bool readData;
    public String errorMessage;       

            public GameSocket(Form form)
            {
                this.form = form;
                Connect();
               
            }
            private void Connect()//<===========35
            {
                socketConnection();
                //Send_Message("newuser");
                //RegisterWithServer();
                if (connected)
                {
                    //Console.WriteLine("Receive");
                    ThreadStart myThreadDelegate = new ThreadStart(receiveSocket);
                    receive = new Thread(new ThreadStart(myThreadDelegate));
                    receive.Start();
                    Thread.Sleep(100);
                    receive.Abort();
                    receive.Join();
                }
            }//<==================47
            public void socketConnection()
            {
                try
                {
                    clientsocket = new TcpClient(serveraddress, serverport);
                    ns = clientsocket.GetStream();
                    sr = new StreamReader(ns);
                    connected = true;

                }
                catch (Exception e)
                {
                    //Thread.ResetAbort();

                    Console.WriteLine("Connect Error: " + e);
                }
            }
            private void receiveSocket() //65
            {
                try
                {


                    /*if (ns.DataAvailable)
                    {
                        if (ns.CanRead)
                        {
                            Byte[] buffer = new Byte[ns.Length];
                            ns.Read(buffer, 0, buffer.Length);
                            string chatter = System.Text.Encoding.ASCII.GetString(buffer);
                            string[] tokens = chatter.Split(new Char[] { ',' });
                            if (tokens[0] == "CHAT")
                            {

                            }
                        }
                    }*/
                    while (!readData && ns.CanRead)
                    {
                        //if data available then read from the stream
                        Console.WriteLine("Running");

                        if (ns.DataAvailable)
                        {
                            Console.WriteLine("0");
                            errorMessage = "connect data";
                            form.Dispose();
                            Console.WriteLine("0");
                            Byte[] buffer = new Byte[2048];
                            ns.Read(buffer, 0, buffer.Length);
                            string chatter = System.Text.Encoding.ASCII.GetString(buffer);
                            string[] tokens = chatter.Split(new Char[] { ',' });

                            //Console.WriteLine("4" + tokens[0]);
                            //Console.WriteLine("4" + tokens[1]);

                           /* if (tokens[0] == "CHAT")
                            {

                            }*/ 
                            readData = true;
                        }
                    }

                }
                catch(ThreadAbortException e)
                {
                    /* StackTrace st = new StackTrace(true); // true means get line numbers.
                     foreach(StackFrame f in st.GetFrames()){
                           Console.Write("CAll Stack: "+f);
                     }*/
                     Console.WriteLine("Receive Socket Error: " + e);
                    Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
                    Console.WriteLine("Exception message: {0}", e.Message);
                    errorMessage = e.Message;
                    Thread.ResetAbort();
                }
                Console.WriteLine("Thread - still alive and working.");
               // Thread.Sleep(1000);
                Console.WriteLine("Thread - finished working.");
            }//99

     

     

    2006年12月7日 下午 01:59

所有回覆

  • 上面的兄弟(brian_tsim),你寫這 Socket 功力應該不錯,但我覺得你這樣寫如果是實驗的東西還可以,但要快速的開發一個有用的商品有一點困難,我曾經也用Socket 去拼一支多方通訊像MSN這總功能,稿一個月只能做到五方通訊,超過五方問題就一堆了,當然其中也不是只用Socket而已,而就要寫一支通訊程式會有很多不穩定的因素,基本上光是網路本身上就會有不穩不通的情形,雙方訊息格式如何,怎麼開始怎麼結束,訊息Loss 怎麼補送等等,而就Socket 方面那時我自己玩一星期,就玩不下去了,於是就上網找通訊元件公司下載試用版的元件,真的整個程式變穩定許多,也簡單許多,接著在花兩個星期時間將試用版破解變成自己的元件庫,網路層完成,接著做訊息層,一對一ok,一對二ok,一對三開始有問題,一對n...,複雜度會跟通訊點數成指數成長,所以如果你要做大型程式如果我目前要做的話,先停下來k WCF,然後直接上 WCF 比較實際.
    2006年12月8日 上午 12:34
  •  

    可以介紹有什麼通訊元件嗎?

    2008年12月25日 下午 07:06