示例#1
0
        /// <summary>
        /// 关闭服务
        /// </summary>
        /// <returns>是否关闭</returns>
        public int close()
        {
            int ret = -1;

            if (m_socket != null)
            {
                try {
                    m_socket.Close();
                    ret = 1;
                } catch (Exception ex) {
                    byte[] rmsg = new byte[1];
                    rmsg[0] = (byte)((char)2);
                    FCClientSockets.recvClientMsg(m_hSocket, m_hSocket, rmsg, 1);
                    FCClientSockets.writeClientLog(m_hSocket, m_hSocket, 2, "socket exit");
                    ret = -1;
                }
            }
            if (m_udpSocket != null)
            {
                try {
                    m_udpSocket.Close();
                    ret = 1;
                } catch (Exception ex) {
                    byte[] rmsg = new byte[1];
                    rmsg[0] = (byte)((char)2);
                    FCClientSockets.recvClientMsg(m_hSocket, m_hSocket, rmsg, 1);
                    FCClientSockets.writeClientLog(m_hSocket, m_hSocket, 2, "udp exit");
                    ret = -1;
                }
            }
            m_connected = false;
            return(ret);
        }
示例#2
0
        /// <summary>
        /// 运行
        /// </summary>
        public void run()
        {
            byte[] str = null;
            bool   get = false;
            int    head = 0, pos = 0, strRemain = 0, bufferRemain = 0, index = 0, len = 0;
            int    intSize = 4;

            byte[]     headStr  = new byte[4];
            int        headSize = 4;
            IPEndPoint sender   = new IPEndPoint(IPAddress.Any, 0); //定义要发送的计算机的地址
            EndPoint   Remote   = (EndPoint)(sender);               //

            while (m_connected)
            {
                try {
                    byte[] buffer = new byte[1024];
                    if (m_type == 0)
                    {
                        len = m_socket.Receive(buffer);
                    }
                    else if (m_type == 1)
                    {
                        m_udpSocket.ReceiveFrom(buffer, ref Remote);
                    }
                    if (len == 0 || len == -1)
                    {
                        byte[] rmsg = new byte[1];
                        rmsg[0] = (byte)((char)3);
                        FCClientSockets.recvClientMsg(m_hSocket, m_hSocket, rmsg, 1);
                        FCClientSockets.writeClientLog(m_hSocket, m_hSocket, 3, "socket error");
                        break;
                    }
                    index = 0;
                    while (index < len)
                    {
                        int diffSize = 0;
                        if (!get)
                        {
                            diffSize = intSize - headSize;
                            if (diffSize == 0)
                            {
                                head = (((buffer[index] | (buffer[index + 1] << 8)) | (buffer[index + 2] << 0x10)) | (buffer[index + 3] << 0x18));
                            }
                            else
                            {
                                for (int i = 0; i < diffSize; i++)
                                {
                                    headStr[headSize + i] = buffer[i];
                                }
                                head = (((buffer[0] | (buffer[1] << 8)) | (buffer[2] << 0x10)) | (buffer[3] << 0x18));
                            }
                            if (str != null)
                            {
                                str = null;
                            }
                            str = new byte[head];
                            if (diffSize > 0)
                            {
                                for (int i = 0; i < headSize; i++)
                                {
                                    str[i] = headStr[i];
                                }
                                pos     += headSize;
                                headSize = intSize;
                            }
                        }
                        bufferRemain = len - index;
                        strRemain    = head - pos;
                        get          = strRemain > bufferRemain;
                        int remain = Math.Min(strRemain, bufferRemain);
                        Array.Copy(buffer, index, str, pos, remain);
                        pos   += remain;
                        index += remain;
                        if (!get)
                        {
                            FCClientSockets.recvClientMsg(m_hSocket, m_hSocket, str, head);
                            head = 0;
                            pos  = 0;
                            if (len - index == 0 || len - index >= intSize)
                            {
                                headSize = intSize;
                            }
                            else
                            {
                                headSize = bufferRemain - strRemain;
                                for (int j = 0; j < headSize; j++)
                                {
                                    headStr[j] = buffer[index + j];
                                }
                                break;
                            }
                        }
                    }
                } catch (Exception ex) {
                    break;
                }
            }
            byte[] rmsg2 = new byte[1];
            rmsg2[0] = (byte)((char)2);
            FCClientSockets.recvClientMsg(m_hSocket, m_hSocket, rmsg2, 1);
            FCClientSockets.writeClientLog(m_hSocket, m_hSocket, 2, "socket exit");
            m_connected = false;
        }