c# socket連接和socket監聽

封裝的socket類

 class SocketClient
    {
        Socket skt;
        IPEndPoint ipEndPoint;

        public SocketClient(string ServerIpAddr, int Port)
        {
            IPAddress ipAddress = IPAddress.Parse(ServerIpAddr);
            ipEndPoint = new IPEndPoint(ipAddress, Port);
            skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        ~SocketClient()
        {
            try
            {
                skt.Shutdown(SocketShutdown.Both);
                skt.Close();
            }
            catch (Exception ex)
            {

            }
        }

        public bool Connect()
        {
            try
            {
                skt.Connect(ipEndPoint);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SendData(int SendBufferLen, byte[] SendBuffer)
        {
            try
            {
                int ret = skt.Send(SendBuffer, SendBufferLen, SocketFlags.None);
                if (!skt.Connected)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SendData(string SendString)
        {
            try
            {
                byte[] SendBuffer = Encoding.Default.GetBytes(SendString);
                int ret = skt.Send(SendBuffer);
                if (!skt.Connected)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool RecvData(byte[] RecvBuffer, ref int RecvBufferLen)
        {
            try
            {
                RecvBufferLen = skt.Receive(RecvBuffer);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool RecvData(byte[] RecvBuffer, int BufferOffset, int NeedRecvLen, ref int RecvBufferLen)
        {
            try
            {
                RecvBufferLen = skt.Receive(RecvBuffer, BufferOffset, NeedRecvLen, SocketFlags.None);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool RecvData(ref string RecvString)
        {
            try
            {
                byte[] RecvBuffer = new byte[1024 * 1024];
                int RecvBufferLen = skt.Receive(RecvBuffer);
                RecvString = Encoding.Default.GetString(RecvBuffer, 0, RecvBufferLen);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DisConnect()
        {
            try
            {
                if (skt != null)
                {
                    skt.Shutdown(SocketShutdown.Both);
                    skt.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

調用socket實現socket連接

SocketClient sc = new SocketClient("127.0.0.1", 60000);
sc.Connect();//進行連接
//然後可以收發數據

實現socket監聽服務

 		Socket acceptSocket = null;
 		Socket listenSocket = null;
 		/// <summary>
        /// 監聽請求
        /// </summary>
        /// <param name="port"></param>
        public int Listen(IPAddress ip, int port)
        {
            listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(new IPEndPoint(ip, port));
            listenSocket.Listen(100);
            try
            {
                    acceptSocket = listenSocket.Accept();
                    return 0;
            }
            catch (Exception ex)
            {
                    DestroySocket(acceptSocket);
                    return -1;
            }
       }
 		/// <summary>
        /// 銷燬Socket對象
        /// </summary>
        /// <param name="socket"></param>
        private static void DestroySocket(Socket socket)
        {
            if (socket.Connected)
            {
                socket.Shutdown(SocketShutdown.Both);
            }
            socket.Close();
        }
        //然後可以收發數據
        acceptSocket.Send(data);
        acceptSocket.Receive(data);
           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章