Ejemplo n.º 1
0
        public virtual void tryConnect(linkCustomer other)
        {
            string ip     = other.linkedEndPoint.Address.ToString(); //((IPEndPoint)other.linkedSocket.RemoteEndPoint).Address.ToString();
            int    port   = other.linkedEndPoint.Port;               //((IPEndPoint)other.linkedSocket.RemoteEndPoint).Port;
            string packet = "2~" + ip + "," + port + "|";

            Socket.Send(Encoding.UTF8.GetBytes(packet));
        }
Ejemplo n.º 2
0
        void WatchConnecting()
        {
            Console.WriteLine("開啟監聽......");
            Thread lkthread = new Thread(waitLinked);

            lkthread.IsBackground = true;
            lkthread.Start(LinkedSocket);

            //持續不斷監聽客戶端發來的請求
            while (true)
            {
                Customer newCustomer = null;
                // string cID;
                try
                {
                    Socket connection = SocketWatch.Accept();

                    bool find = false;
                    for (int id = 0; id < MAX_CONNECT_NUM; id++)
                    {
                        if (customerList[id] == null)
                        {
                            newCustomer = new linkCustomer(id, connection);
                            //Console.WriteLine("找到id:"+id+"newCustomer:"+ newCustomer);
                            find             = true;
                            customerList[id] = newCustomer;
                            Thread thread = new Thread(recv);
                            //設定為後臺執行緒,隨著主執行緒退出而退出
                            thread.IsBackground = true;
                            //啟動執行緒
                            Console.WriteLine("new customer ip:" + ((IPEndPoint)connection.RemoteEndPoint).Address + " port:" + ((IPEndPoint)connection.RemoteEndPoint).Port);
                            thread.Start(newCustomer);
                            break;
                        }
                    }
                    if (!find)
                    {
                        Console.WriteLine("connect close");
                        connection.Shutdown(SocketShutdown.Both);
                        connection.Close();
                    }
                }
                catch (Exception ex)
                {
                    //提示套接字監聽異常
                    Console.WriteLine(ex.Message);
                    break;
                }
                //byte[] idrec = new byte[1024 * 1024];
                //int length = connection.Receive(idrec);
                //var cID = Encoding.UTF8.GetString(idrec, 0, length);
                //ClientConnectionItems.Add(cID, connection);
                //顯示與客戶端連線情況
                //Console.WriteLine("\r\n[客戶端\"" + cID + "\"建立連線成功! 客戶端數量:" + ClientConnectionItems.Count + "]");
                //獲取客戶端的IP和埠號
                //IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
                //int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;
                //string sendmsg = "[" + "本端:" + cID + " 連線服務端成功!]";
                //byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg);
                //connection.Send(arrSendMsg);
                //建立一個通訊執行緒
            }
        }
Ejemplo n.º 3
0
        void recv(object socketclientpara)
        {
            linkCustomer customer = (linkCustomer)socketclientpara;

            Console.WriteLine("customer:" + customer);
            while (true)
            {
                try
                {
                    customer.listening();
                    //customer.encodePacket
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception:" + e.Message);
                    Console.WriteLine("customer" + customer.Id + "斷開連接");
                    customer.CloseSocket();
                    customerList[customer.Id] = null;
                    if (tableList.ContainsKey(customer.Id))
                    {
                        tableList.Remove(customer.Id);
                    }
                    break;
                }
            }

            /*
             * Socket socketServer = socketclientpara as Socket;
             *
             * while (true)
             * {
             *  try
             *  {
             *      byte[] arrServerRecMsg = new byte[1024 * 1024];
             *      int length = socketServer.Receive(arrServerRecMsg);
             *      //將機器接受到的位元組陣列轉換為人可以讀懂的字串
             *      string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
             *      string ccID = strSRecMsg.Substring(0, 4);
             *      int len = strSRecMsg.Length;
             *      string hc = ccID + ":" + strSRecMsg.Substring(4, len - 4);
             *      Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "]\r\n" + strSRecMsg);
             *      //判斷是否包含這個客戶端
             *      bool contains = ClientConnectionItems.ContainsKey(ccID);
             *      if (contains)
             *      {
             *          ClientConnectionItems[ccID].Send(Encoding.UTF8.GetBytes(hc));
             *      }
             *      else
             *      {
             *          Console.WriteLine("輸入有誤,不予轉發\r\n");
             *      }
             *      arrServerRecMsg.DefaultIfEmpty();
             *  }
             *  catch (Exception)
             *  {
             *      string temp = ClientConnectionItems.First().Key;
             *      //提示套接字監聽異常
             *      Console.WriteLine("\r\n[客戶端\"" + socketServer.RemoteEndPoint + "\"已經中斷連線! 客戶端數量:" + ClientConnectionItems.Count + "]");
             *      ClientConnectionItems.Remove(ClientConnectionItems.First().Key);
             *      Console.WriteLine("\r\n[客戶端\"" + temp + "\"已經中斷連線! 客戶端數量:" + ClientConnectionItems.Count + "]");
             *      break;
             *  }
             * }*/
        }