Exemple #1
0
        static void Main(string[] args)
        {
            SslSetup.InstallCertificate();
            FormClientSimulator form = new FormClientSimulator();

            form.ShowDialog();
        }
Exemple #2
0
        public int Connect()
        {
            int inRet = 0;

            try
            {
                _client                = new TcpClient(_hostIP, _port);
                _client.SendTimeout    = 30 * 1000; // _inSendTimeout * 1000;//30000;//90000;
                _client.ReceiveTimeout = 30 * 1000; // _inReceiveTimeout * 1000;// 30000;// 420000;
            }
            catch
            {
                return(-1);
            }

            bool             leaveInnerStreamOpen = false;
            EncryptionPolicy encryptionPolicy     = EncryptionPolicy.RequireEncryption;

            //create the SSL stream starting from the NetworkStream associated
            //with the TcpClient instance
            _sslStream = new SslStream(_client.GetStream(),
                                       leaveInnerStreamOpen, new RemoteCertificateValidationCallback(ServerValidationCallback)
                                       , new LocalCertificateSelectionCallback(ClientCertificateSelectionCallback), encryptionPolicy);

            X509CertificateCollection clientCertificates = SslSetup.GetAllCertificates();
            bool checkCertificateRevocation = true;

            try
            {
                //Start the handshake
                _sslStream.AuthenticateAsClient(_hostIP, clientCertificates, _protocol, checkCertificateRevocation);
                _sslStream.ReadTimeout  = 5 * 1000;
                _sslStream.WriteTimeout = 5 * 1000;
            }
            catch
            {
                _client.Close();
                inRet = -2;
            }

            return(inRet);
        }
Exemple #3
0
        public void DoSendTCPIP()
        {
            Iso8583Data iso8583Data = (Iso8583Data)null;

            this.ParseData();
            TransmittedData transmittedData = new TransmittedData((EMessageLengthType)this.cboLengthType.SelectedIndex);

            this.btnSendTCPIP.Enabled = false;
            try
            {
                iso8583Data = new Iso8583Data();
                if (this.ckbSslEnable.Checked == true)
                {
                    for (int index = 0; (Decimal)index < this.numSendTimes.Value; ++index)
                    {
                        try
                        {
                            this.txtRawResponse.Text = "";

                            int hostBytesRead = 0;
                            int timeout       = 30;

                            string _message    = txtRawMessage.Text;
                            byte[] hostMessage = new byte[4096];
                            byte[] message     = new byte[4096];
                            hostBytesRead = 0;

                            int inLengthFormat = 0;
                            switch (this.cboLengthType.SelectedIndex)
                            {
                            case 0:
                                inLengthFormat = 2;
                                break;

                            case 1:
                                inLengthFormat = 1;
                                break;

                            default:
                                inLengthFormat = 0;
                                break;
                            }
                            string szLogMsg = "";
                            hostMessage = SslSetup.HexString2Bytes(_message, inLengthFormat, out szLogMsg);
                            SslClient sslHostClient = new SslClient(txtServer.Text.Trim(), int.Parse(txtPort.Text.Trim())
                                                                    , SslProtocols.Tls, timeout);
                            int inConnect = sslHostClient.Connect();
                            sslHostClient.Send(hostMessage);

                            hostBytesRead = sslHostClient.Receive(ref message);
                            if (hostBytesRead > 0)
                            {
                                String content = BitConverter.ToString(message, 0, hostBytesRead).Replace("-", "");

                                if (content.Length > 4)
                                {
                                    txtRawResponse.Text = content.Substring(2, content.Length - 2);
                                    iso8583Data.Unpack(message, 2, message.Length - 2);
                                    this.txtResponse.Text = iso8583Data.LogFormat();
                                }
                            }
                        }
                        catch { }
                    }
                }
                else
                {
                    TcpClient client = (TcpClient)null;

                    if (this.ckbOneConnection.Checked)
                    {
                        client = new TcpClient(this.txtServer.Text, int.Parse(this.txtPort.Text));
                    }
                    for (int index = 0; (Decimal)index < this.numSendTimes.Value; ++index)
                    {
                        try
                        {
                            if (!this.ckbOneConnection.Checked)
                            {
                                client = new TcpClient(this.txtServer.Text, int.Parse(this.txtPort.Text));
                            }
                            this.txtRawResponse.Text = "";
                            client.GetStream().ReadTimeout = 100000;
                            transmittedData.Write(client.GetStream(), this.DataConverted(this.txtRawMessage, this.cbkANSI.Checked));
                            client.GetStream().Flush();
                            if (this.cbkANSI.Checked)
                            {
                                this.txtRawResponse.Text = Encoding.Default.GetString(this.buffer, 0, client.Client.Receive(this.buffer));
                            }
                            else
                            {
                                transmittedData.Read(client, (int)this.numericUpDown1.Value, false);
                                this.txtRawResponse.Text = IsoUltil.BytesToHexString(transmittedData.ReadMessage, 20, false);
                                iso8583Data.Unpack(transmittedData.ReadMessage);
                                this.txtResponse.Text = iso8583Data.LogFormat();
                            }
                            if (!this.ckbOneConnection.Checked)
                            {
                                client.Client.Shutdown(SocketShutdown.Both);
                                client.Client.Disconnect(false);
                                client.Client.Close();
                            }
                            if (this.numInterval.Value > new Decimal(0))
                            {
                                Thread.Sleep((int)this.numInterval.Value);
                            }
                        }
                        catch (Exception ex)
                        {
                            this.txtResponse.Text = ex.ToString() + "\r\n";
                            if (iso8583Data != null)
                            {
                                this.txtResponse.Text += iso8583Data.LogFormat(iso8583Data.LastBitError);
                            }
                            if (this.cbkStopWhenError.Checked)
                            {
                                throw ex;
                            }
                        }
                    }
                    if (this.ckbOneConnection.Checked)
                    {
                        client.Client.Shutdown(SocketShutdown.Both);
                        client.Client.Disconnect(false);
                        client.Client.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                this.txtResponse.Text = ex.ToString() + "\r\n";
                if (iso8583Data != null)
                {
                    this.txtResponse.Text += iso8583Data.LogFormat(iso8583Data.LastBitError);
                }
            }
            this.btnSendTCPIP.Enabled = true;
            this.thrSending           = (Thread)null;
        }
Exemple #4
0
        public byte[] readByteMessage()
        {
            try
            {
                start = DateTime.Now.Ticks / 10000000;
                byte[] header = new byte[2];
                byte[] data   = null;
                int    i      = 0;
                int    len    = 0;
                string szLen  = "";
                while (true)
                {
                    int inData = _sslStream.ReadByte();
                    if (inData < 0)
                    {
                        end = DateTime.Now.Ticks / 10000000;
                        if (end - start >= _inTimeout)
                        {
                            return(null);;
                        }
                        else
                        {
                            continue;
                        }
                    }


                    if (inData >= 0 && i < 2)
                    {
                        header[i] = (byte)inData;
                        if (i == 1)
                        {
                            szLen = SslSetup.ByteArrayToString(header);
                            if (szLen.Trim() == "")
                            {
                                return(null);
                            }
                            else
                            {
                                len = int.Parse(szLen);
                                if (len == 0)
                                {
                                    return(null);
                                }
                                else
                                {
                                    data    = new byte[len + 2];
                                    data[0] = header[0];
                                    data[1] = header[1];
                                }
                            }
                        }

                        i++;
                        continue;
                    }
                    else
                    {
                        data[i] = (byte)inData;
                        if (i == len)
                        {
                            break;
                        }
                        else
                        {
                            i++;
                            continue;
                        }
                    }
                }
                return(data);
            }
            catch
            {
                return(null);
            }
        }