コード例 #1
0
        public bool Send(string host, int listenerPortOfServer, string[] commands, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, int timeoutInMilliseconds)
        {
            var result = false;

            Socket      socket     = null;
            TcpListener dataSocket = null;

            try
            {
                var ipAddress = GetIpAddress();
                if (ipAddress == null)
                {
                    return(false);
                }

                dataSocket = new TcpListener(IPAddress.Parse(ipAddress), 0);
                dataSocket.Start();

                var ip = ((IPEndPoint)dataSocket.LocalEndpoint).Address;

                socket = new Socket(addressFamily, socketType, protocolType)
                {
                    SendBufferSize = MaxBufferSize,
                    SendTimeout    = timeoutInMilliseconds,
                    ReceiveTimeout = timeoutInMilliseconds
                                     //ReceiveBufferSize = MaxBufferSize
                };

                var res     = socket.BeginConnect(host, listenerPortOfServer, null, null);
                var success = res.AsyncWaitHandle.WaitOne(timeoutInMilliseconds, true);

                if (success)
                {
                    for (var i = 0; i < commands.Length; i++)
                    {
                        if (commands[i] == "PORT ")
                        {
                            var ipStr = ipUtils.A1A2A3A4P1P2(ip.ToString(), ((IPEndPoint)dataSocket.LocalEndpoint).Port);
                            commands[i] += String.Concat(ipStr, "\r\n");
                        }

                        var sendBuffer = Encoding.ASCII.GetBytes(commands[i]);
                        socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);

                        if (commands[i].IndexOf("RETR ", StringComparison.Ordinal) != 0)
                        {
                            WaitForSocketData(socket, 1000);
                        }

                        do
                        {
                            var receiveBuffer = new byte[socket.Available];
                            var readedBytes   = socket.Receive(receiveBuffer, receiveBuffer.Length, SocketFlags.None);
                            if (readedBytes > 0)
                            {
                                var s = new String(Encoding.ASCII.GetChars(receiveBuffer, 0, readedBytes));
                                if (s.IndexOf("150 ") == 0 || s.IndexOf("125 ") == 0)
                                {
                                    while (!dataSocket.Pending())
                                    {
                                        Thread.Sleep(1);
                                    }

                                    var ds = dataSocket.AcceptSocket();
                                    WaitForSocketData(ds, Int32.MaxValue);

                                    while (true)
                                    {
                                        if (!WaitForSocketData(ds, 10000))
                                        {
                                            break;
                                        }

                                        var dataReceiveBuffer = new byte[ds.Available];
                                        /*var readBytes =*/ ds.Receive(dataReceiveBuffer, dataReceiveBuffer.Length, SocketFlags.None);

                                        if (commands[i].IndexOf("RETR ") == 0)
                                        {
                                            var fullPath = path + commands[i].Replace("RETR ", String.Empty).Replace("\r\n", String.Empty);
                                            using (var fileStream = File.Open(fullPath, FileMode.Append))
                                            {
                                                using (var binaryWriter = new BinaryWriter(fileStream))
                                                {
                                                    binaryWriter.Write(dataReceiveBuffer);
                                                    binaryWriter.Flush();
                                                    binaryWriter.Close();
                                                }
                                                fileStream.Close();
                                            }
                                        }
                                    }
                                }
                            }

                            WaitForSocketData(socket, 100);
                        }while (socket.Available > 0);
                        result = true;
                    }
                }
            }
            catch { }
            finally
            {
                try
                {
                    dataSocket?.Stop();
                    if (socket != null)
                    {
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();
                    }
                }
                catch { }
            }

            return(result);
        }