Exemple #1
0
        private SocketEntity(Socket listener, SocketConfig config)
        {
            _listener = listener;
            _config   = config;

            _timer          = new Timer(_config.ConnectionCollectionInterval);
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        }
Exemple #2
0
        public static SocketResult SendData(string ip, int port, string content, bool includeHeader)
        {
            SocketConfig config = new SocketConfig();

            config.IncludeHeader = includeHeader;
            config.IPAddress     = ip;
            config.Port          = port;

            return(SendData(config, content));
        }
Exemple #3
0
 internal static void SetLog(Object sender, SocketConfig config)
 {
     if (config == null)
     {
         return;
     }
     SetLog(SocketLogType.Debug, sender, "---------------------------------");
     SetLog(SocketLogType.Debug, sender, "IP: " + config.IPAddress);
     SetLog(SocketLogType.Debug, sender, "Port: " + config.Port.ToString());
     SetLog(SocketLogType.Debug, sender, "BackLog: " + config.BackLog.ToString());
     SetLog(SocketLogType.Debug, sender, "SendTimeout: " + config.SendTimeout.ToString());
     SetLog(SocketLogType.Debug, sender, "ReceiveTimeout: " + config.ReceiveTimeout.ToString());
     SetLog(SocketLogType.Debug, sender, "ConnectionTimeoutSecond: " + config.ConnectionTimeoutSecond.ToString());
     SetLog(SocketLogType.Debug, sender, "ReceiveEndSign: " + config.ReceiveEndSign);
     SetLog(SocketLogType.Debug, sender, "CodePageName: " + config.CodePageName);
     SetLog(SocketLogType.Debug, sender, "---------------------------------");
 }
Exemple #4
0
        public static SocketEntity Create(int port)
        {
            try
            {
                SocketConfig cfg = new SocketConfig();
                cfg.Port = port;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(IPAddress.Any, port));
                return(new SocketEntity(listener, cfg));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #5
0
        public static SocketEntity Create(string ip, int port)
        {
            if (ip == null || ip.Length < 1 || ip == "127.0.0.1")
            {
                return(Create(port));
            }
            try
            {
                SocketConfig cfg = new SocketConfig();
                cfg.IPAddress = ip;
                cfg.Port      = port;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
                return(new SocketEntity(listener, cfg));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #6
0
        public static SocketEntity Create(SocketConfig config)
        {
            try
            {
                int    port = config.Port;
                string ip   = config.IPAddress;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                if (ip == null || ip.Length < 1 || ip == "127.0.0.1")
                {
                    listener.Bind(new IPEndPoint(IPAddress.Any, port));
                }
                else
                {
                    listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
                }
                return(new SocketEntity(listener, config));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #7
0
 internal static void SetLog(SocketConfig config)
 {
     SetLog(null, config);
 }
Exemple #8
0
        public static byte[] GetResponseByteWithHeader(Encoding encoder, string strSend, SocketConfig config)
        {
            if (encoder == null || strSend == null || config == null)
            {
                return(null);
            }
            int    bcount = encoder.GetByteCount(strSend);
            string header = ResponseHeaderTemplate;

            header = header.Replace("[LENGTH]", bcount.ToString());
            header = header.Replace("[PORT]", config.Port.ToString());
            return(encoder.GetBytes(header + strSend));
        }
Exemple #9
0
        public static SocketResult SendData(SocketConfig config, string content)
        {
            SocketResult result = SocketResult.Empty;

            try
            {
                SocketLogMgt.SetLog("=============================================");
                SocketLogMgt.SetLog(": Send Data Begin.");
                SocketLogMgt.SetLog(config);

                string strSend = EnsureEOF(content, config.SendEndSign);

                if (SocketLogMgt.DumpData)
                {
                    SocketLogMgt.SetLog(": Data to be sent.");
                    SocketLogMgt.SetLog("------------------------");
                    SocketLogMgt.SetLog(strSend);
                    SocketLogMgt.SetLog("------------------------");
                }

                Byte[]   bytesSent = null;
                Encoding encoder   = GetEncoder(config.CodePageName);
                if (config.IncludeHeader)
                {
                    bytesSent = GetRequestByteWithHeader(encoder, strSend, config);
                }
                else
                {
                    bytesSent = encoder.GetBytes(strSend);
                }
                if (bytesSent == null)
                {
                    SocketLogMgt.SetLog(SocketLogType.Error, "Encode data failed.");
                    return(SocketResult.SendFailed);
                }

                Byte[] bytesReceived = new Byte[ReceiveResponseBufferSize];

                SocketLogMgt.SetLog(": Socket prepared.");
                SocketLogMgt.SetLog("------------------------");

                string        strReceived = null;
                StringBuilder sb          = new StringBuilder();
                IPEndPoint    ipe         = new IPEndPoint(IPAddress.Parse(config.IPAddress), config.Port);
                using (Socket socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
                {
                    int    id    = GetNewSocketID();
                    string strID = "(" + id.ToString() + ") ";

                    //socket.NoDelay = true;
                    socket.SendTimeout    = config.SendTimeout;
                    socket.ReceiveTimeout = config.ReceiveTimeout;

                    SocketLogMgt.SetLog(strID + "Socket created.");

                    socket.Connect(ipe);
                    if (socket.Connected)
                    {
                        SocketLogMgt.SetLog(strID + "Socket connected.");
                    }
                    else
                    {
                        SocketLogMgt.SetLog(SocketLogType.Warning, strID + "Connection failed.");
                        return(SocketResult.Disconnect);
                    }

                    _allDone.Reset();
                    SocketLogMgt.SetLog(strID + "Send data.");
                    socket.BeginSend(bytesSent, 0, bytesSent.Length, 0,
                                     new AsyncCallback(OnDataSent), new SocketWrapper(strID, socket));

                    bool rec = true;
                    while (rec)
                    {
                        if (!socket.Connected)
                        {
                            SocketLogMgt.SetLog(SocketLogType.Warning, strID + "Connection closed.");
                            break;
                        }

                        SocketLogMgt.SetLog(strID + "Receive data.");
                        int bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
                        SocketLogMgt.SetLog(strID + "Receive succeeded. " + bytes.ToString() + " bytes.");
                        string str = encoder.GetString(bytesReceived, 0, bytes);
                        sb.Append(str);

                        strReceived = sb.ToString();
                        rec         = !IsEOF(strReceived, config.ReceiveEndSign);
                    }

                    _allDone.WaitOne();

                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();

                    SocketLogMgt.SetLog(strID + "Socket disconnected.");
                }

                SocketLogMgt.SetLog("------------------------");

                if (SocketLogMgt.DumpData)
                {
                    SocketLogMgt.SetLog(": Data received.");
                    SocketLogMgt.SetLog("------------------------");
                    SocketLogMgt.SetLog(strReceived);
                    SocketLogMgt.SetLog("------------------------");
                }

                strReceived = GetMessageContent(strReceived);
                result      = new SocketResult(strReceived);
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                result = new SocketResult(err);
            }

            SocketLogMgt.SetLog(": Send Data End");
            SocketLogMgt.SetLog("=============================================\r\n");
            return(result);
        }