The Geeky Way//thegeekyway.com/2022-12-11T23:11:00+05:30Infrastructure as Code with Terraform2022-12-11T23:11:00+05:302022-12-11T23:11:00+05:30sanyam_khuranatag:thegeekyway.com,2022-12-11:/infrastructure-as-code-with-terraform/<p>Infrastructure as Code (IaC) is an approach to managing IT infrastructure in which infrastructure resources are managed as code rather than through manual processes. Terraform is a popular open-source tool for implementing IaC. In this article, we'll explore how to use Terraform to manage infrastructure as code and how it …</p><p>Infrastructure as Code (IaC) is an approach to managing IT infrastructure in which infrastructure resources are managed as code rather than through manual processes. Terraform is a popular open-source tool for implementing IaC. In this article, we'll explore how to use Terraform to manage infrastructure as code and how it can help you maintain consistency and manage changes in your infrastructure.</p>
<h2>Benefits of Infrastructure as Code</h2>
<p>There are several benefits of implementing IaC using Terraform, including:</p>
<ul>
<li>
<p>Consistency: When you manage your infrastructure as code, you can ensure that all of your resources are created and configured in the same way every time. This reduces the risk of errors and inconsistencies that can arise when you manage infrastructure manually.</p>
</li>
<li>
<p>Automation: IaC with Terraform allows you to automate the provisioning and configuration of your infrastructure. This can save time and reduce the likelihood of human error.</p>
</li>
<li>
<p>Version control: By managing your infrastructure as code, you can use version control tools to track changes and roll back changes if necessary.</p>
</li>
</ul>
<h2>Getting Started with Terraform</h2>
<p>To get started with Terraform, you'll need to follow a few key steps:</p>
<h3>1. Install Terraform</h3>
<p>The first step is to install Terraform on your local machine. You can download the latest version of Terraform from the official website.</p>
<h3>2. Create a Terraform Configuration</h3>
<p>Once you've installed Terraform, you can start creating your infrastructure configuration. The Terraform configuration is written in a language called HashiCorp Configuration Language (HCL). Here's an example configuration that creates an AWS EC2 instance:</p>
<div class="highlight"><pre><span></span><code><span class="n">provider</span><span class="w"> </span><span class="s">"aws"</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">access_key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"ACCESS_KEY"</span>
<span class="w"> </span><span class="n">secret_key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"SECRET_KEY"</span>
<span class="w"> </span><span class="n">region</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"us-west-2"</span>
<span class="p">}</span>
<span class="n">resource</span><span class="w"> </span><span class="s">"aws_instance"</span><span class="w"> </span><span class="s">"example"</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">ami</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"ami-0c55b159cbfafe1f0"</span>
<span class="w"> </span><span class="n">instance_type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"t2.micro"</span>
<span class="p">}</span>
</code></pre></div>
<p>In this example, we're using the aws_instance resource to create an EC2 instance. We're also specifying the AWS region and our access and secret keys in the provider block.</p>
<h3>3. Initialize and Apply the Configuration</h3>
<p>Once you've created your Terraform configuration, you can initialize it by running the following command in your terminal:</p>
<div class="highlight"><pre><span></span><code><span class="n">terraform</span><span class="w"> </span><span class="n">init</span>
</code></pre></div>
<p>This will download the necessary providers and modules that are required for your configuration. Once initialization is complete, you can apply the configuration by running the following command:</p>
<div class="highlight"><pre><span></span><code>terraform apply
</code></pre></div>
<p>This will create the resources specified in your Terraform configuration.</p>
<h3>4. Updating and Destroying Resources</h3>
<p>If you need to make changes to your infrastructure, you can simply update your Terraform configuration and apply the changes using the <code>terraform apply</code> command. If you need to destroy your resources, you can use the <code>terraform destroy</code> command.</p>
<h3>Conclusion</h3>
<p>Infrastructure as Code with Terraform is a powerful approach to managing your infrastructure. By managing your resources as code, you can ensure consistency, automate infrastructure provisioning and configuration, and use version control tools to track changes. Getting started with Terraform is easy, and with a little practice, you can use it to manage even the most complex infrastructure.</p>
<p>In summary, Terraform provides a powerful and flexible way to manage infrastructure as code. It offers many benefits over manual infrastructure management, including consistency, automation, and version control. By following the steps outlined in this article, you can get started with Terraform and start reaping the benefits of Infrastructure as Code.</p>Terraform and AWS Lambda2022-10-25T11:53:00+05:302022-10-25T11:53:00+05:30sanyam_khuranatag:thegeekyway.com,2022-10-25:/terraform-and-aws-lambda/<p>AWS Lambda is a serverless computing service that allows you to run your code without provisioning or managing servers. Terraform is a popular tool for infrastructure as code, which allows you to manage your infrastructure as code rather than using manual processes. In this article, we'll explore how to use …</p><p>AWS Lambda is a serverless computing service that allows you to run your code without provisioning or managing servers. Terraform is a popular tool for infrastructure as code, which allows you to manage your infrastructure as code rather than using manual processes. In this article, we'll explore how to use Terraform to deploy and manage AWS Lambda functions.</p>
<h2>Setting up AWS Credentials</h2>
<p>Before you can start using Terraform to deploy Lambda functions, you'll need to set up your AWS credentials. You can do this by creating an access key and secret access key in the AWS Management Console. Once you have your access key and secret access key, you can set them up on your local machine by running the following command:</p>
<div class="highlight"><pre><span></span><code>aws configure
</code></pre></div>
<h2>Creating a Lambda Function with Terraform</h2>
<p>To create a Lambda function with Terraform, you'll need to follow a few key steps:</p>
<h3>1. Initialize a New Terraform Project</h3>
<p>The first step is to initialize a new Terraform project. You can do this by creating a new directory for your project and running the following command:</p>
<div class="highlight"><pre><span></span><code><span class="n">terraform</span><span class="w"> </span><span class="n">init</span>
</code></pre></div>
<p>This will initialize your Terraform project and download any necessary providers.</p>
<h3>2. Define Your Lambda Function Configuration</h3>
<p>Next, you'll need to define the configuration for your Lambda function. Here's an example configuration that creates a Lambda function that logs a message to CloudWatch:</p>
<div class="highlight"><pre><span></span><code><span class="n">provider</span><span class="w"> </span><span class="s">"aws"</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">region</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"us-east-1"</span>
<span class="p">}</span>
<span class="n">resource</span><span class="w"> </span><span class="s">"aws_lambda_function"</span><span class="w"> </span><span class="s">"example_lambda"</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">function_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"example_lambda"</span>
<span class="w"> </span><span class="n">role</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">aws_iam_role</span><span class="p">.</span><span class="n">example_lambda</span><span class="p">.</span><span class="n">arn</span>
<span class="w"> </span><span class="n">handler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"example_lambda.handler"</span>
<span class="w"> </span><span class="n">runtime</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"nodejs12.x"</span>
<span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"example_lambda.zip"</span>
<span class="w"> </span><span class="n">source_code_hash</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">filebase64sha256</span><span class="p">(</span><span class="s">"example_lambda.zip"</span><span class="p">)</span>
<span class="w"> </span><span class="n">timeout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">10</span>
<span class="w"> </span><span class="n">memory_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">128</span>
<span class="p">}</span>
<span class="n">resource</span><span class="w"> </span><span class="s">"aws_iam_role"</span><span class="w"> </span><span class="s">"example_lambda"</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"example_lambda_role"</span>
<span class="w"> </span><span class="n">assume_role_policy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">jsonencode</span><span class="p">({</span>
<span class="w"> </span><span class="n">Version</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"2012-10-17"</span>
<span class="w"> </span><span class="n">Statement</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Effect</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Allow"</span>
<span class="w"> </span><span class="n">Principal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Service</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"lambda.amazonaws.com"</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">Action</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"sts:AssumeRole"</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">]</span>
<span class="w"> </span><span class="p">})</span>
<span class="p">}</span>
</code></pre></div>
<p>In this example, we're using the aws_lambda_function resource to create a Lambda function. We're also specifying the AWS region, function name, IAM role, runtime, filename, timeout, and memory size in the resource block.</p>
<h3>3. Deploy Your Lambda Function</h3>
<p>Once you've defined your Lambda function configuration, you can deploy your function by running the following commands:</p>
<div class="highlight"><pre><span></span><code>terraform apply
</code></pre></div>
<p>This will create your Lambda function and any necessary resources.</p>
<h3>4. Updating and Destroying Your Lambda Function</h3>
<p>If you need to update your Lambda function, you can simply update your Terraform configuration and apply the changes using the <code>terraform apply</code> command. If you need to destroy your Lambda function, you can use the <code>terraform destroy</code> command.</p>
<h2>Conclusion</h2>
<p>In conclusion, using Terraform to deploy and manage AWS Lambda functions can be a powerful tool in your infrastructure management arsenal. By defining your Lambda functions as code and leveraging Terraform's infrastructure-as-code paradigm, you can automate the deployment and management of your Lambda functions and maintain consistency across your infrastructure.</p>
<p>Terraform simplifies the process of deploying and managing AWS Lambda functions by allowing you to define your infrastructure in code, version control it, and automate its deployment. You can easily manage and scale your Lambda functions with Terraform, and roll out updates in a controlled, automated way. Additionally, the use of Terraform allows for collaboration across teams, as the code is easily shared and version controlled.</p>
<p>Overall, Terraform is an excellent tool for managing and deploying AWS Lambda functions, and with its robust set of features, it can help you streamline your infrastructure management process and bring greater efficiency and consistency to your deployment pipeline.</p>Setting up CI/CD Pipelines with GitHub Actions2022-08-15T17:29:00+05:302022-08-15T17:29:00+05:30sanyam_khuranatag:thegeekyway.com,2022-08-15:/setting-up-ci-cd-pipelines-with-github-actions/<p>Continuous integration (CI) and continuous delivery (CD) are essential practices in modern software development. These practices allow teams to deliver software faster, with fewer bugs, and with more confidence. GitHub Actions is a powerful tool that can help you automate your CI/CD pipelines and integrate them seamlessly into your …</p><p>Continuous integration (CI) and continuous delivery (CD) are essential practices in modern software development. These practices allow teams to deliver software faster, with fewer bugs, and with more confidence. GitHub Actions is a powerful tool that can help you automate your CI/CD pipelines and integrate them seamlessly into your GitHub workflow.</p>
<p>In this article, we will explore how to set up CI/CD pipelines with GitHub Actions. We will cover the basics of how GitHub Actions works, how to define workflows, and how to use Actions to automate tasks like building, testing, and deploying your code.</p>
<h2>What is GitHub Actions?</h2>
<p>GitHub Actions is a feature of GitHub that allows you to automate tasks in your software development workflow. With Actions, you can define custom workflows that run when specific events occur, like a code push or a pull request. Workflows are defined using YAML files, which makes them easy to read and maintain.</p>
<p>GitHub Actions provides a wide range of pre-built Actions that you can use to automate common tasks, like building and testing your code. You can also create your own custom Actions and share them with the GitHub community.</p>
<h2>Defining Workflows</h2>
<p>To use GitHub Actions, you need to define one or more workflows. A workflow is a set of jobs that are run when a specific event occurs. Workflows are defined using YAML files that are stored in your repository under the <code>.github/workflows</code> directory.</p>
<p>Here is an example workflow file that defines a simple CI pipeline:</p>
<div class="highlight"><pre><span></span><code><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">CI</span>
<span class="nt">on</span><span class="p">:</span>
<span class="w"> </span><span class="nt">push</span><span class="p">:</span>
<span class="w"> </span><span class="nt">branches</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="w"> </span><span class="nv">main</span><span class="w"> </span><span class="p p-Indicator">]</span>
<span class="w"> </span><span class="nt">pull_request</span><span class="p">:</span>
<span class="w"> </span><span class="nt">branches</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="w"> </span><span class="nv">main</span><span class="w"> </span><span class="p p-Indicator">]</span>
<span class="nt">jobs</span><span class="p">:</span>
<span class="w"> </span><span class="nt">build</span><span class="p">:</span>
<span class="w"> </span><span class="nt">runs-on</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">ubuntu-latest</span>
<span class="w"> </span><span class="nt">steps</span><span class="p">:</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Checkout code</span>
<span class="w"> </span><span class="nt">uses</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">actions/checkout@v2</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Build code</span>
<span class="w"> </span><span class="nt">run</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">|</span>
<span class="w"> </span><span class="no">npm install</span>
<span class="w"> </span><span class="no">npm run build</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Run tests</span>
<span class="w"> </span><span class="nt">run</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">npm test</span>
</code></pre></div>
<p>This workflow is triggered when code is pushed or a pull request is opened against the <code>main</code> branch. It defines a single job called <code>build</code> that runs on an Ubuntu operating system. The job contains three steps:</p>
<div class="highlight"><pre><span></span><code><span class="n">Checking</span><span class="w"> </span><span class="k">out</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">code</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">GitHub</span><span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n n-Quoted">`actions/checkout`</span><span class="w"> </span><span class="k">Action</span><span class="p">.</span>
<span class="n">Building</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">code</span><span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="n n-Quoted">`npm`</span><span class="p">.</span>
<span class="n">Running</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">tests</span><span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="n n-Quoted">`npm`</span><span class="p">.</span>
</code></pre></div>
<h2>Using Actions</h2>
<p>GitHub Actions provides a wide range of pre-built Actions that you can use to automate common tasks. For example, you can use the <code>actions/setup-node</code> Action to set up Node.js on your system, or the <code>actions/upload-artifact</code> Action to upload build artifacts to GitHub.</p>
<p>You can also create your own custom Actions using Docker containers. Here is an example of a custom Action that uses a Docker container to deploy a Node.js application:</p>
<div class="highlight"><pre><span></span><code><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Deploy</span>
<span class="nt">on</span><span class="p">:</span>
<span class="w"> </span><span class="nt">push</span><span class="p">:</span>
<span class="w"> </span><span class="nt">branches</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="w"> </span><span class="nv">main</span><span class="w"> </span><span class="p p-Indicator">]</span>
<span class="nt">jobs</span><span class="p">:</span>
<span class="w"> </span><span class="nt">deploy</span><span class="p">:</span>
<span class="w"> </span><span class="nt">runs-on</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">ubuntu-latest</span>
<span class="w"> </span><span class="nt">steps</span><span class="p">:</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Checkout code</span>
<span class="w"> </span><span class="nt">uses</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">actions/checkout@v2</span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Deploy application</span>
<span class="w"> </span><span class="nt">uses</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">docker://node:12</span>
<span class="w"> </span><span class="nt">with</span><span class="p">:</span>
<span class="w"> </span><span class="nt">args</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">|</span>
<span class="w"> </span><span class="no">npm install</span>
<span class="w"> </span><span class="no">npm run build</span>
<span class="w"> </span><span class="no">npm start</span>
</code></pre></div>
<p>This workflow is triggered when code is pushed to the <code>main</code> branch. It defines a single job called <code>deploy</code> that runs on an Ubuntu operating system. The job contains two steps:</p>
<div class="highlight"><pre><span></span><code>Checking out the code from GitHub using the `actions/checkout` Action.
Running a Docker container based on the `node:12` image. The container runs three commands: `npm install`, `npm run build`, and `npm start`.
</code></pre></div>
<h2>Conclusion</h2>
<p>In conclusion, setting up CI/CD pipelines with GitHub Actions can significantly streamline your software development process. With the ability to automate your tests, builds, and deployments, you can ensure that your code is always reliable and up-to-date. By leveraging the power of GitHub Actions, you can save time and resources, as well as reduce the likelihood of human error.</p>
<p>In this article, we walked through the process of setting up a basic CI/CD pipeline using GitHub Actions. We covered how to define jobs, run tests, build Docker images, and push them to Docker Hub. While this example is relatively simple, it can serve as a starting point for more complex CI/CD pipelines.</p>
<p>By incorporating additional tools and services such as Kubernetes or Terraform, you can extend your CI/CD pipeline to include deploying your application to a production environment. With continuous integration and continuous deployment, you can ensure that your application is always running smoothly and your customers are receiving the latest features and fixes.</p>
<p>We hope this article has been useful in helping you get started with setting up CI/CD pipelines with GitHub Actions. Remember to always test your code thoroughly and follow best practices to ensure the reliability and security of your applications.</p>Pros And Cons Of Using JWT (JSON Web Tokens)2022-07-24T05:36:00+05:302022-07-24T05:36:00+05:30Sanyam_Khuranatag:thegeekyway.com,2022-07-24:/pros-and-cons-of-jwt/<p>JWT (JSON Web tokens) is a great way to implement authentication in your applications, though often it is being treated as a silver bullet for almost every backend project. While JWT provides a lot of benefits while implementing auth, it does come with a price. Anyone who is thinking to …</p><p>JWT (JSON Web tokens) is a great way to implement authentication in your applications, though often it is being treated as a silver bullet for almost every backend project. While JWT provides a lot of benefits while implementing auth, it does come with a price. Anyone who is thinking to integrate JWT, needs to understand both the pros and cons of using JWT & carefully weigh them, before using them as their auth layer.</p>
<div style="text-align:center">
<p><img alt="Pros And Cons of JWT" src="//thegeekyway.com/uploads/images/2022/07/jwt-pros-cons.png"></p>
</div>
<h3>Pros of loving JWT:</h3>
<ul>
<li>You can create and verify the tokens on the fly without a need to store them ever in the database.</li>
<li>Simple implementation and faster development time.</li>
<li>Can be used across services and specifically be isolated in an authorization micro-service that can create these tokens. The rest of your microservices can just have the public key to verify the signature of the tokens.</li>
<li>The tokens are based on JSON, which is defacto standard now-a-days for inter-message communication among web application & services.</li>
</ul>
<h3>Taking a step back to understand the cons:</h3>
<ul>
<li>Steep learning curve, if you need to understand them correctly avoiding the pitfalls.</li>
<li>No way to log out or invalidate sessions for users. Moreover, there is no way for a user to disable their sessions across multiple devices.</li>
<li>Since the tokens are generated and verified on the fly, we can't have access to the different logged-in clients which can pose problems when you need to identify the devices.</li>
<li>Data leakage is plausible through a single secret key used for signing the tokens. It is much harder to lose the entire database, for example, in cases of session management when compared to JWTs. Since JWTs are created on the fly, the only thing that is protecting the creation of forged tokens is the secret key used to sign the tokens.</li>
<li>JWTs need care & understanding for implementation. I'll give you a basic example of a simple coding mistake which is a pitfall for every developer starting with JWTs. If you use the <code>none</code> algorithm, it uses no signing key, which means creating a forged token is child's play (I'll explain this in a subsequent post on this blog). Once you have the forged token and you're using ids for users that are easier to guess (think of integer ids), or you're using frameworks such as Strapi that has user id 1 as the administrator, with a forged token, your entire account will be taken over in a matter of seconds.</li>
<li>We should always take care of not blindly loading signing keys with the <code>kid</code> header in JWT. The key should always be first validated for correctness as compared with the algorithm specified in the header.</li>
</ul>
<h2>Conclusion:</h2>
<p>While JWT is a great technology and provides very fast development times, it is time that we understand that it's not a silver bullet for the Auth (authentication/authorization) requirements of your application. If you need to maintain sessions, please by all means use session management. Take your time to learn JWTs before implementing them. While they look a lot simpler, a simple mistake such as using the <code>none</code> algorithm can be disastrous causing account takeovers, data loss, and much more. Having said that, JWTs can be super-useful if implemented correctly and when there is no requirement for session management.</p>Django and Machine Learning: Building Intelligent Web Applications2021-09-19T16:21:00+05:302021-09-19T16:21:00+05:30sanyam_khuranatag:thegeekyway.com,2021-09-19:/django-and-machine-learning-building-intelligent-web-applications/<p>Machine learning and artificial intelligence (AI) are rapidly transforming the way we build and interact with web applications. By leveraging the power of machine learning algorithms, developers can build intelligent applications that can learn from user behavior, adapt to changing environments, and make data-driven decisions in real-time.</p>
<p>Django is a …</p><p>Machine learning and artificial intelligence (AI) are rapidly transforming the way we build and interact with web applications. By leveraging the power of machine learning algorithms, developers can build intelligent applications that can learn from user behavior, adapt to changing environments, and make data-driven decisions in real-time.</p>
<p>Django is a powerful Python web framework that provides a solid foundation for building intelligent web applications. In this article, we'll explore how to combine Django and machine learning to build intelligent web applications.</p>
<h2>Building Intelligent Applications with Django and Machine Learning</h2>
<p>To build intelligent web applications with Django and machine learning, you'll need to follow a few key steps:</p>
<h3>1. Data Collection and Preprocessing</h3>
<p>The first step in building an intelligent web application is to collect and preprocess data. This typically involves gathering data from a variety of sources, cleaning and normalizing the data, and selecting the most relevant features to use in your machine learning model.</p>
<p>With Django, you can use the Django ORM to interact with your application's database and extract the data you need. You can also use Django's form validation and cleaning features to preprocess user input data.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">MyModel</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">'active'</span><span class="p">)</span>
<span class="c1"># preprocess data</span>
<span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">'my_template.html'</span><span class="p">,</span> <span class="p">{</span><span class="s1">'data'</span><span class="p">:</span> <span class="n">data</span><span class="p">})</span>
</code></pre></div>
<h3>2. Model Training and Evaluation</h3>
<p>The next step is to train a machine learning model using the preprocessed data. This involves selecting an appropriate machine learning algorithm, defining a set of features and labels, and using a training set to train the model.</p>
<p>Once the model is trained, you'll need to evaluate its performance using a validation set. This will help you determine whether the model is accurate enough to be used in your web application.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LogisticRegression</span>
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">test_size</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>
<span class="n">clf</span> <span class="o">=</span> <span class="n">LogisticRegression</span><span class="p">()</span>
<span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
<span class="n">accuracy</span> <span class="o">=</span> <span class="n">clf</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">)</span>
</code></pre></div>
<h3>3. Integration with Django</h3>
<p>The final step is to integrate the trained machine learning model with your Django web application. This typically involves creating a view or API endpoint that takes user input data, applies the machine learning model, and returns the predicted output.</p>
<p>You can use Django's built-in views and serializers to create RESTful API endpoints that can be used to serve predictions to other applications. You can also use Django's templating system to render predictions directly within your web application.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">JsonResponse</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.csrf</span> <span class="kn">import</span> <span class="n">csrf_exempt</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="nd">@csrf_exempt</span>
<span class="k">def</span> <span class="nf">predict_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">'POST'</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">body</span><span class="p">)</span>
<span class="n">prediction</span> <span class="o">=</span> <span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="s1">'feature_1'</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="s1">'feature_2'</span><span class="p">]])</span>
<span class="k">return</span> <span class="n">JsonResponse</span><span class="p">({</span><span class="s1">'prediction'</span><span class="p">:</span> <span class="n">prediction</span><span class="p">})</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">JsonResponse</span><span class="p">({</span><span class="s1">'error'</span><span class="p">:</span> <span class="s1">'POST request required'</span><span class="p">})</span>
</code></pre></div>
<h2>Use Cases for Django and Machine Learning</h2>
<p>There are a wide variety of use cases for building intelligent web applications with Django and machine learning. Here are just a few examples:</p>
<h3>E-Commerce Recommendation Engines</h3>
<p>E-commerce websites can use machine learning to make personalized product recommendations to users. By analyzing user behavior and purchase history, a machine learning model can predict which products a user is most likely to be interested in.</p>
<p>With Django, you can build an e-commerce recommendation engine that integrates with your application's product database and user authentication system.</p>
<h3>Sentiment Analysis for Social Media</h3>
<p>Social media platforms can use machine learning to analyze user posts and determine the sentiment of each post. This can be used to identify and flag potentially harmful or offensive content.</p>
<p>With Django, you can build a sentiment analysis application that integrates with popular social media platforms like Twitter and Facebook. Users can input a post or comment, and the machine learning model can predict whether the sentiment is positive, negative, or neutral.</p>
<h3>Fraud Detection for Financial Applications</h3>
<p>Financial applications can use machine learning to detect and prevent fraudulent activity. By analyzing transaction data and user behavior, a machine learning model can identify patterns and anomalies that may indicate fraudulent activity.</p>
<p>With Django, you can build a fraud detection application that integrates with your application's transaction database and user authentication system. Users can report suspicious activity, and the machine learning model can flag potential fraudsters in real-time.
post. This can be used to identify and flag potentially harmful or offensive content.</p>
<h2>Conclusion</h2>
<p>In this article, we explored how to combine Django and machine learning to build intelligent web applications. By following the key steps of data collection and preprocessing, model training and evaluation, and integration with Django, you can build a wide variety of intelligent applications that can learn from user behavior, adapt to changing environments, and make data-driven decisions in real-time.</p>
<p>The use cases for Django and machine learning are endless, from e-commerce recommendation engines to sentiment analysis for social media to fraud detection for financial applications. By leveraging the power of machine learning algorithms, you can take your Django web application to the next level and provide your users with a more personalized and intelligent experience.</p>List of few tags that are supported in HTML52019-08-24T01:12:53+05:302019-08-24T01:12:53+05:30Deep_Singhtag:thegeekyway.com,2019-08-24:/few-tags-now-supported-html5/<p>Hi everyone, some of you might be new to web development, so you must have thought that what is HTML5, how is it different from previous versions, what is new in HTML5.<br>
In this article we will be discussing about the new tags which are now there in HTML5 but …</p><p>Hi everyone, some of you might be new to web development, so you must have thought that what is HTML5, how is it different from previous versions, what is new in HTML5.<br>
In this article we will be discussing about the new tags which are now there in HTML5 but were not there in previous versions of HTML.</p>
<ol>
<li>article tag</li>
</ol>
<p>This tag specifies an article which contains independent content. It doesn't co-relate to any other paragraph.<br>
This tag is supported IE 9+, Firefox, Opera, Chrome, and Safari. </p>
<iframe height="265" style="width: 100%;" scrolling="no" title="iyqxF" src="//codepen.io/Deep_Singh/embed/iyqxF/?height=265&theme-id=0&default-tab=html,result" frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/Deep_Singh/pen/iyqxF/'>iyqxF</a> by Deep Singh
(<a href='https://codepen.io/Deep_Singh'>@Deep_Singh</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
<ol>
<li>aside tag</li>
</ol>
<p>This tag is supported IE 9+, Firefox, Opera, Chrome, and Safari.</p>
<iframe height="265" style="width: 100%;" scrolling="no" title="kCsJq" src="//codepen.io/Deep_Singh/embed/kCsJq/?height=265&theme-id=0&default-tab=html,result" frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/Deep_Singh/pen/kCsJq/'>kCsJq</a> by Deep Singh
(<a href='https://codepen.io/Deep_Singh'>@Deep_Singh</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
<ol>
<li>audio tag</li>
</ol>
<p>It defines music, audio and other strams.</p>
<p>This tag is supported IE 9+, Firefox, Opera, Chrome, and Safari.
Firefox supports mp3</p>
<iframe height="265" style="width: 100%;" scrolling="no" title="Sample audio tag" src="//codepen.io/jfreyre/embed/bIdvi/?height=265&theme-id=0&default-tab=html,result" frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/jfreyre/pen/bIdvi/'>Sample audio tag</a> by Freyre
(<a href='https://codepen.io/jfreyre'>@jfreyre</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>Understanding Event Dispatching Thread in Java2019-08-24T01:11:30+05:302019-08-24T01:11:30+05:30gagan93tag:thegeekyway.com,2019-08-24:/edt_java/<p>Swing provides a rich toolkit of GUI components which can be used to create powerful User Interface. Sometimes, we don’t get expected results when we are working on complex GUI & try to update GUI of that running application. From <strong>running application</strong> we mean that we have a Swing application …</p><p>Swing provides a rich toolkit of GUI components which can be used to create powerful User Interface. Sometimes, we don’t get expected results when we are working on complex GUI & try to update GUI of that running application. From <strong>running application</strong> we mean that we have a Swing application running and we update something in the app while it is running. There are some rules (as per API documentation), which if followed correctly, can lead to expected results in a GUI application.</p>
<p>A normal swing application is started within the event dispatching thread in this way :</p>
<div class="highlight"><pre><span></span><code>public static void main(String[]args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ConstructorOfClass();
}
});
}
</code></pre></div>
<p>According to Oracle Documentation, when a Swing Application starts, two threads start running : <strong>Main Thread</strong> and <strong>Event Dispatching Thread (EDT) .</strong> The ED thread consists of the code related to GUI of the application. The main thread just executes the logic of the application, but whenever GUI needs to be updated, it must be passed to the EDT. The usual way of calling code inside EDT is :</p>
<div class="highlight"><pre><span></span><code>SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
/* EDT code here like updating label, button
or some other work related to GUI updation
*/
}
});
</code></pre></div>
<p>So if we are calculating something and after that we need to update some component in GUI, we will write the GUI updation code inside the <strong>run</strong> method as shown above. SwingUtilities's invokeLater method takes Runnable objects and executes them one by one on a single thread (EDT) so that their is no ambiguity related to updation of GUI. In contrast to this approach, if we write the GUI updation code in some other thread (or even main thread), we may not get expected results.This is so because many Swing methods are not thread-safe.</p>
<p><strong>Thread Safety</strong> : Thread safe simply means that it may be used from multiple threads at the same time without causing problems. This can mean that access to any resources are synchronized.This is given in API documentation that <strong>all GUI updation should be strictly done in only one thread called the Event Dispatching Thread.<br>
</strong></p>Few terms in C++2019-08-24T01:10:55+05:302019-08-24T01:10:55+05:30sanyam_khuranatag:thegeekyway.com,2019-08-24:/data-file-handling-in-c/<p>Howdy Coders! </p>
<p>Well, today we will learn about a few terms related to File Handling in C++.</p>
<p><strong>Stream :-</strong> A sequence of bytes </p>
<p><strong>File:</strong> A collection of data or information stored on some media with some specific name is called file.</p>
<p><strong>Text File :-</strong> The file which stores data or information in …</p><p>Howdy Coders! </p>
<p>Well, today we will learn about a few terms related to File Handling in C++.</p>
<p><strong>Stream :-</strong> A sequence of bytes </p>
<p><strong>File:</strong> A collection of data or information stored on some media with some specific name is called file.</p>
<p><strong>Text File :-</strong> The file which stores data or information in ASCII characters. In text file each line is terminated with a special character “n” or endl (end of line) character also known as delimiter character.</p>
<p><strong>Binary File :-</strong> It is a file that contains information in the same format as it is stored in the memory. In binary file no delimiter character is used. The records are separated by record size.</p>Ansible?2019-08-24T00:58:20+05:302019-08-24T00:58:20+05:30storymode7tag:thegeekyway.com,2019-08-24:/ansible/<div style="text-align:center">
<p><img alt="Ansible Logo" src="//thegeekyway.com/uploads/images/2019/08/ansible_logo.png"></p>
</div>
<p>Hearing about Ansible lately? Everyone's using it! But what's the buzz about, what is ansible actually? And why should you use it? Lot's of questions and lot's of answers. Let's dive!</p>
<!--more-->
<h2>What is it?</h2>
<p>Well Ansible is a tool to automate the setup of an EC2 instance, a vagrant box …</p><div style="text-align:center">
<p><img alt="Ansible Logo" src="//thegeekyway.com/uploads/images/2019/08/ansible_logo.png"></p>
</div>
<p>Hearing about Ansible lately? Everyone's using it! But what's the buzz about, what is ansible actually? And why should you use it? Lot's of questions and lot's of answers. Let's dive!</p>
<!--more-->
<h2>What is it?</h2>
<p>Well Ansible is a tool to automate the setup of an EC2 instance, a vagrant box, and almost anything that you can ssh into.</p>
<h2>What's the usecase?</h2>
<p>Imagine you've to setup an instance and use it as a server using nginx. Simple, right? Just ssh into it, do the nginx conf and leave it running. But wait, you did the setup on say an Ubuntu instance, but for some reasons, you have to change your server OS, to something like a Fedora may be? Or an Amazon linux? Now, you need to repeat all the steps you just did for ubuntu setup to perform the exactly same task. Another usecase is to setup multiple instances automatically, all you need to define is the tasks, roles etc. for Ansible. We'll be getting to it shortly.</p>
<h2>Installing Ansible</h2>
<p><code>sudo apt-get install ansible</code> should do just fine. Or <code>dnf</code> in place of <code>apt-get</code> for RHEL. Interestingly, you don't need to install ansible on the target machine! You just install it on your local machine once. And just exploit it using the tasks you define. This is because Ansible basically just SSH into your remote machine, and executes task if required.</p>
<h2>Getting started</h2>
<p>We'll start with doing everything in a get it done way. And then move to the way of how it should be done. Firstly you define the target(s) to act upon. This is called the inventory. You can name this file anything, inventory, hosts, instances, etc. it's upto you. In this file, type in the IPs of the instances you wish to control in format:</p>
<div class="highlight"><pre><span></span><code><span class="k">[name]</span>
<span class="na">0.0.0.1 ansible_user</span><span class="o">=</span><span class="s">'ubuntu' ansible_ssh_private_key_file='path/to/privateKey/for/server'</span>
<span class="na">0.0.1.2 ansible_user</span><span class="o">=</span><span class="s">'userName_to_login_as' ansible_ssh_private_key_file='path/to/private/key'</span>
</code></pre></div>
<p>You can access these IPs using the *name* you provided. If you want to test this on vagrant, write an inventory file in the format:</p>
<div class="highlight"><pre><span></span><code><span class="k">[vag]</span>
<span class="na">127.0.0.1 ansible_port</span><span class="o">=</span><span class="s">2222 ansible_user='vagrant' ansible_ssh_private_key_file='/path/toVagrantfile/.vagrant/machines/default/virtualbox/private_key'</span>
</code></pre></div>
<p>If you wish, you can follow along by performing the commands on your machine instead of a server too.
Using localhost:</p>
<div class="highlight"><pre><span></span><code><span class="k">[me]</span>
<span class="na">localhost ansible_connection</span><span class="o">=</span><span class="s">local</span>
</code></pre></div>
<p>Now, if you wish to use localhost, just use <code>me</code> in place of <code>vag</code>/<code>groupName</code>.<br>
If the inventory is ready we can begin executing the commands!</p>
<p><strong>Note</strong>: Though most of the commands used in this blog are non destructive and would not change anything in your PC to cause problems. Caution should be excercised when hacking through the different commands on your own. Especially if the target machine hosts important data, or you are performing all the commands on your localhost</p>
<p><strong>Note</strong>: If you get an error/warning related to usage of python2 interpreter, you can create a file <code>ansible.cfg</code> and specify python3 interpreter by writing this in the file:</p>
<div class="highlight"><pre><span></span><code><span class="k">[defaults]</span>
<span class="na">interpreter_python</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">/usr/bin/python3</span>
</code></pre></div>
<h2>Executing some commands</h2>
<p>Ansible can work only when the target machine is ready to ssh, and the credentials provided in the inventory file are correct. Now that ansible knows how to authenticate itself to the machines, it'll have no problems executing any commands.</p>
<p>A little idea about Modules and arguments, and we are ready to go.</p>
<p>Modules are small pieces of python code, that ansible uses to handle resources on the target machine. And some modules require arguments to act upon. Let's see first an example that doesn't require an argument.</p>
<p>Ping module. Since we have already provided the inventory file, we don't need to provide ansible with the IPs of the instances to ping.</p>
<p>We can simply do:</p>
<div class="highlight"><pre><span></span><code>ansible -i inventory_file groupName -m ping
</code></pre></div>
<p>groupName, like we provided <code>vag</code> in the inventory file example above. It's just a collection of IPs or domains along with their options under one name.</p>
<p><code>-m</code>: states that we're going to use a module Whose name in this case is ping</p>
<p>If you've provided inventory file and group name correctly you should get an output like:<br>
<code>default | SUCCESS => { "changed": false, "ping": "pong" }</code><br>
Congratulations, you just executed your first command using ansible.</p>
<p>Now let's try one example which takes an argument. How about a Hello World sample? For this we're going to use <code>shell</code> module.</p>
<div class="highlight"><pre><span></span><code>ansible -i inventory groupName -m "shell" -a "echo Hello World!"
</code></pre></div>
<p>Output comes out to be:<br>
<code>default | SUCCESS | rc=0 >> Hello World!</code><br>
Let's try one last thing, installing a package. We'll be using apt module.</p>
<div class="highlight"><pre><span></span><code>ansible -i inventory groupName -m apt -a "name=nginx state=present update_cache=true"
</code></pre></div>
<p>The argument here is self explanatory. apt module, takes arguments name, state mainly. Name states which package to act upon, and state tells what action is needed to be performed if any. This makes sure idempotence is there. Means, the same action performed repeatedly should not alter the state of the resource. state can be absent, present, latest, etc.</p>
<p>Update_cache is suggested because most of the images used to setup an instance would require you to update repository cache before you install anything.</p>
<p>This command should give you an error<br>
<code>default | FAILED! => { "changed": false, "msg": "Failed to lock apt for exclusive operation" }</code><br>
And that is because the default user doesn't have enough privileges. For privilege escalation, we need to supply another flag. <code>-b</code> .</p>
<div class="highlight"><pre><span></span><code>ansible -i inventory groupName -b -m apt -a "name=nginx state=present update_cache=true"
</code></pre></div>
<p><code>-b</code> or <code>--become</code> tells ansible whether to activate or deactivate privilege escalation. Default for privilege escalation is root, and hence the command will be executed as root. But you can also specify another user, as whom you'd like to execute the command. This can be done using <code>--become-user userName</code> . Note that, if you the target system has requires a password, you need to pass <code>-K</code> flag to ask for become pass.</p>
<p>Now the terminal will take time depending on the internet connection available to the target machine and will generate output like: </p>
<div class="highlight"><pre><span></span><code>default | SUCCESS => {
"cache_update_time": 1526681063,
"cache_updated": true,
"changed": true,
"stderr": "",
"stderr_lines": [],
...
}
</code></pre></div>
<p>In every output the value for</p>
<div class="highlight"><pre><span></span><code>"changed"
</code></pre></div>
<p>variable is the gist of success or failure. This and the color of the output are enough to denote unchanged, changed, failure.</p>
<p>But ansible can do a lot more. This method is easy if you need to run one single module. But this is often not the case. You'd almost always require a collection of modules to setup a target machine. To see how many modules ansible provides head on to <a href="http://docs.ansible.com/ansible/latest/modules/modules_by_category.html">this</a> page. In that case giving arguments to ansible command wont be the best choice. Now enters ansible playbook.</p>
<h2>Ansible playbook</h2>
<p>Playbook is like a collection of ansible commands put together in a single YAML file. We performed 3 tasks all unrelated, but in case of a setup used to install and configure a single package inside the target machine, like setting up nginx, would require more tasks. And it'd be more logical to group all of them in a single playbook. Let's create a simple playbook, to say hello world, and then we'll move to installing nginx using playbook.</p>
<h3>A bit of YAML</h3>
<p>Before typing out your first YAML, let's just get a basic idea of it's syntax. These syntax basics have nothing specific with ansible. These basics will be same in every YAML file, so if you are already familiar with it's syntax, feel free to skip to the playbook part.</p>
<p>A YAML file can optionally begin with <code>---</code> (triple dash) and end with <code>...</code> (triple dot)<br>
<code>#</code> before any line comments it.<br>
A list is represented by lines starting with a <code>-</code> at same indentation level. For example:</p>
<div class="highlight"><pre><span></span><code>---
numbers:
- one
- two
- three
- four
...
</code></pre></div>
<p>And a dictionary is represented like:</p>
<div class="highlight"><pre><span></span><code>---
name_of_dictionary:
key1: value1
key2: value2
...
</code></pre></div>
<p>Now there can be combinations of keys, dictionaries. Let's do one example of list of dictionary that is used in playbooks.</p>
<div class="highlight"><pre><span></span><code>---
- dictionary1:
key1: value1
key2: value2
key3:
- one
- two
- dictionary2:
key4: value4
key5: value5
key6:
- foo
- bar
...
</code></pre></div>
<p>Here we have a list of dictionaries, dictionary1 and dictionary2. In these dictionaries, some of the values are themselves a list. Like key3 in dictionary1 and key6 in dictionary2.</p>
<p>If you're a pythonista, there's another format that'd be more familiar to you:</p>
<div class="highlight"><pre><span></span><code>---
dictionary1: {key1: value1, key2: value2}
list1: ['one', 'two', 'three']
...
</code></pre></div>
<p>There's whole lot of other details that you might like to have a glance. They are available with the <a href="http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html">ansible documentation</a>.</p>
<h3>Back to the playbooks</h3>
<p>Let's create the playbook 'nginx.yml' now, and then we'll see that in detail:</p>
<div class="highlight"><pre><span></span><code>---
- hosts: vag
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: true
</code></pre></div>
<p>This is just the playbook version of the bash command we performed earlier using ansible to install nginx.</p>
<p>First let's see what each key means and then we'll run this playbook.</p>
<p>We can perform a lot of tasks for different hosts defined in the inventory, inside a single playbook. We can have a list of dictionaries, and depending upon the hosts specified, the tasks will be executed for that specific host. If you want to perform the tasks for all the hosts defined in the inventory file, you can also give the value <code>all</code> for hosts key. Here we are considering the host <code>vag</code> we defined previously in the inventory.</p>
<p><code>become</code> is required to use apt. Otherwise, we'd come across an error (as we did while executing directly using bash). Note that, <code>yes</code> is just one of the ways to provide a boolean value. You can specify <code>True</code>, <code>TRUE</code>, <code>yes</code> and so on...</p>
<p><code>tasks</code> is again a list of dictionaries defining the tasks we want to perform against the target machine.<br>
Inside tasks, the first key is <code>name</code> , we can name it anything, this will be shown when we run the playbook, so keep the name quite descriptive.<br>
Secondly we specify the module to use for this first task. Here <code>apt</code> is used.<br>
Now we are supplying arguments to <code>apt</code> using YAML syntax, values are same as we did while executing from terminal. The arguments are just key value pair, where the module name acts as dictionary name.</p>
<p>You can also specify <code>latest</code> as a value to <code>state</code> key. This will update the package too, if an update comes. Whereas <code>present</code> would just ensure that the package is installed on the target machine.</p>
<p>Run the playbook using:</p>
<div class="highlight"><pre><span></span><code>ansible-playbook nginx.yml -i inventory
</code></pre></div>
<p>This is same as the single line command we performed earlier, just that we have a named task here. Output here will be similar to this:
(If you are trying this on your local machine, you might need to pass a : <code>--ask-become-pass</code> or <code>-K</code> as a flag to <code>ansible-playbook</code> command.)</p>
<pre>PLAY [me] **********...
TASK [Gathering Facts] **********...
<font color="#4E9A06">ok: [vag]</font>
TASK [Install Nginx] **********...
<font color="#C4A000">changed: [vag]</font>
PLAY RECAP **********...
<font color="#C4A000">vag</font> : <font color="#4E9A06">ok=2 </font> <font color="#C4A000">changed=1 </font> unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</pre>
<p>Now you got a playbook running, you can add more tasks, find more modules and hack through.
Bigger projects even have a playbook spread over folders, where, tasks, defaults, vars, etc. all have a separate folder and a main.yml in each of them. </p>
<p>Hope this will get you started on ansible.</p>
<p>Thanks a lot for reading!</p>MD5 Hashing using C#2019-08-24T00:46:59+05:302019-08-24T00:46:59+05:30sanyam_khuranatag:thegeekyway.com,2019-08-24:/md5-hashing-using-c/<p>Its common now a days that all the passwords are being encrypted in order to prevent data theft. So the passwords are being encrypted, the most common and most secure encryption method is MD5 hashing.<br>
<strong>MD5</strong> hashing stands for Message Digest algorithm 5. The idea of MD5 algorithm is to …</p><p>Its common now a days that all the passwords are being encrypted in order to prevent data theft. So the passwords are being encrypted, the most common and most secure encryption method is MD5 hashing.<br>
<strong>MD5</strong> hashing stands for Message Digest algorithm 5. The idea of MD5 algorithm is to convert all the string/char/integer into a fixed “32″ bit hexadecimal code. The input can be of any size, but the output will always be fixed, here is an example of MD5 hashing algorithm.</p>
<p><img alt="MD5 hashing algorithm" src="http://thegeekyway.com/uploads/images/2014/03/MD5-hashing-algorithm.png"></p>
<p>The main point is that whatever the length of the input is, the output will always be of “32″ bit. The namespace used is <strong>System.Security.Cryptography</strong> and the assembly used is <strong>mscorlib (in mscorlib.dll)</strong>.<br>
The first step in creating a MD5 encryption using C# is :- </p>
<h2>Step 1 : Include the required namespace.</h2>
<div class="highlight"><pre><span></span><code><span class="nv">using</span><span class="w"> </span><span class="nv">System</span>.<span class="nv">Security</span>.<span class="nv">Cryptography</span><span class="c1">;</span>
<span class="nv">using</span><span class="w"> </span><span class="nv">System</span>.<span class="nv">Text</span><span class="c1">;// for stringbuilder class</span>
</code></pre></div>
<h2>Step 2 : Create an object of MD5 class.</h2>
<div class="highlight"><pre><span></span><code>MD5 md5H=MD5.Create();
</code></pre></div>
<h2>Step 3 : Convert the string into byte array and compute its hash.</h2>
<div class="highlight"><pre><span></span><code>byte[]data=md5H.ComputeHash(Encoding.UTF8.GetBytes(text));
</code></pre></div>
<h2>Step 4 : Loop through each byte of hashed data and format each one as a hexadecimal string</h2>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="k">data</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="n">sBuilder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="k">data</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span><span class="ss">"x2"</span><span class="p">));</span>
</code></pre></div>
<p>The whole combined C# coding is</p>
<div class="highlight"><pre><span></span><code><span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Collections</span><span class="p">.</span><span class="n">Generic</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Linq</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">.</span><span class="n">WebControls</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="n">Security</span><span class="p">.</span><span class="n">Cryptography</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="nc">Text</span><span class="p">;</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">partial</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nl">MD5Hashing</span><span class="p">:</span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">UI</span><span class="p">.</span><span class="n">Page</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">MD5Hash</span><span class="p">(</span><span class="n">string</span><span class="w"> </span><span class="nc">text</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">MD5</span><span class="w"> </span><span class="n">md5H</span><span class="o">=</span><span class="n">MD5</span><span class="p">.</span><span class="k">Create</span><span class="p">();</span>
<span class="w"> </span><span class="o">//</span><span class="nf">convert</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">byte</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">compute</span><span class="w"> </span><span class="n">its</span><span class="w"> </span><span class="n">hash</span>
<span class="w"> </span><span class="n">byte</span><span class="err">[]</span><span class="k">data</span><span class="o">=</span><span class="n">md5H</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">Encoding</span><span class="p">.</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="nc">text</span><span class="p">));</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="k">create</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">stringbuilder</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">collect</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">bytes</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">create</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="n">StringBuilder</span><span class="w"> </span><span class="n">sB</span><span class="o">=</span><span class="k">new</span><span class="w"> </span><span class="n">StringBuilder</span><span class="p">();</span>
<span class="w"> </span><span class="o">//</span><span class="n">loop</span><span class="w"> </span><span class="n">through</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">byte</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">hashed</span><span class="w"> </span><span class="k">data</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nf">format</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">one</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">hexadecimal</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="k">data</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">sB</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="k">data</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span><span class="ss">"x2"</span><span class="p">));</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="o">//</span><span class="k">return</span><span class="w"> </span><span class="n">hexadecimal</span><span class="w"> </span><span class="n">string</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">sB</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
</code></pre></div>
<p><img alt="MD5 hashing output" src="http://thegeekyway.com/uploads/images/2014/03/MD5-hashing-output.png"></p>Hail GraphQL2019-08-12T12:00:00+05:302019-08-12T12:00:00+05:30storymode7tag:thegeekyway.com,2019-08-12:/hail-graphql/<div style="text-align:center">
<p><img alt="GraphQL Logo" src="//thegeekyway.com/uploads/images/2019/08/graphql-logo.png"></p>
</div>
<p>We start with basics, but then we've to upgrade. But why? Because the latest solution to the old problem is more elegant and much faster! To me, it happened to be GraphQL. It's a query language that follows you. The mantra? The structure remains the same. Let's see how we …</p><div style="text-align:center">
<p><img alt="GraphQL Logo" src="//thegeekyway.com/uploads/images/2019/08/graphql-logo.png"></p>
</div>
<p>We start with basics, but then we've to upgrade. But why? Because the latest solution to the old problem is more elegant and much faster! To me, it happened to be GraphQL. It's a query language that follows you. The mantra? The structure remains the same. Let's see how we can get started with GraphQL using GitHub's API as an example.</p>
<!--more-->
<h2>Background</h2>
<p>I was working on <a href="https://github.com/CuriousLearner/CPython-Pull-Requests">CPython-Pull-Requests</a>, which queries GitHub to show files and a list of PRs opened against them. (Want to check it out? <a href="https://cpython-pulls.herokuapp.com/">Go here</a>)
Previous work by <a href="https://github.com/csabella">Cheryl Sabella</a> was awesome. But it could do with a few touchups. Which <a href="https://github.com/CuriousLearner/">CuriousLearner</a> and <a href="https://github.com/storymode7/">me</a> and <a href="https://github.com/geekyshacklebolt">GeekyShacklebolt</a> did.
The previous version had one big drawback. It took ~10 minutes to get all the relevant data from GitHub.
That was because GitHub's REST API got much more data than what we required. Then we were advised to use GraphQL.
And guess what? The query takes ~27 Seconds now!</p>
<h2>What is GraphQL</h2>
<p><a href="https://graphql.org/">GraphQL</a> is a Query Language (The QL in GraphQL) and is used to fetch data from servers. But what makes it different is how you query the server. Your query has the same structure as that of your required response. And due to this you only get what you ask. Nothing extra.</p>
<h2>GitHub terminologies</h2>
<h3>Object</h3>
<p>Object means resources that you can access. An object has related connections. The connections have multiple edges all reaching to a different node. Examples of objects are repositories, issues, gists, blames, etc.
Complete list: <a href="https://developer.github.com/v4/object/">https://developer.github.com/v4/object/</a></p>
<h3>Connection</h3>
<p>Connection allows you to query for related objects. Like <a href="https://developer.github.com/v4/object/repository/">here</a> you can see a list of connections available under the repository object.</p>
<h3>Edge</h3>
<p>A link between nodes. You need to go through an edge to a node. But since an edge will always take you to a node so you don't need to specify edge in your query. Though providing edge isn't wrong. It clarifies the meaning. But since it's obvious, it's optional.
Like</p>
<div class="highlight"><pre><span></span><code><span class="nx">edge</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">node</span><span class="p">{</span>
<span class="w"> </span><span class="nx">stuff</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Example: <a href="https://developer.github.com/v4/object/repositoryedge/">RepositoryEdge</a></p>
<h3>Nodes</h3>
<p>Objects, node, and fields are sometimes used interchangeably. But what helps me clarify node is that <code>node</code> is the final part of your query that returns some scalar(A value with the base data type. Like Int, String, Boolean). Each node should return a scalar. If it does not, you have to include subfields until they return scalars.
In a node, you can specify what fields you require. Remember that the final field that is returned inside a node should be a scalar.</p>
<p>NOTE: To find various fields, connections, nodes you can refer here <a href="https://developer.github.com/v4/query/">https://developer.github.com/v4/query/</a></p>
<h2>Enough talk. Let's get started!</h2>
<p>What better way to understand something new than to see it work, play with it. Let's take the one I used (GitHub).
You can use <a href="https://developer.github.com/v4/explorer/">GitHub's explorer</a> to test queries straight away. But it asks a <em>LOT</em> of permissions to support every type of query.
Another way is to make a smaller version yourself. With fewer permissions. Here's a short version that I wrote. For this, you just need an authentication token generated at https://github.com/settings/tokens with no scopes other than default scopes. i.e you don't need to select any option from the list of scopes provided in settings.</p>
<p><a href="//thegeekyway.com/uploads/files/graphql-without-pagination.py">Minimal Explorer</a></p>
<p>This is the minimal explorer that I wrote. But since we're not taking loads of permissions, we're a little limited. But for getting started, it's enough (Not only getting started, but for getting big too. CPython-Pull-Requests uses only default scopes, no additional scopes.)</p>
<p>So here's our first query. </p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="nx">viewer</span><span class="p">{</span>
<span class="w"> </span><span class="nx">login</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="s2">"data"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"viewer"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"login"</span><span class="o">:</span><span class="w"> </span><span class="s2">"storymode7"</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>(See the similarity? The "data" provided in the response to our query has <em>exactly</em> the same structure.)
Viewer is a <a href="https://developer.github.com/v4/object/user/">User object</a>. There is also a <code>user</code> field. The difference of <code>viewer</code> from <code>user</code> field is that viewer represents the currently authenticated user. Whereas <code>user</code> is used to lookup a user from their login. So, in this case, the response was the login of the currently authenticated user(storymode7).</p>
<p>You can also prepend <code>query</code> before your query. It is useful if you need to pass some parameters to your query.</p>
<div class="highlight"><pre><span></span><code><span class="nx">query</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">rateLimit</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">limit</span>
<span class="w"> </span><span class="nx">remaining</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="s2">"data"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"rateLimit"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"limit"</span><span class="o">:</span><span class="w"> </span><span class="mf">5000</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"remaining"</span><span class="o">:</span><span class="w"> </span><span class="mf">5000</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p><a href="https://developer.github.com/v4/object/ratelimit/">RateLimit object</a>. It contains fields related to the rate limit info of currently logged in client.</p>
<h2>Using variables in your query</h2>
<p>Like in a programming language, you can use variable to make query easy to modify. Like if you want to change the number of results returned by a query, you don't need to change your <code>query</code> every time. You can just change the relevant variable without touching the query at all.
To use variables you need to define the variable type when you write your query.
While using variables, keeping this in mind helps: You can use a <code>field</code> in an <code>object</code> as a parameter. You can also list these fields under nodes to display them.
Variables are prefixed with a <code>$</code> sign wherever they are in a query. And enclosed in <code>""</code> within <code>variables</code>.
Example:</p>
<div class="highlight"><pre><span></span><code><span class="nx">query</span><span class="p">(</span><span class="nx">$user_name</span><span class="o">:</span><span class="nb">String</span><span class="o">!</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">user</span><span class="p">(</span><span class="nx">login</span><span class="o">:</span><span class="w"> </span><span class="nx">$user_name</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">repositories</span><span class="p">(</span><span class="nx">first</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">nodes</span><span class="p">{</span>
<span class="w"> </span><span class="nx">name</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="p">{</span>
<span class="w"> </span><span class="s2">"user_name"</span><span class="o">:</span><span class="w"> </span><span class="s2">"storymode7"</span>
<span class="p">}</span>
</code></pre></div>
<p>Executing this query in GitHub explorer: Paste the variables part (The last part with "user_name") under QUERY VARIABLES
Executing this query in my minimal explorer: Paste the variables part (The last part with "user_name") under <code>variables</code>. So that it looks like:</p>
<div class="highlight"><pre><span></span><code><span class="n">variables</span> <span class="o">=</span> <span class="s1">'''</span><span class="se">\</span>
<span class="s1">{</span>
<span class="s1"> "user_name": "storymode7"</span>
<span class="s1">}</span>
<span class="s1">'''</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="s2">"data"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"user"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"repositories"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"nodes"</span><span class="o">:</span><span class="w"> </span><span class="p">[</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"name"</span><span class="o">:</span><span class="w"> </span><span class="s2">"django-init"</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">]</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Look closely on this line:
<code>query($user_name:String!) {</code>
This is to make sure that nullability matches. I.E if the field requires a variable (If they are required, the type has a <code>!</code> in end). Then your variable definition should specify the variable as compulsory too! Lookup <code>login</code> field <a href="https://developer.github.com/v4/object/user/#fields">here</a>, and you'll notice it's defined as: <code>login (String!)</code></p>
<h2>Pagination</h2>
<p>A query can not list more than hundred resources in one request. For example:</p>
<div class="highlight"><pre><span></span><code><span class="nx">query</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">repository</span><span class="p">(</span><span class="nx">owner</span><span class="o">:</span><span class="s2">"python"</span><span class="p">,</span><span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="s2">"cpython"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">pullRequests</span><span class="p">(</span><span class="nx">states</span><span class="o">:</span><span class="w"> </span><span class="nx">OPEN</span><span class="p">,</span><span class="w"> </span><span class="nx">first</span><span class="o">:</span><span class="w"> </span><span class="mf">101</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">nodes</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="n">Requesting</span><span class="w"> </span><span class="mi">101</span><span class="w"> </span><span class="n">records</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n n-Quoted">`pullRequests`</span><span class="w"> </span><span class="k">connection</span><span class="w"> </span><span class="n">exceeds</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n n-Quoted">`first`</span><span class="w"> </span><span class="k">limit</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="mi">100</span><span class="w"> </span><span class="n">records</span><span class="p">.</span>
</code></pre></div>
<p>This is where we pagination comes. The use of variables, before & after fields, cursors, python all comes in together!
Under a connection, you can see there's a pageInfo field. Example: <a href="https://developer.github.com/v4/object/pullrequestconnection/">PullRequestConnection</a>. </p>
<p>Since we're going to deal with paging forward, the fields of interest to us in <a href="https://developer.github.com/v4/object/pageinfo/">pageInfo</a> are: <code>endCursor</code> & <code>hasNextPage</code>.
So, to get all the info we need to keep turning the page until we're on the last page. We can check if we're on the last page yet, by checking the value of <code>hasNextPage</code> which is a <code>Boolean</code>. If we have a next page available, we copy the <code>endCursor</code> value and then we update the query for the next request with that <code>endCursor</code> specified in the <code>after</code> field. So now we only get details after those that were at the 'end' of the previous 'page'.
In short, to paginate:
* Check if <code>hasNextPage</code> (if not then quit)
* Copy <code>endCursor</code>
* Update the after field in query with <code>endCursor</code>'s value
* Send the query
* Repeat</p>
<p>Let's see how these <code>endCursor</code>, <code>hasNextPage</code> etc look in the Response of a small query:</p>
<p>Query:</p>
<div class="highlight"><pre><span></span><code><span class="nx">query</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">repository</span><span class="p">(</span><span class="nx">owner</span><span class="o">:</span><span class="s2">"python"</span><span class="p">,</span><span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="s2">"cpython"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">pullRequests</span><span class="p">(</span><span class="nx">states</span><span class="o">:</span><span class="w"> </span><span class="nx">OPEN</span><span class="p">,</span><span class="w"> </span><span class="nx">first</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">pageInfo</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">endCursor</span>
<span class="w"> </span><span class="nx">hasNextPage</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">totalCount</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="s2">"data"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"repository"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"pullRequests"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"pageInfo"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"endCursor"</span><span class="o">:</span><span class="w"> </span><span class="s2">"Y3Vyc29yOnYyOpHOBlW8Wg=="</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"hasNextPage"</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="s2">"totalCount"</span><span class="o">:</span><span class="w"> </span><span class="mf">1048</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Now here's an <a href="//thegeekyway.com/uploads/files/graphql-with-pagination.py">example script</a> with pagination. That will fetch titles of all PRs for "cpython" repo with owner "python".
Since we're getting a list of all the nodes(PR titles), I am capturing node of every request and appending it to the previous node.
I'm not attaching the final node created by the accumulation of all other nodes back to the response because I've achieved what I wanted.
Titles of all PRs opened in the "CPython" repo.
Also, since the printing of such a large amount of information is useless, I'm sharing a small snippet of output:</p>
<p>Output:</p>
<div class="highlight"><pre><span></span><code><span class="nv">Page</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="nv">fetched</span>
<span class="nv">Page</span><span class="w"> </span><span class="mi">11</span><span class="w"> </span><span class="nv">fetched</span>
[
<span class="w"> </span>{
<span class="w"> </span><span class="s2">"title"</span>:<span class="w"> </span><span class="s2">"bpo-29553: Fixed ArgumentParses format_usage for mutually exclusive groups"</span>
<span class="w"> </span>},
<span class="w"> </span>{
<span class="w"> </span><span class="s2">"title"</span>:<span class="w"> </span><span class="s2">"Alternarive for bpo-29553 - Fixed ArgumentParses format_usage for mutually exclusive groups"</span>
<span class="w"> </span>},
<span class="w"> </span>{
...
</code></pre></div>
<p>Note: I'm only displaying the node part here since that is the focus of this query.</p>
<h2>Is this all?</h2>
<p>No! GraphQL has a lot of features. This post includes a few that I came across while working on CPython-Pull-Requests. There are a lot of things. Like mutations, that you can use to modify data. This is the part that would require some permissions and this is the reason why GitHub's explorer is so permission heavy. It supports every type of GraphQL query that you can do on GitHub, including mutations.
There are more features that you can find out on <a href="https://developer.github.com/v4/">GitHub's GraphQL API reference</a>.</p>Introduction to Shell Script2019-06-04T08:39:00+05:302019-06-04T08:39:00+05:30GeekyShacklebolttag:thegeekyway.com,2019-06-04:/introduction-to-shell-script/<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/Shell_script.jpg"></p>
</div>
<p>Ever wanted to automate a task on your *nix machine? Or are you fed up executing some set of commands again and again? If yes, then shell scripting can help you out there. In this tutorial, introduce yourself to shell scripting, along with some hands-on examples to practice.</p>
<!--more-->
<h2>What is …</h2><div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/Shell_script.jpg"></p>
</div>
<p>Ever wanted to automate a task on your *nix machine? Or are you fed up executing some set of commands again and again? If yes, then shell scripting can help you out there. In this tutorial, introduce yourself to shell scripting, along with some hands-on examples to practice.</p>
<!--more-->
<h2>What is a shell?</h2>
<p>The shell is a user program or it is an environment provided for user interaction. It is a command language interpreter, that executes commands read from the standard input device such as a keyboard or from a file.</p>
<p>Several shells are available for Linux including:</p>
<ul>
<li><a href="https://bash.cyberciti.biz/guide/BASH" title="BASH">BASH</a> ( Bourne-Again SHell ) - Most common shell in Linux. It's Open Source.</li>
<li><a href="https://bash.cyberciti.biz/guide/CSH" title="CSH">CSH</a> (C SHell) - The C shell's syntax and usage are very similar to the C programming language.</li>
<li><a class="new" href="https://bash.cyberciti.biz/wiki/index.php?title=KSH&action=edit&redlink=1" title="KSH (page does not exist)">KSH</a> (Korn SHell) - Created by <a href="https://bash.cyberciti.biz/guide/David_Korn" title="David Korn">David Korn</a> at <a class="new" href="https://bash.cyberciti.biz/wiki/index.php?title=AT_%26_T_Bell_Labs&action=edit&redlink=1" title="AT & T Bell Labs (page does not exist)">AT & T Bell Labs</a>. The Korn Shell also was the base for the POSIX Shell standard specifications.</li>
<li><a class="new" href="https://bash.cyberciti.biz/wiki/index.php?title=TCSH&action=edit&redlink=1" title="TCSH (page does not exist)">TCSH</a> - It is an enhanced but completely compatible version of the Berkeley <a class="mw-redirect" href="https://bash.cyberciti.biz/guide/UNIX" title="UNIX">UNIX</a> <a href="https://bash.cyberciti.biz/guide/C_shell" title="C shell">C shell</a> (<a href="https://bash.cyberciti.biz/guide/CSH" title="CSH">CSH</a>).</li>
</ul>
<p>A shell is simply accessible using a terminal. If you are familiar with *nix, then you might have an experience of using it. Now, if you want to know what all shells are available in your system, then use the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>cat<span class="w"> </span>/etc/shells
<span class="c1"># /etc/shells: valid login shells</span>
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/zsh
/usr/bin/zsh
</code></pre></div>
<h2>What is a shell script?</h2>
<p>It is a programming language of shells, where you may write a set of commands to execute for a particular shell. And similar to programming, shell scripts support that sequential, conditional and iteration flow along with functions that help to make your programs more modular.</p>
<p><strong>Shell Program:</strong> It is simply the file containing shell commands. You may directly run this file instead of individually executing each command.</p>
<p><strong>Please note that each shell does the same job, but each understands different command syntax and provides different built-in functions.</strong> In this tutorial, we'll discuss shell scripting <strong>w.r.t</strong> bash. Because <strong>"bash"</strong> is used as a default shell in most *nix systems.</p>
<h2>Use cases of shell scripting</h2>
<p>The following tasks could be done easily with shell scripting.</p>
<ul>
<li><strong>Automating a series of tasks:</strong><br>
Like loading script files according to the availability of shells found in your system. Or loading OS-specific settings. (here loading means to source/run a file)</li>
<li><strong>Setting cron/anacron jobs:</strong><br>
Like every week, auto removing packages that are no longer usable.</li>
<li><strong>Executing a set of commands.</strong><br>
Like installing no. of packages, without repeatedly giving your confirmations (y).</li>
</ul>
<p>Commands you write in a script can also be executed directly on a shell, and commands you write in the shell can also be written in the script. Therefore, you may do everything with scripting, that you may think of doing with a shell.</p>
<h2>Writing your first script</h2>
<p>To create a shell program you need to go through the following steps:</p>
<p><strong>1. Create a file for the shell program.</strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>touch<span class="w"> </span>newfile.sh
</code></pre></div>
<p>Here, we have given the extension <strong>.sh </strong> or <strong>.bash</strong> (conventional for bash script), though it is not necessary, but quite useful to distinguish between scripts of different shells.</p>
<p><strong>2. Put a shebang (#!) on the first line of the file<br>
</strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
</code></pre></div></td></tr></table></div>
<p><strong>Shebang(#!)</strong> is a special comment, that informs, which shell to be selected while executing this file. It is actually the path to the shell required to run this script.</p>
<p><strong>3. Add some commands to the file</strong></p>
<div class="highlight"><pre><span></span><code>echo "Script is working fine!"
</code></pre></div>
<p><strong>4. Save the file.</strong></p>
<p><strong>5. Make the file executable and run the program.</strong></p>
<p>To run a script file, at least it must have the permission of <strong>"readability"</strong> so that we can explicitly execute it using a shell.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>bash<span class="w"> </span>newfile.sh
Script<span class="w"> </span>is<span class="w"> </span>working<span class="w"> </span>fine!
</code></pre></div>
<p>And if the script file is <strong>"executable"</strong>, then we may directly run the file as shown below.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>chmod<span class="w"> </span>+x<span class="w"> </span>newfile.sh
$<span class="w"> </span>./newfile.sh<span class="w"> </span>
Script<span class="w"> </span>is<span class="w"> </span>working<span class="w"> </span>fine!
</code></pre></div>
<p>Hurray! You got your first shell script running. Let's go ahead and explore more programming elements of scripting.</p>
<h2>Comments</h2>
<p>Writing comments is really useful in any programming language. Because the code is read much more times than it is written. Also, comments make the code easy to comprehend, thus, it is advisable to use comments to explain complex parts of the code.</p>
<p>In shell scripting, you may give comments with <strong>hash (#)</strong> symbol.</p>
<div class="highlight"><pre><span></span><code># File: newfile.sh
# Purpose: To learn and practice shell script
# Author: Your name
# These are comments in a shell script.
</code></pre></div>
<h2>Shell Parameters</h2>
<p>Values you pass to a shell program from a terminal while executing the script, are called program parameters. For example.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span>Hello<span class="w"> </span>World
</code></pre></div>
<p>Here, <strong><em>"Hello"</em></strong> and <strong><em>"World"</em></strong> are <strong>2</strong> parameters that are passed to <strong>newfile.sh</strong>. You may pass any number of parameters to a shell script. But, the question arises, how are we gonna use them in a program?</p>
<h4><strong>\$0 to \$9</strong></h4>
<ul>
<li><strong>\$0:</strong> It holds the complete path to the script file from the current working directory.</li>
<li><strong>\$1-\$9:</strong> These hold shell parameters. For example: <strong>\$2</strong> is second parameter.</li>
<li><strong>\$#:</strong> It holds the total number of shell parameters (i.e exclude \$0).</li>
</ul>
<p>To access these parameters we may use <strong>dollar (\$)</strong> symbol followed by the number of parameter. For example, if your file is.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"Running script </span><span class="nv">$0</span><span class="s2">"</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"First parameter is </span><span class="nv">$1</span><span class="s2">"</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"Second parameter is </span><span class="nv">$2</span><span class="s2">"</span>
</code></pre></div></td></tr></table></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span>Hello<span class="w"> </span>World
Running<span class="w"> </span>script:<span class="w"> </span>./newfile.sh
First<span class="w"> </span>parameter<span class="w"> </span>is:<span class="w"> </span>Hello
Second<span class="w"> </span>parameter<span class="w"> </span>is:<span class="w"> </span>World
</code></pre></div>
<p>Along with these you also have other predefined variables.</p>
<h4>\$* and \$@</h4>
<ul>
<li><strong>\$*: </strong>It has all the parameters passed as <strong>a single word</strong>, where each element is space separated.</li>
<li><strong>\$@:</strong> It has all the parameters passed but it holds each parameter as a separate word.</li>
</ul>
<p><strong>\$* and \$@</strong> seems confusing from the definition, therefore, let's take a look at the example below.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"With \$*"</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"Total parameters: </span><span class="nv">$#</span><span class="s2">"</span>
<span class="k">for</span><span class="w"> </span>i<span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="s2">"</span><span class="nv">$*</span><span class="s2">"</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$i</span><span class="s2">"</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p>The output is.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="s2">"2 3"</span><span class="w"> </span><span class="s2">"4 5"</span>
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">3</span>
With<span class="w"> </span><span class="nv">$*</span>
<span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">4</span><span class="w"> </span><span class="m">5</span>
</code></pre></div>
<p>See, here total parameters are <strong>3</strong>, but you are actually getting <strong>1</strong> string that includes all parameters<strong>.<br>
</strong></p>
<p><strong>Note: While using \$*, if the parameter is "2 3", then it is treated as "2" and "3". This means the shell interprets the space symbol. But, this is not the case with \$@.</strong></p>
<p>If you use <strong>"\$@"</strong> instead of <strong>"\$*"</strong> you'll get the following.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="s2">"2 3"</span><span class="w"> </span><span class="s2">"4 5"</span>
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">3</span>
With<span class="w"> </span><span class="nv">$@</span>
<span class="m">1</span>
<span class="m">2</span><span class="w"> </span><span class="m">3</span>
<span class="m">4</span><span class="w"> </span><span class="m">5</span>
</code></pre></div>
<p>Here, <strong>3</strong> separate parameters can be easily seen.</p>
<h4><strong>\$\$ and \$?</strong></h4>
<ul>
<li>
<p><strong>\$\$:</strong> It shows the <strong>process ID</strong>. Following command shows the process ID of the current terminal.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="nv">$$</span>
<span class="m">2123</span>
</code></pre></div>
</li>
<li>
<p><strong>\$?:</strong> It holds <strong>exit status</strong> of the last command. For example.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>clear
$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="nv">$?</span>
<span class="m">0</span>
</code></pre></div>
<p>Here, <strong>0</strong> indicates that last command (<strong>clear</strong>) was successful. In case of failure, <strong>\$?</strong> returns any non-zero value.<br>
To return exit status <strong>0</strong> or <strong>1</strong> from your shell script, simply use the <strong>exit</strong> command.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"Successful"</span>
<span class="nb">exit</span><span class="w"> </span><span class="m">0</span>
</code></pre></div></td></tr></table></div>
</li>
</ul>
<h4>Shift Command to access more than 9 parameters</h4>
<p>You may easily go from <strong>\$1</strong> to <strong>\$9.</strong> But how would you use the 10th parameter?</p>
<p>Accessing 10th parameter is not possible with <strong>"\$10"</strong> because the shell will interpret only the first digit, that is <strong>"1"</strong>, and output of this statement would be the first parameter followed by a <strong>"0"</strong>.</p>
<p>To solve this issue we have <strong>shift command</strong>. This command shifts all the parameter to left by one. As a result, it also decreases the number of total parameters <strong>(\$#).</strong> For example.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="k">for</span><span class="w"> </span>i<span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">4</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Total parameters: </span><span class="nv">$#</span><span class="s2">"</span>
<span class="w"> </span><span class="nb">shift</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Parameters: </span><span class="nv">$*</span><span class="s2">"</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p>The output of this command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span>a<span class="w"> </span>b<span class="w"> </span>c<span class="w"> </span>d<span class="w"> </span>e<span class="w"> </span>f
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">6</span>
Parameters:<span class="w"> </span>b<span class="w"> </span>c<span class="w"> </span>d<span class="w"> </span>e<span class="w"> </span>f
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">5</span>
Parameters:<span class="w"> </span>c<span class="w"> </span>d<span class="w"> </span>e<span class="w"> </span>f
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">4</span>
Parameters:<span class="w"> </span>d<span class="w"> </span>e<span class="w"> </span>f
Total<span class="w"> </span>parameters:<span class="w"> </span><span class="m">3</span>
Parameters:<span class="w"> </span>e<span class="w"> </span>f
</code></pre></div>
<p>We can see, that parameters are shifting to left and getting reduced by one. So, if you want to access 10th parameter then you need to left shift once and access <strong>\$9.</strong> However, this command is not much recommended due to the side effect of losing earlier parameters.</p>
<h2>Variables and Expressions</h2>
<p>Expressions are valid combination of variables and operators.</p>
<p><strong><em>Variables:</em></strong> These are <strong>shell parameters</strong> or any other initialized variable in a shell program like.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nv">var</span><span class="o">=</span><span class="m">10</span><span class="w"> </span><span class="c1"># An integer variable</span>
<span class="nv">str</span><span class="o">=</span><span class="s1">'Hello'</span><span class="w"> </span><span class="c1"># A string variable</span>
</code></pre></div></td></tr></table></div>
<p><strong>Note: No space around "=" (assignment) operator.</strong></p>
<p><strong><em>Operators:</em></strong> These are special symbols to operate on variables. For example.</p>
<p><strong>String operations</strong></p>
<hr>
<p><code>-z string</code> True: if the length of a <code>string</code> is 0.
<code>-n string</code> True: if the length of a <code>string</code> is not 0.
<code>string1 = string2</code> True: if the two strings are identical.
<code>string != string2</code> True: if the two strings are NOT identical.
<code>string</code> True: if the <code>string</code> is not NULL.</p>
<hr>
<p><strong>Integer operations</strong></p>
<hr>
<p><code>int1 -eq int2</code> True: if the first int is equal to the second.
<code>int1 -ne int2</code> True: if the first int is not equal to the second.
<code>int1 -gt int2</code> True: if the first int is greater than the second.
<code>int1 -ge int2</code> True: if the first int is greater than or equal to the second.
<code>int1 -lt int2</code> True: if the first int is less than the second.
<code>int1 -le int2</code> True: if the first int is less than or equal to the second.</p>
<hr>
<p><strong>File operations</strong></p>
<hr>
<p><code>-r file</code> <code>True: if the file</code> exists and is readable.
<code>-w file</code> <code>True: if the file</code> exists and is writable.
<code>-x file</code> <code>True: if the file</code> exists and is executable.
<code>-f file</code> <code>True: if the file</code> exists and is a regular file.
<code>-d file</code> <code>True: if the file</code> exists and is the directory.</p>
<hr>
<p><strong>Logical operations</strong></p>
<hr>
<p><code>!</code> reverse the result of expression
<code>-a</code> AND operator
<code>-o</code> OR operator</p>
<hr>
<h2>Conditional Flow</h2>
<p>When program control flows sequentially from start to end of a program, it is known as the sequential flow of programming. But, in case of conditional flow, the program is executed on the basis of conditional statements. If the result of such statements is true, one part of the code is executed, and if the result is false, another part of the code is executed, not both. This is called "Mutual Exclusion".</p>
<p>Shell script provides 2 conditional statements 1) <code>if-else</code> and 2) <code>case</code>. Let's have a look at their syntax and usage.</p>
<h4><strong>If-else statement</strong></h4>
<p><strong><em>Usage:</em></strong> To execute a set of commands, on the basis of evaluation of an expression.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="nv">command1</span><span class="w"> </span>#<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nv">block</span><span class="w"> </span><span class="nv">started</span>
<span class="k">then</span>
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list1</span>
<span class="nv">elif</span><span class="w"> </span><span class="nv">command2</span><span class="w"> </span>#<span class="w"> </span><span class="nv">optional</span>
<span class="k">then</span>
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list2</span>
<span class="k">else</span><span class="w"> </span>#<span class="w"> </span><span class="nv">optional</span>:<span class="w"> </span><span class="nv">to</span><span class="w"> </span><span class="nv">accept</span><span class="w"> </span><span class="nv">rest</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">conditions</span>.
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list3</span>
<span class="nv">fi</span><span class="w"> </span>#<span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="k">if</span><span class="o">-</span><span class="k">else</span><span class="w"> </span><span class="nv">block</span>
</code></pre></div>
<p>Here, <strong>elif</strong> and <strong>else</strong> parts are optional. This implies <strong>if command1</strong> returns success <strong>(0),</strong> then execute <strong>command-list1.</strong> Otherwise, <strong>if command2</strong> returns success <strong>(0),</strong> then execute <strong>command-list2,</strong> and if no condition turns out to be successful, then execute <strong>command-list3.</strong></p>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="k">if</span><span class="w"> </span>ls<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>^z<span class="w"> </span>><span class="w"> </span>/dev/null
<span class="k">then</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span>Firt<span class="w"> </span><span class="k">case</span>.
<span class="k">elif</span><span class="w"> </span>ls<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>^a<span class="w"> </span>><span class="w"> </span>/dev/null
<span class="k">then</span><span class="w"> </span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span>Second<span class="w"> </span><span class="k">case</span>.
<span class="k">else</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span>Third<span class="w"> </span><span class="k">case</span>.
<span class="k">fi</span>
</code></pre></div></td></tr></table></div>
<p>Here, "<strong>ls | grep \^z" </strong> searches files/folders in the current directory that starts with <strong>"z". </strong>And <strong>"> /dev/null" </strong>sends the output of the file in a <strong>null</strong> device (typically used for disposing of unwanted output streams of a process).</p>
<p><strong><em>The output</em></strong> of the above program is shown below. (As I don't have any file that starts with <strong>"z"</strong>)</p>
<div class="highlight"><pre><span></span><code>Second case.
</code></pre></div>
<h4><strong>Test Condition</strong></h4>
<p><strong><em>Usage:</em></strong> It is mostly used in association with if-else to evaluate expressions instead of commands.</p>
<p><strong><em>Syntax:</em></strong> <code>test expression</code> or <code>[ expression ]</code></p>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="w"> </span>[<span class="w"> </span><span class="nv">expression</span><span class="w"> </span>]
<span class="k">then</span>
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list1</span>
<span class="nv">fi</span>
</code></pre></div>
<p>Let's put all this in a short example.</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$#</span><span class="s2">"</span><span class="w"> </span>-gt<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="c1"># if total number of parameters exceed 1</span>
<span class="k">then</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Sorry, you entered too many parameters."</span>
<span class="k">elif</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$#</span><span class="s2">"</span><span class="w"> </span>-eq<span class="w"> </span><span class="m">1</span><span class="w"> </span>-a<span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"1"</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="c1"># if only one parameter is 1</span>
<span class="k">then</span><span class="w"> </span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Hi! Nice to meet you!"</span>
<span class="k">elif</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$#</span><span class="s2">"</span><span class="w"> </span>-eq<span class="w"> </span><span class="m">1</span><span class="w"> </span>-a<span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2"</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="c1"># if only one parameter is 2</span>
<span class="k">then</span><span class="w"> </span>
<span class="w"> </span><span class="nb">pwd</span>
<span class="k">elif</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$#</span><span class="s2">"</span><span class="w"> </span>-eq<span class="w"> </span><span class="m">1</span><span class="w"> </span>-a<span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="w"> </span>!<span class="o">=</span><span class="w"> </span><span class="s2">"1"</span><span class="w"> </span>-a<span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="w"> </span>!<span class="o">=</span><span class="w"> </span><span class="s2">"2"</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="c1"># if only one is parameter is neither 1 nor 2</span>
<span class="k">then</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"You entered the wrong parameter."</span>
<span class="k">else</span><span class="w"> </span><span class="c1"># if the total number of parameters is below 1</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Sorry, you entered fewer parameters."</span>
<span class="k">fi</span>
</code></pre></div></td></tr></table></div>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh
Sorry,<span class="w"> </span>you<span class="w"> </span>entered<span class="w"> </span>fewer<span class="w"> </span>parameters.
$<span class="w"> </span>./newfile.sh<span class="w"> </span><span class="m">1</span>
Hi!<span class="w"> </span>Nice<span class="w"> </span>to<span class="w"> </span>meet<span class="w"> </span>you!
$<span class="w"> </span>./newfile.sh<span class="w"> </span><span class="m">2</span>
/home/thegeekyway
$<span class="w"> </span>./newfile.sh<span class="w"> </span><span class="m">3</span>
Sorry,<span class="w"> </span>you<span class="w"> </span>entered<span class="w"> </span>the<span class="w"> </span>wrong<span class="w"> </span>parameter.
</code></pre></div>
<h4>Case statement</h4>
<p><strong><em>Usage:</em></strong> Shell script also supports <strong>case conditional statement. </strong>It is used to evaluate a particular variable with some set of values.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code>case value in
pattern1) command-list1;;
pattern2) command-list2;;
*) command-list2;; # this is optional: to accept rest of the conditions
esac
</code></pre></div>
<p><strong><em>Example:</em></strong> Let's make the above command using <strong>case statements.</strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="k">case</span><span class="w"> </span><span class="nv">$#</span><span class="w"> </span><span class="k">in</span>
<span class="w"> </span><span class="s2">"0"</span><span class="o">)</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"You entered fewer parameters."</span>
<span class="w"> </span>exit<span class="p">;;</span>
<span class="w"> </span><span class="s2">"2"</span><span class="o">)</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"You entered too many parameters."</span>
<span class="w"> </span>exit<span class="p">;;</span>
<span class="k">esac</span>
<span class="k">case</span><span class="w"> </span><span class="nv">$1</span><span class="w"> </span><span class="k">in</span>
<span class="w"> </span><span class="s2">"1"</span><span class="o">)</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Hi, nice to meet you!"</span><span class="p">;;</span>
<span class="w"> </span><span class="s2">"2"</span><span class="o">)</span><span class="w"> </span>pwd<span class="p">;;</span>
<span class="w"> </span>*<span class="o">)</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"You entered the wrong parameters."</span><span class="p">;;</span>
<span class="k">esac</span>
</code></pre></div></td></tr></table></div>
<p>The output is same as above.</p>
<h2>Repeated action commands (Loops)</h2>
<p>To write a set of commands, which repeat themselves until a condition is satisfied, we have loops in programming languages. Bash supports 3 types of loops:</p>
<ul>
<li>For loop</li>
<li>While loop</li>
<li>Until loop</li>
</ul>
<h4>For Loop</h4>
<p><strong><em>Usage:</em></strong> When iterating any variable over an iterator like "\$@", we use "for-loop".</p>
<p>Iterators are objects, having multiple values within them like lists in python. Therefore, using a for-loop we may access their individual values one by one.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">iterator</span>
<span class="n">do</span>
<span class="w"> </span><span class="n">command</span><span class="o">-</span><span class="n">list</span>
<span class="n">done</span>
</code></pre></div>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="k">for</span><span class="w"> </span>var<span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="s2">"</span><span class="nv">$@</span><span class="s2">"</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$var</span><span class="s2">"</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh<span class="w"> </span>The<span class="w"> </span>Geeky<span class="w"> </span>Way
The
Geeky
Way
</code></pre></div>
<h4>While Loop</h4>
<p><strong><em>Usage:</em></strong> To repeat a set of commands, till an expression remains true.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="k">while</span><span class="w"> </span>[<span class="w"> </span><span class="nv">expression</span><span class="w"> </span>]
<span class="k">do</span>
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list</span>
<span class="nv">done</span>
</code></pre></div>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nv">var</span><span class="o">=</span><span class="m">0</span>
<span class="k">while</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span>-le<span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="o">]</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$var</span><span class="s2">"</span>
<span class="w"> </span><span class="o">((</span>var++<span class="o">))</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh
<span class="m">0</span>
<span class="m">1</span>
<span class="m">2</span>
<span class="m">3</span>
</code></pre></div>
<h4>Until Loop</h4>
<p><strong><em>Usage:</em></strong> It is reverse of <strong>while loop</strong>, it repeats a set of commands, untill an expression becomes true.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="k">until</span><span class="w"> </span>[<span class="w"> </span><span class="nv">expression</span><span class="w"> </span>]
<span class="k">do</span>
<span class="w"> </span><span class="nv">command</span><span class="o">-</span><span class="nv">list</span>
<span class="nv">done</span>
</code></pre></div>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nv">var</span><span class="o">=</span><span class="m">0</span>
<span class="k">until</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span>-eq<span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="o">]</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$var</span><span class="s2">"</span>
<span class="w"> </span><span class="o">((</span>var++<span class="o">))</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh
<span class="m">0</span>
<span class="m">1</span>
<span class="m">2</span>
</code></pre></div>
<h2>Read input at runtime</h2>
<p><strong>Read Command:</strong> To read input from the user at runtime of shell script.</p>
<p><strong><em>Syntax:</em></strong> <code>read var1 var2</code></p>
<p>This command will hold (pause) the script to take input from the terminal. Once you give appropriate values, the shell script would run ahead.</p>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="nb">read</span><span class="w"> </span>var
<span class="k">for</span><span class="w"> </span>i<span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nv">$var</span>
<span class="k">do</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$i</span><span class="s2">"</span>
<span class="k">done</span>
</code></pre></div></td></tr></table></div>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh
The<span class="w"> </span>Geeky<span class="w"> </span>Way
The
Geeky
Way
</code></pre></div>
<h2>Functions</h2>
<p><strong><em>Usage:</em></strong> These are set of commands, which can be called any number of times in a script. Thus, it gives the feature of re-usability of code and makes the code modular.</p>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="n">function_name</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">command</span><span class="o">-</span><span class="n">list</span>
<span class="k">return</span><span class="w"> </span><span class="n">value</span><span class="w"> </span>#<span class="w"> </span><span class="n">optional</span>
<span class="p">}</span>
</code></pre></div>
<p><strong><em>Example:</em></strong></p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="ch">#!/bin/bash</span>
confirm<span class="o">()</span>
<span class="o">{</span>
<span class="nb">echo</span><span class="w"> </span>-e<span class="w"> </span><span class="s2">"Are you sure, that you want to continue (y)?: \c"</span>
<span class="nb">read</span><span class="w"> </span>var
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'y'</span><span class="w"> </span><span class="o">]</span>
<span class="k">then</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="m">0</span>
<span class="k">else</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="m">1</span><span class="w"> </span>
<span class="k">fi</span><span class="w"> </span>
<span class="o">}</span>
<span class="k">if</span><span class="w"> </span>confirm<span class="w"> </span>-eq<span class="w"> </span><span class="m">0</span>
<span class="k">then</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Continue process!"</span>
<span class="k">else</span><span class="w"> </span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Discontinue process!"</span>
<span class="k">fi</span>
</code></pre></div></td></tr></table></div>
<p><strong>Note that, no test expression in the last block of "if-else", because calling function is a command in itself. And we put expressions in the test, not commands.</strong></p>
<p><strong><em>Output:</em></strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>./newfile.sh
Are<span class="w"> </span>you<span class="w"> </span>sure,<span class="w"> </span>that<span class="w"> </span>you<span class="w"> </span>want<span class="w"> </span>to<span class="w"> </span><span class="k">continue</span><span class="w"> </span><span class="o">(</span>y<span class="o">)</span>?:<span class="w"> </span>y
Continue<span class="w"> </span>process!!
</code></pre></div>
<h2>Conclusion</h2>
<p>Knowledge of shell scripting is a required norm, not only for Linux administrators but for a general Linux user as well.</p>
<p>Being a Linux user, learning shell script is simply a boon. This sets you apart from those who just use Linux, then those who know how to use Linux!</p>
<p>To know more about shell and scripting, you may visit <a href="http://linuxcommand.org/">here</a>.</p>
<p>If you have any query or idea to improve this post, please feel free to write in the comments section below.</p>
<p>Thanks for reading!</p>[CHEAT-SHEET] List of Important IRC Commands2018-06-14T17:13:00+05:302018-06-14T17:13:00+05:30sanyam_khuranatag:thegeekyway.com,2018-06-14:/list-important-irc-commands/<p>While you start using IRC, it becomes very essential to do certain tasks. To ensure yours tasks are carried out smoothly, you must know the commands which may come handy. So, today we've compiled here a list of IRC Commands that would come handy doing different tasks in IRC.</p>
<p><strong>Changing …</strong></p><p>While you start using IRC, it becomes very essential to do certain tasks. To ensure yours tasks are carried out smoothly, you must know the commands which may come handy. So, today we've compiled here a list of IRC Commands that would come handy doing different tasks in IRC.</p>
<p><strong>Changing your Nick</strong></p>
<div class="highlight"><pre><span></span><code>/nick YourNick
</code></pre></div>
<p>Here, YourNick is the nick you want. So, if your nick was Harry, now it would be YourNick like:</p>
<div class="highlight"><pre><span></span><code>/nick YourNick
Harry has changed nick to YourNick
</code></pre></div>
<p>If YourNick is already registered, you have to identify by giving your password as shown:</p>
<div class="highlight"><pre><span></span><code><span class="o">/</span><span class="n">nick</span><span class="w"> </span><span class="n">YourNick</span>
<span class="n">Harry</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="n">changed</span><span class="w"> </span><span class="n">nick</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">YourNick</span>
<span class="o">-</span><span class="n">NickServ</span><span class="o">-</span><span class="w"> </span><span class="n">This</span><span class="w"> </span><span class="n">nickname</span><span class="w"> </span><span class="n">is</span><span class="w"> </span><span class="n">registered</span><span class="p">.</span><span class="w"> </span><span class="n">Please</span><span class="w"> </span><span class="n">choose</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">different</span><span class="w"> </span><span class="n">nickname</span><span class="p">,</span><span class="w"> </span><span class="k">or</span><span class="w"> </span><span class="n">identify</span><span class="w"> </span><span class="n">via</span><span class="w"> </span><span class="o">/</span><span class="n">msg</span><span class="w"> </span><span class="n">NickServ</span><span class="w"> </span><span class="n">identify</span><span class="w"> </span><span class="o"><</span><span class="n">password</span><span class="o">></span><span class="p">.</span>
</code></pre></div>
<p>Here substitute \<password> with the password you set when you registered your nick. When you enter your password, if it is correct, the following would be shown:</p>
<div class="highlight"><pre><span></span><code><span class="o">*</span><span class="nv">NickServ</span><span class="o">*</span><span class="w"> </span><span class="nv">identify</span><span class="w"> </span><span class="o"><</span><span class="nv">password</span><span class="o">></span>
[<span class="mi">12</span>:<span class="mi">22</span>]<span class="w"> </span><span class="o">-</span><span class="nv">NickServ</span><span class="o">-</span><span class="w"> </span><span class="nv">You</span><span class="w"> </span><span class="nv">are</span><span class="w"> </span><span class="nv">now</span><span class="w"> </span><span class="nv">identified</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nv">YourNick</span>.
</code></pre></div>
<p><strong>Getting help</strong></p>
<div class="highlight"><pre><span></span><code>/help
</code></pre></div>
<p>When used this way, you'll get an index of various things you can get help with as shown.</p>
<div class="highlight"><pre><span></span><code><span class="nv">index</span><span class="w"> </span><span class="nv">Help</span><span class="w"> </span><span class="nv">topics</span><span class="w"> </span><span class="nv">available</span><span class="w"> </span><span class="nv">to</span><span class="w"> </span><span class="nv">users</span>:
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">ACCEPT</span><span class="w"> </span><span class="nv">ADMIN</span><span class="w"> </span><span class="nv">AWAY</span><span class="w"> </span><span class="nv">CHALLENGE</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">CMODE</span><span class="w"> </span><span class="nv">CNOTICE</span><span class="w"> </span><span class="nv">CPRIVMSG</span><span class="w"> </span><span class="nv">CREDITS</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">ERROR</span><span class="w"> </span><span class="nv">EXTBAN</span><span class="w"> </span><span class="nv">HELP</span><span class="w"> </span><span class="nv">INDEX</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">INFO</span><span class="w"> </span><span class="nv">INVITE</span><span class="w"> </span><span class="nv">ISON</span><span class="w"> </span><span class="nv">JOIN</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">KICK</span><span class="w"> </span><span class="nv">KNOCK</span><span class="w"> </span><span class="nv">LINKS</span><span class="w"> </span><span class="nv">LIST</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">LUSERS</span><span class="w"> </span><span class="nv">MAP</span><span class="w"> </span><span class="nv">MOTD</span><span class="w"> </span><span class="nv">NAMES</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">NICK</span><span class="w"> </span><span class="nv">NOTICE</span><span class="w"> </span><span class="nv">OPER</span><span class="w"> </span><span class="nv">PART</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">PASS</span><span class="w"> </span><span class="nv">PING</span><span class="w"> </span><span class="nv">PONG</span><span class="w"> </span><span class="nv">PRIVMSG</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">QUIT</span><span class="w"> </span><span class="nv">STATS</span><span class="w"> </span><span class="nv">TIME</span><span class="w"> </span><span class="nv">TOPIC</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">TRACE</span><span class="w"> </span><span class="nv">UMODE</span><span class="w"> </span><span class="nv">USER</span><span class="w"> </span><span class="nv">USERHOST</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">USERS</span><span class="w"> </span><span class="nv">VERSION</span><span class="w"> </span><span class="nv">WHO</span><span class="w"> </span><span class="nv">WHOIS</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="nv">WHOWAS</span>
<span class="o">==</span><span class="w"> </span><span class="nv">index</span><span class="w"> </span><span class="k">End</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="o">/</span><span class="nv">HELP</span>.
</code></pre></div>
<p>You can get specific help by typing the stuff you need help with. For example, if you need help with WHOIS then it can be done as:</p>
<div class="highlight"><pre><span></span><code>/help WHOIS
</code></pre></div>
<p>This would give the following result:</p>
<div class="highlight"><pre><span></span><code><span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span>[<span class="nv">remoteserver</span><span class="o">|</span><span class="nv">nick</span>]<span class="w"> </span><span class="nv">nick</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">will</span><span class="w"> </span><span class="nv">display</span><span class="w"> </span><span class="nv">detailed</span><span class="w"> </span><span class="nv">user</span><span class="w"> </span><span class="nv">information</span><span class="w"> </span><span class="k">for</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">specified</span><span class="w"> </span><span class="nv">nick</span>.<span class="w"> </span><span class="k">If</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">first</span><span class="w"> </span><span class="nv">parameter</span><span class="w"> </span><span class="nv">is</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">specified</span>,<span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">will</span><span class="w"> </span><span class="nv">display</span><span class="w"> </span><span class="nv">information</span><span class="w"> </span><span class="nv">from</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">specified</span><span class="w"> </span><span class="nv">server</span>,<span class="w"> </span><span class="nv">or</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">server</span><span class="w"> </span><span class="nv">that</span><span class="w"> </span><span class="nv">the</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">user</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="nv">on</span>.<span class="w"> </span><span class="nv">This</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="nv">how</span><span class="w"> </span><span class="nv">to</span><span class="w"> </span><span class="nv">remotely</span><span class="w"> </span><span class="nv">see</span>
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="nv">idle</span><span class="w"> </span><span class="nv">time</span><span class="w"> </span><span class="nv">and</span><span class="w"> </span><span class="nv">away</span><span class="w"> </span><span class="nv">status</span>.
<span class="o">==</span><span class="w"> </span><span class="nv">WHOIS</span><span class="w"> </span><span class="k">End</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="o">/</span><span class="nv">HELP</span>.
</code></pre></div>
<p><strong> Admin Info for servers</strong></p>
<p>To get information about Admin of any server, you can use the following command:</p>
<div class="highlight"><pre><span></span><code>/Admin [<servers>]
</code></pre></div>
<p>Here servers is optional, if used without any parameter, the admin information about current server would be returned.</p>
<p>Else, the information about given server would be returned. Here is what I got when using irc.freenode.net. You can expect somewhat similar results.</p>
<div class="highlight"><pre><span></span><code><span class="o">==</span><span class="w"> </span><span class="n">Administrative</span><span class="w"> </span><span class="n">info</span><span class="w"> </span><span class="n">about</span><span class="w"> </span><span class="n">herbert</span><span class="p">.</span><span class="n">freenode</span><span class="p">.</span><span class="n">net</span>
<span class="o">==</span><span class="w"> </span><span class="n">You</span><span class="s1">'re using freenode! For assistance, please '</span><span class="o">/</span><span class="n">stats</span><span class="w"> </span><span class="n">p</span><span class="err">'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="n">someone</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">list</span>
<span class="o">==</span><span class="w"> </span><span class="k">For</span><span class="w"> </span><span class="n">further</span><span class="w"> </span><span class="n">assistance</span><span class="p">,</span><span class="w"> </span><span class="n">please</span><span class="w"> </span><span class="n">see</span><span class="w"> </span><span class="nl">http</span><span class="p">:</span><span class="o">//</span><span class="n">freenode</span><span class="p">.</span><span class="n">net</span><span class="o">/</span><span class="n">faq</span><span class="p">.</span><span class="n">shtml</span><span class="p">,</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="n">email</span>
<span class="o">==</span><span class="w"> </span><span class="n">support</span><span class="nv">@freenode</span><span class="p">.</span><span class="n">net</span>
</code></pre></div>
<p><strong> AWAY Message</strong></p>
<p>The Away message could be used to inform other people that you're away for sometimes, it can be used as:</p>
<div class="highlight"><pre><span></span><code>/AWAY <message>
</code></pre></div>
<p>Here message is any information you want to tell people for why you are going away like:</p>
<div class="highlight"><pre><span></span><code>/AWAY Going to get some food, feeling hungry, brb in 10 mins.
</code></pre></div>
<p>This would return the following:</p>
<div class="highlight"><pre><span></span><code>== You have been marked as being away
</code></pre></div>
<p>When used without any message, the away status will be removed.</p>
<p><strong>Get WHOIS Information</strong></p>
<p>Often you might want to see the whois information for someone, whom you don't recognize. The whois command can be used as:</p>
<div class="highlight"><pre><span></span><code>/WHOIS BellBaghton
</code></pre></div>
<p>Here BellBaghton is the nick you want the information about. This would return the following:</p>
<div class="highlight"><pre><span></span><code><span class="n">BellBaghton</span><span class="w"> </span><span class="p">[</span><span class="o">~</span><span class="n">BellBaghton</span><span class="mf">@120.59.14.51</span><span class="p">]</span>
<span class="o">==</span><span class="w"> </span><span class="n">realname</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">Bell</span><span class="w"> </span><span class="n">Baghton</span>
<span class="o">==</span><span class="w"> </span><span class="n">server</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">kornbluth</span><span class="p">.</span><span class="n">freenode</span><span class="p">.</span><span class="n">net</span><span class="w"> </span><span class="p">[</span><span class="n">Frankfurt</span><span class="p">,</span><span class="w"> </span><span class="n">Germany</span><span class="p">]</span>
<span class="o">==</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">is</span><span class="w"> </span><span class="n">using</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">secure</span><span class="w"> </span><span class="n">connection</span>
<span class="o">==</span><span class="w"> </span><span class="n">account</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">BellBaghton</span>
<span class="o">==</span><span class="w"> </span><span class="n">End</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">WHOIS</span>
</code></pre></div>
<p><strong>NOTE:</strong> BellBaghton is just arbitrary nick and all information in result is changed here. It is for illustration purpose only.</p>
<p><strong>Action Messages</strong></p>
<p>All action messages can be displayed by using following command:</p>
<div class="highlight"><pre><span></span><code>/me <my action>
</code></pre></div>
<p>Here \<my action> can be replaced with any action you're doing like:</p>
<div class="highlight"><pre><span></span><code>/me thinks that IRC is really good.
</code></pre></div>
<p>This would return the following:</p>
<div class="highlight"><pre><span></span><code>* YourNick thinks that IRC is really good.
</code></pre></div>
<p>Here YourNick is the nick you're using.</p>
<p>There is a lot more, which I'll add later in the post.</p>Symmetric and Asymmetric Encryption2018-04-09T18:00:00+05:302018-04-09T18:00:00+05:30GeekyShacklebolttag:thegeekyway.com,2018-04-09:/symmetric-and-asymmetric-encryption/<p>Secured communication is inevitably a required norm in the world. To achieve the same, different methods are being approached. You must have heard the word <strong>"cryptography"</strong>, which is really useful to safely transmit your signals by ensuring secrecy. Today, with advancements in this field, we have 2 of its widely …</p><p>Secured communication is inevitably a required norm in the world. To achieve the same, different methods are being approached. You must have heard the word <strong>"cryptography"</strong>, which is really useful to safely transmit your signals by ensuring secrecy. Today, with advancements in this field, we have 2 of its widely known types i.e. private key cryptography (symmetric encryption) and public key cryptography (asymmetric encryption). What's the difference between them? Which one is better in what context? Let's see!</p>
<!--more-->
<p>First, let's understand the basic terminologies:-<br>
<strong>Cryptography:</strong><br>
It is the technique of writing or storing information in a way that it’s revealed only to those who need to see it, and hides it from all others.<br>
<strong>Encryption:</strong><br>
It is the implementation of cryptography. This converts data to ciphertext.<br>
<strong>Decryption:</strong><br>
It is reverse of encryption. This converts ciphertext to readable data.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2017/12/Encryption-decryption-blog.png" title="Encryption-Decryption"></p>
</div>
<h2>Symmetric Encryption:-</h2>
<p>It is also known as private key encryption or private key cryptography. Here, only one key (secret key) is used for both encryption and decryption, by all the parties involved in communication over a network. That's why it is faster than asymmetric encryption.</p>
<p>The same key is kept for the entire session of communication. Therefore, this key is also known as <strong>"session key"</strong>. This session key is either developed by both the parties (client and server) or alone by the client. If it is developed by the client only, then it is required to transmit to the server. In this case, a special concern is provided while transmitting this session key over a network. Because, if the key would get into the wrong hands, s/he can see all your transmitting information with any possible intention.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2017/12/Symmetric-encryption-blog.png" title="Symmetric Encryption"></p>
</div>
<p>Modern approaches of symmetric encryption are executed using algorithms such as DES, QUAD, Blowfish, AES, 3DES, RC4 etc.<br>
This type of encryption is used, when we are interested in transmission speed such as in transferring large files.</p>
<h2>Asymmetric Encryption:-</h2>
<p>It is also known as public key encryption or public key cryptography. Here, a pair of keys is used for encryption and decryption. These are the public key and the private key. The public key is used for encryption and the private key is used for decryption. Here, the private key is kept a secret to the owner and the public key is kept with all the servers to whom this client wants to connect.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2017/12/Asymmetric-encryption-blog.png" title="Asymmetric Encryption"></p>
</div>
<p><em>Understanding asymmetric encryption with workflow.</em></p>
<p>Suppose, a client wants to send a signal to a server. It will encrypt this signal using <strong>"server's public key"</strong> and will transmit over the network. When server will receive this signal, it will decrypt this, using <strong>"server's own private key"</strong>.</p>
<p>The same is followed by a signal coming from the server. The server will encrypt the signal using <strong>"client's public key"</strong> and the client will decrypt this, using <strong>"client's own private key"</strong>.</p>
<p>In this way, 2 keys equally participate in the process of public key cryptography. Due to the involvement of 2 keys, it is slower and more complex than symmetric encryption. Diffie-Hellman and RSA are the most widely used algorithms for asymmetric encryption.</p>
<h3>Key differences between symmetric and asymmetric encryption:-</h3>
<ol>
<li>Symmetric encryption involves only one key e.i session key. While asymmetric encryption uses two keys e.i public and private key.</li>
<li>Symmetric encryption is faster and simpler than asymmetric encryption.</li>
<li>Symmetric encryption is preferable over asymmetric encryption when a large file transfer is concerned.</li>
<li>Asymmetric encryption is used to share the session key using which, symmetric encryption can be initiated.</li>
<li>Asymmetric encryption is newer than symmetric encryption.</li>
<li>In case of asymmetric encryption, no secret channel is necessary for the exchange of the public key.</li>
</ol>
<p>With this, we have reached the end of this post. If you have any query regarding the topic, please let us know in the comments section below.</p>
<p>Have a good day!</p>Get rid of your old terminal, Go Fish-ing2018-04-03T17:43:00+05:302018-04-03T17:43:00+05:30vipulgupta2048tag:thegeekyway.com,2018-04-03:/get-rid-of-your-old-terminal-go-fish-ing/<div style="text-align:center">
<p>Tired of using Bash terminal? Struggling with shell scripting. Want to try something better, easier to customize, configure. Fish is a simple, powerful yet user-friendly command-line shell giving you features that you have never heard of such as auto-suggestions, cleaner more organized scripts, web-based configuration/ customizations and a lot more …</p></div><div style="text-align:center">
<p>Tired of using Bash terminal? Struggling with shell scripting. Want to try something better, easier to customize, configure. Fish is a simple, powerful yet user-friendly command-line shell giving you features that you have never heard of such as auto-suggestions, cleaner more organized scripts, web-based configuration/ customizations and a lot more. Read on for my take on Fish and how it can make you more productive in your day-to-day.<a href="//thegeekyway.com/uploads/images/2018/04/fish_screenshot-e1523016144797.png"><img alt="" src="//thegeekyway.com/uploads/images/2018/04/fish_screenshot-e1523016144797.png"></a></p>
</div>
<!--more-->
<h3>Simple Installation</h3>
<p>Fish is easily available on Linux (All popular distros), Windows, and MacOS. Here are the steps for installing it on Debian.</p>
<div class="highlight"><pre><span></span><code>sudo apt-add-repository ppa:fish-shell/release-2
sudo apt-get update
sudo apt-get install fish
</code></pre></div>
<p>You can find similar installation steps, for other distros and operating systems <a href="https://fishshell.com/">here</a>. When fish gets installed, type the following in your terminal.</p>
<div class="highlight"><pre><span></span><code>- $ fish
</code></pre></div>
<p>And you are ready to go.</p>
<p>For Fish to remember and suggest properly, try running this command when you install new applications. This will start scanning the man pages of your distros and suggest new arguments as you type in the terminal. To accept a suggestion, press the right arrow key to complete the command and it as simple as that.</p>
<div class="highlight"><pre><span></span><code>- $ fish_update_completions
</code></pre></div>
<h3>Let's Play</h3>
<p>The official website of Fish offers an easy tutorial for both beginners and professionals to get started and make the most of it. I'll be mentioning the features I use the most and love while using Fish.</p>
<h4>Summons commands like a pro</h4>
<p>Fish provides suggestions and completions to every command and their specific arguments that you would type in the shell, from the get-go. Start typing to see it for yourself.<br>
This works on its ability to predict arguments straight from man pages and from the history it maintains, of the previous commands you entered. (Don't worry its smart enough not to record any sensitive information)</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2018/04/completions.png" title="A glimpse of what suggestions feel like in Fish"><img alt="" src="//thegeekyway.com/uploads/images/2018/04/completions-e1522570951842.png"></a></p>
</div>
<p>No need to remember arcane syntax, arguments or commands. Since it remembers what you type, it will keep suggesting you more recent commands to increase productivity and let you focus more on what you are going to type next.</p>
<h4>Similarity to old 'Bash' (and some new scripting rules)</h4>
<p>Commands you use in bash or to write shell scripts in Bash are mostly the same in Fish. There are some differences. But the alternatives are much easier to use. Check out the <a href="https://fishshell.com/docs/current/index.html">documentation</a> for more information.<br>
Man pages are a smart way to get more information about commands. But reading and searching for them are a pain. Fish comes to the rescue by opening man pages in web browsers. Give it a try.</p>
<div class="highlight"><pre><span></span><code>- $ help set
</code></pre></div>
<h4>Magic happens when you press Tab</h4>
<p>Terminals usually autocomplete your half-written commands when you press the Tab key. Fish does that and a ton of other stuff. Here are a few snippets</p>
<div class="highlight"><pre><span></span><code><span class="o">></span><span class="w"> </span><span class="o">/</span><span class="n">geek</span><span class="w"> </span><span class="nv">@press</span><span class="err">{</span><span class="n">Tab</span><span class="err">}</span><span class="w"> </span><span class="err">→</span><span class="w"> </span><span class="o">/</span><span class="n">geekyway</span><span class="o">/</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="o">></span><span class="w"> </span><span class="o">~/</span><span class="n">geekyway</span><span class="o">/</span><span class="n">v</span><span class="w"> </span><span class="nv">@press</span><span class="err">{</span><span class="n">Tab</span><span class="err">}</span>
<span class="o">~/</span><span class="nf">stuff</span><span class="o">/</span><span class="n">vipul</span><span class="p">.</span><span class="n">sh</span><span class="w"> </span><span class="p">(</span><span class="n">Executable</span><span class="p">,</span><span class="w"> </span><span class="mf">4.8</span><span class="n">kB</span><span class="p">)</span><span class="w"> </span><span class="o">~/</span><span class="nf">stuff</span><span class="o">/</span><span class="n">vipul</span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="n">Directory</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="o">></span><span class="w"> </span><span class="o">~/</span><span class="n">vipulgupta2048</span><span class="o">/</span><span class="w"> </span><span class="nv">@press</span><span class="err">{</span><span class="n">tab</span><span class="err">}</span><span class="w"> </span>
<span class="k">go</span><span class="o">/</span><span class="w"> </span>
<span class="n">aeious</span><span class="o">/</span><span class="w"> </span>
<span class="n">Android</span><span class="o">/</span><span class="w"> </span>
<span class="n">AndroidStudioProjects</span><span class="o">/</span>
<span class="err">…</span><span class="ow">and</span><span class="w"> </span><span class="mi">44</span><span class="w"> </span><span class="n">more</span><span class="w"> </span><span class="k">rows</span>
</code></pre></div>
<p>Keep pressing tab, shift+tab, arrow keys and other combinations to fully explore all the amazing features of Fish. Comment down below to let us know what cool new features you found while exploring.</p>
<h4>Want to customize? Fish has you covered</h4>
<p>Programmers love to customize their own personal setups be it desktop environments, text editors, and even terminals. I am no different. Hence when I found that you can customize Fish using a web browser and not write code for it. I was blown away. Try it out by typing</p>
<div class="highlight"><pre><span></span><code>> fish_config
</code></pre></div>
<p>I can bet you haven't seen a smarter, more intuitive way to customize and change your terminal settings that include your prompt and its colors, change your functions, abbreviations and many other components.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2018/04/Screenshot_2018-04-01_09-44-47.png" title="Customise anything you want ..."><img alt="" src="//thegeekyway.com/uploads/images/2018/04/Screenshot_2018-04-01_09-44-47.png"></a></p>
</div>
<h4>Other Awesome Features</h4>
<ul>
<li>Syntax in Fish gets highlighted automatically.</li>
<li>Separating Commands (Semicolon)</li>
<li>Combiners (And, Or, Not)</li>
<li>Conditionals (If, Else, Switch)</li>
<li>And many more...</li>
</ul>
<h4>Sold Already, Make it default</h4>
<p>To find the shells installed in your system, run this command.</p>
<div class="highlight"><pre><span></span><code>> cat /etc/shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/tmux
/usr/bin/fish
/usr/local/bin/fish
</code></pre></div>
<p>You need to select "/usr/local/bin/fish" and then enter the command to change your default shell.</p>
<div class="highlight"><pre><span></span><code>> chsh -s /usr/local/bin/fish
</code></pre></div>
<p>That's it now for the basics to get you started, I guess Happy Fish-ing !!</p>
<p>Share this post with your co-workers, batchmates, friends who struggle with using Bash, want a change, or just want to try new things.</p>
<p><strong>More to come</strong> (Part 2- <strong>Tips/tricks of Fish</strong> and <strong>Writing Scripts for Fish</strong>)<br>
Fork and star my dotfiles for more reference, and if you want the greeting message that I have on my terminal. - <a href="https://github.com/vipulgupta2048/dotvfiles/tree/master/fish">vipulgupta2048/dotvfiles/fish</a></p>Runlevels and rc.d scripts2018-03-16T16:07:00+05:302018-03-16T16:07:00+05:30storymode7tag:thegeekyway.com,2018-03-16:/runlevels-and-rc-d-scripts/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2018/01/linux-boot-and-shutdown-scripts1.png"><img alt="" src="//thegeekyway.com/uploads/images/2018/01/linux-boot-and-shutdown-scripts1.png"></a></p>
</div>
<p>Many operating systems use the concept of runlevels. This is particularly the case for System V Unix based systems. As you might have read in our earlier post <a href="http://thegeekyway.com/understanding-complete-boot-process-linux/">how Linux boots</a> it uses runlevels to determine what to do. Let's study runlevels and rc scripts in detail.</p>
<h2>Runlevels</h2>
<p>Runlevels are …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2018/01/linux-boot-and-shutdown-scripts1.png"><img alt="" src="//thegeekyway.com/uploads/images/2018/01/linux-boot-and-shutdown-scripts1.png"></a></p>
</div>
<p>Many operating systems use the concept of runlevels. This is particularly the case for System V Unix based systems. As you might have read in our earlier post <a href="http://thegeekyway.com/understanding-complete-boot-process-linux/">how Linux boots</a> it uses runlevels to determine what to do. Let's study runlevels and rc scripts in detail.</p>
<h2>Runlevels</h2>
<p>Runlevels are the state to which a computer intends to go, or it intends to reach. There are total 7 runlevels.</p>
<p>Runlevel 0: Halt or Shutdown</p>
<p>Runlevel 1: Single-user mode</p>
<p>Runlevel 2: Basic Multi-user mode but without networking</p>
<p>Runlevel 3: Full text-based Multi-user mode (includes networking)</p>
<p>Runlevel 4: X11 with session managers (Generally not used by most of the operating systems and is customizable)</p>
<p>Runlevel 5: Full GUI based Multi-user mode</p>
<p>Runlevel 6: Reboot</p>
<p>These are the basic definitions what each runlevel is responsible for. For example, if an operating system is required to shut down, then it'd go to runlevel 0. You can even switch between these runlevels using the init command. Just type init \<runlevel> and it'd change the current runlevel to the one specified. For example:</p>
<div class="highlight"><pre><span></span><code>init 0
</code></pre></div>
<p>will shut down the system.</p>
<p>Each system has a default runlevel to which it boots. Like Fedora has runlevel as default. To see which runlevel you are in type:</p>
<div class="highlight"><pre><span></span><code>runlevel
</code></pre></div>
<p>To check the default runlevel of your system, type:</p>
<div class="highlight"><pre><span></span><code>systemctl get-default
</code></pre></div>
<p>You can even change the default, keeping in mind what runlevel provided, like you want it to be multiuser or the graphical one. Using it you can set the default like:</p>
<div class="highlight"><pre><span></span><code>systemctl set-default multi-user.target
rm '/etc/systemd/system/default.target # To remove current default
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'
</code></pre></div>
<p>Runlevel and target file have the following relation:</p>
<p>runlevel 0: poweroff.target</p>
<p>runlevel 1: rescue.target</p>
<p>runlevel 2: multi-user.target</p>
<p>runlevel 3: multi-user.target</p>
<p>runlevel 4: multi-user.target</p>
<p>runlevel 5: graphical.target</p>
<p>runlevel 6: reboot.target</p>
<p>You should never mess with the default runlevel unless you are absolutely sure with what you're doing. Like, if you manage to set runlevel 0 or 6 as default you will end up in a loop kind of condition. Your PC will shut down( or reboot depending on what runlevel you set) as soon as it has booted.</p>
<h2>rc Scripts</h2>
<p>When a computer has entered into a specific runlevel, it runs scripts according to the runlevel it's in. The rc here comes from use in the older computers which had rc files to contain commands that run at startup. These files were called runcom files. Where runcom files meant files used to RunCommand.</p>
<p>Your script must contain the line before any command executes:</p>
<div class="highlight"><pre><span></span><code>. /etc/init.d/functions
</code></pre></div>
<p>This is because, your script might be executed at a time when nothing is loaded, not even file system( though it can be specified in boot facilities in <code>Required-Start</code> in LSB styled stanzas discussed below) so we can not say that each command we write will already be present( Remeber that each command is executed from a script present in PATH variable of your system). Hence in order for those commands to work, we need the functions file to be loaded as the first step. This function file contains the functions that a shell script might use.</p>
<p>Fedora and most of the distros have folders that contain scripts that are to be run at a specific runlevel. They are located in the <code>/etc</code> folder. The folders containing these scripts are:</p>
<p><code>/etc/init.d</code><br>
<code>/etc/rc.d/rc0.d</code><br>
<code>/etc/rc.d/rc1.d</code><br>
<code>/etc/rc.d/rc2.d</code><br>
<code>/etc/rc.d/rc3.d</code><br>
<code>/etc/rc.d/rc4.d</code><br>
<code>/etc/rc.d/rc5.d</code><br>
<code>/etc/rc.d/rc6.d</code></p>
<p>And, folders with the same name also exist in <code>/etc</code> but they are actually symbolic links of there corresponding folders in <code>/etc/rc.d</code> For example:</p>
<p><code>/etc/rc0.d (Symbolic link of /etc/rc.d/rc0.d)</code></p>
<p>Each of these folders contain symbolic links of scripts that are actually stored in init.d for the runlevel they have in their name. Like <code>/etc/rc.d/rc0.d</code> contains a symbolic link for scripts in <code>/etc/init.d</code> that are required to be run at shutdown.</p>
<p>Now if you open any of these folders you'd find out weird names with the structure like</p>
<p><code>S|K[01-99]filename</code> where filename is the name of the script in <code>/etc/int.d</code></p>
<p>Like K50netconsole. Now let's see what these names mean.</p>
<h2>Demystifying the symlink names</h2>
<p>The terms have their meanings as follows:</p>
<p>S: start the service, if it's not already started.</p>
<p>K: Kill the service, if it's running.</p>
<p>01-99 is the priority for that start or kill task</p>
<p>filename is the exact same name of the file in <code>/etc/init.d</code> folder.</p>
<p>(Remember that it's actually <code>/etc/init.d</code> that actually houses those scripts. Rest of the folders just have symlinks to these.)</p>
<p>So, If a file named shutdown-script is to be run at shutdown all times with a priority of 90 then the file <code>shutdown-script</code> should be present in <code>init.d</code> folder. And a symlink to it must be created in <code>/etc/rc.d/rc0.d</code> with the name S90shutdown-script, rest of the folders rc[1-6] can have K90shutdown-script. If 90 is the priority required to shut down.</p>
<h2>Priority Of these scripts</h2>
<p>Essentially, it can be better thought of as a position, and think of them being executed downwards from 01 to 99. For example, if you have a script that requires the network to be switched on then the network script should be at a higher position or priority than your script. Like the network, a script can have a priority of 01 and your script can have any number greater than it. So that it's executed <em>after the network script.</em></p>
<h2>The chkconfig</h2>
<p>the chkconfig command here deserves the 'The' in the heading for how easy it makes to manage these scripts and runlevels! Yes, this command just needs your source script file and manages creation deletion, the setting of S or D and also the priority level!</p>
<p>chkconfig has an amazing man page and here are the basic commands that will get you going.</p>
<div class="highlight"><pre><span></span><code>chkconfig --list
</code></pre></div>
<p>This prints the System V services, and that will do for us.</p>
<div class="highlight"><pre><span></span><code>chkconfig --add
</code></pre></div>
<p>Is used to add your file for management by chkconfig. BUT wait. This command won't run unless you have some things set with your script.</p>
<p>First and foremost, your script should exist in the home of all scripts, and that is:</p>
<div class="highlight"><pre><span></span><code>/etc/init.d
</code></pre></div>
<p>Paste your script here.</p>
<p>Secondly, your file must have certain comment lines so that it can be managed by chkconfig.</p>
<p>It must have these comment lines, though the description is not absolutely necessary and the script would work without it, it's recommended. So that anyone who intends to clean up the unnecessary services knows if this script is essential or not:</p>
<div class="highlight"><pre><span></span><code># chkconfig: 235 29 88
# description: The description of the script like what it does
</code></pre></div>
<p>The first line here has some numbers ahead <code>chkconfig:</code> like 235, 29 and 88.</p>
<p>here the First number is the runlevels it's required to run on combined into a string. Like 235 here means it's supposed to run in runlevel 2,3 and 5.</p>
<p>The next two numbers are the priority for start and stop respectively. So in this example, start priority is 29 and stop priority is 88.</p>
<p>you can use backslash for a multiline comment though you still require a <code>#</code>, in the beginning, to identify it as a comment.</p>
<p>This is how it creates the symlinks with required priorities.</p>
<p>Another way you can do this is enter LSB(Linux Standard Base) style init stanzas instead of these two comments. For it you can do it like:</p>
<div class="highlight"><pre><span></span><code> <span class="o">###</span> <span class="kr">BEGIN</span> <span class="n">INIT</span> <span class="n">INFO</span>
<span class="o">#</span> <span class="n">Provides</span><span class="p">:</span> <span class="n">scriptA</span>
<span class="o">#</span> <span class="n">Required</span><span class="o">-</span><span class="n">Start</span><span class="p">:</span> <span class="n">scriptB</span>
<span class="o">#</span> <span class="n">Default</span><span class="o">-</span><span class="n">Start</span><span class="p">:</span> <span class="mi">2</span> <span class="mi">3</span> <span class="mi">5</span>
<span class="o">#</span> <span class="n">Default</span><span class="o">-</span><span class="n">Stop</span><span class="p">:</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">6</span>
<span class="o">#</span> <span class="n">Description</span><span class="p">:</span> <span class="n">Description</span> <span class="n">of</span> <span class="n">what</span> <span class="n">the</span> <span class="n">script</span> <span class="n">does</span>
<span class="o">###</span> <span class="kr">END</span> <span class="n">INIT</span> <span class="n">INFO</span>
</code></pre></div>
<p>This one gives you more ease though it just looks a bit more complicated.</p>
<p>In this, <code>Required-Start</code> is the script that is required to start this script. That is, it should be running before the current script gets up. <code>Provides</code>: It tells about the boot facilities that this script provides so that those will be counted as already present when another script requires it. Hence priorities will be set accordingly to what is required to be already present and what it'll provide. <code>Default-Start</code> or <code>Stop</code> tells which runlevel should this start on and stopped(if running) respectively.</p>
<p>And don't forget a shebang at the beginning of the file.</p>
<p>Now we are all set to go with these things done. Let's execute this command to add it to the management of chkconfig:</p>
<div class="highlight"><pre><span></span><code>chkconfig --add filename
</code></pre></div>
<p>like for a file named bootpref in <code>init.d</code> it would be:</p>
<div class="highlight"><pre><span></span><code>chkconfig --add bootpref
</code></pre></div>
<p>Now most probably your file will be visible with a status of on under the respective runlevels when you run</p>
<div class="highlight"><pre><span></span><code>chkconfig --list
</code></pre></div>
<p>And if it's not or you want to change runlevels you can go like:</p>
<div class="highlight"><pre><span></span><code>chkconfig bootpref --level 0 on
</code></pre></div>
<p>Will make it run on runlevel 0 that is at shutdown.</p>
<p>Similarly off argument will kill it if present in that level. If no level is specified then these act on level 2,3,4 and 5. You can also specify multiple levels like <code>--level 26</code> for level 2 and 6.</p>
<div class="highlight"><pre><span></span><code>chkconfig --del bootpref
</code></pre></div>
<p>will delete its symlinks from all rc folders but not from the init.d folder. That is required to be done manually. Ad chkconfig generally doesn't modify the content of init.d but rc folders.</p>
<p>Hope this will help you play with your scripts in a better and safe manner.</p>
<p>Thank you for reading!</p>Transfer data with SCP2018-03-14T20:17:00+05:302018-03-14T20:17:00+05:30GeekyShacklebolttag:thegeekyway.com,2018-03-14:/transfer-data-with-scp/<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/Secure_copy_protocol.png"></p>
</div>
<p>Hello readers! Transferring data to your friends' machine is not a big deal when using storage devices such as pen drives, CDs, etc. But have you ever intended to migrate files between different systems over a <strong>network,</strong> to a machine that's out of your reach, like transferring data between an …</p><div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/Secure_copy_protocol.png"></p>
</div>
<p>Hello readers! Transferring data to your friends' machine is not a big deal when using storage devices such as pen drives, CDs, etc. But have you ever intended to migrate files between different systems over a <strong>network,</strong> to a machine that's out of your reach, like transferring data between an EC2 instance and your local computer? If yes, then <strong>scp</strong> is a simple tool to help you out there. Let's find out then, what is <strong>scp</strong> and what are its use cases?<!--more--></p>
<h2>What is Secure Copy Protocol (SCP)?</h2>
<p>I recently did a post on <a href="http://thegeekyway.com/ultimate-guide-how-ssh-works/"><strong>how SSH works</strong></a>, which elaborate, behind the scene of the protocol. So, as we have <strong>SSH</strong> for a secure connection, similarly, we have <strong>scp</strong> for a secure copy. It is a network protocol, based on the <strong>BSD</strong> and<a href="http://yuba.stanford.edu/rcp/"><strong>RCP</strong></a>, which supports file transfers between hosts on a network.</p>
<p><strong>scp</strong> uses Secure Socket Shell (<strong>SSH</strong>) for data transfer and uses the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data. Therefore, for using , you first need to have an <strong><a href="https://www.ssh.com/ssh/server">SSH</a><a href="https://www.ssh.com/ssh/server">server</a></strong>. </p>
<h4>Syntax:</h4>
<p>Normal <strong>"cp"</strong> (copy) command has the following syntax on *nix platforms. Here, data is copied to a different location in the same system.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span><span class="w"> </span><span class="n">cp</span><span class="w"> </span><span class="o">[</span><span class="n">OPTION</span><span class="o">]</span><span class="p">...</span><span class="w"> </span><span class="o">[</span><span class="n">-T</span><span class="o">]</span><span class="w"> </span><span class="n">SOURCE</span><span class="w"> </span><span class="n">DEST</span>
</code></pre></div>
<p>Similarly, <strong>"scp"</strong> follows the same format. But, in this case, data is transferred between 2 different systems. Thus, its syntax is as follows.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">[</span><span class="n">OPTION</span><span class="o">]</span><span class="w"> </span><span class="n">user1</span><span class="nv">@hostname</span><span class="err">:</span><span class="o">/</span><span class="n">source</span><span class="o">/</span><span class="w"> </span><span class="n">user2</span><span class="nv">@hostname</span><span class="err">:</span><span class="o">/</span><span class="n">destination</span><span class="o">/</span>
</code></pre></div>
<h4>SCP with use cases</h4>
<h4>Transferring files on password-based-authentication</h4>
<p><strong>1. From localhost to a remote server.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span>
</code></pre></div>
<p><strong>2. From a remote server to localhost.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span><span class="n">newfile</span><span class="w"> </span><span class="o">~/</span>
</code></pre></div>
<p><strong>3. From one remote server to another, through localhost.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="mi">-3</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@19.111.19.111</span><span class="o">:~/</span>
</code></pre></div>
<p><strong>"-3" (option) </strong>copies between two remote hosts, transferred through the localhost. Note that, this option disables the progress meter. Without this option, the data is copied directly between two remote hosts, for which, they should be accessible to each other.</p>
<h4>Transferring files on key based authentication</h4>
<p><strong>1. From localhost to a remote server.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span>
</code></pre></div>
<p><strong>"-i" (option)</strong> is used to include the identity file (our private key). Necessary for key based authentication.</p>
<p><strong>2. From a remote server to localhost.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span><span class="n">newfile</span><span class="w"> </span><span class="o">~/</span>
</code></pre></div>
<p><strong>3. From one remote server to another, through localhost.</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="mi">-3</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key1</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key2</span><span class="w"> </span><span class="n">ubuntu1</span><span class="mf">@18.222.18.222</span><span class="o">:~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu1</span><span class="mf">@19.111.19.111</span><span class="o">:~/</span>
</code></pre></div>
<h2>SCP with different parameters</h2>
<p>Parameters either change the behavior or extends the functionality of a command. Let's see some of the alterations of SCP.</p>
<p><strong>Identity file: -i</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@123.12.123</span><span class="o">:~/</span>
</code></pre></div>
<p>As I have already mentioned this option includes identity file (private key).</p>
<p><strong>Recursive: -r</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">-</span><span class="n">r</span><span class="w"> </span><span class="o">~/</span><span class="n">source</span><span class="o">/</span><span class="n">folder</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@123.12.123</span><span class="o">:~/</span><span class="n">destination</span><span class="o">/</span>
</code></pre></div>
<p>This option recursively copies files from the source folder.</p>
<p><strong>Verbose: -v</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">v</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@123.12.123</span><span class="o">:~/</span><span class="n">source</span><span class="w"> </span><span class="o">~/</span><span class="n">destination</span><span class="o">/</span>
</code></pre></div>
<p>This option gives you background information while transferring files as shown below.</p>
<div class="highlight"><pre><span></span><code><span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">channel</span><span class="w"> </span><span class="mi">0</span><span class="o">:</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="o">[</span><span class="n">client</span><span class="o">-</span><span class="n">session</span><span class="o">]</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Requesting</span><span class="w"> </span><span class="n">no</span><span class="o">-</span><span class="n">more</span><span class="o">-</span><span class="n">sessions</span><span class="err">@</span><span class="n">openssh</span><span class="o">.</span><span class="na">com</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Entering</span><span class="w"> </span><span class="n">interactive</span><span class="w"> </span><span class="n">session</span><span class="o">.</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">pledge</span><span class="o">:</span><span class="w"> </span><span class="n">network</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">client_input_global_request</span><span class="o">:</span><span class="w"> </span><span class="n">rtype</span><span class="w"> </span><span class="n">hostkeys</span><span class="o">-</span><span class="mi">00</span><span class="err">@</span><span class="n">openssh</span><span class="o">.</span><span class="na">com</span><span class="w"> </span><span class="n">want_reply</span><span class="w"> </span><span class="mi">0</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Sending</span><span class="w"> </span><span class="n">environment</span><span class="o">.</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Sending</span><span class="w"> </span><span class="n">env</span><span class="w"> </span><span class="n">LANG</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">en_IN</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Sending</span><span class="w"> </span><span class="n">command</span><span class="o">:</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">v</span><span class="w"> </span><span class="o">-</span><span class="n">t</span><span class="w"> </span><span class="o">~/</span>
<span class="n">Sending</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">modes</span><span class="o">:</span><span class="w"> </span><span class="n">C0664</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">file_on_client</span>
<span class="n">Sink</span><span class="o">:</span><span class="w"> </span><span class="n">C0664</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">file_on_client</span>
<span class="n">file_on_client</span><span class="w"> </span><span class="mi">100</span><span class="o">%</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mf">0.0</span><span class="n">KB</span><span class="o">/</span><span class="n">s</span><span class="w"> </span><span class="mi">00</span><span class="o">:</span><span class="mi">00</span><span class="w"> </span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">client_input_channel_req</span><span class="o">:</span><span class="w"> </span><span class="n">channel</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">rtype</span><span class="w"> </span><span class="n">exit</span><span class="o">-</span><span class="n">status</span><span class="w"> </span><span class="n">reply</span><span class="w"> </span><span class="mi">0</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">channel</span><span class="w"> </span><span class="mi">0</span><span class="o">:</span><span class="w"> </span><span class="n">free</span><span class="o">:</span><span class="w"> </span><span class="n">client</span><span class="o">-</span><span class="n">session</span><span class="o">,</span><span class="w"> </span><span class="n">nchannels</span><span class="w"> </span><span class="mi">1</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">fd</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">clearing</span><span class="w"> </span><span class="n">O_NONBLOCK</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">fd</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">clearing</span><span class="w"> </span><span class="n">O_NONBLOCK</span>
<span class="n">Transferred</span><span class="o">:</span><span class="w"> </span><span class="n">sent</span><span class="w"> </span><span class="mi">3556</span><span class="o">,</span><span class="w"> </span><span class="n">received</span><span class="w"> </span><span class="mi">2600</span><span class="w"> </span><span class="n">bytes</span><span class="o">,</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mf">2.9</span><span class="w"> </span><span class="n">seconds</span>
<span class="n">Bytes</span><span class="w"> </span><span class="n">per</span><span class="w"> </span><span class="n">second</span><span class="o">:</span><span class="w"> </span><span class="n">sent</span><span class="w"> </span><span class="mf">1231.7</span><span class="o">,</span><span class="w"> </span><span class="n">received</span><span class="w"> </span><span class="mf">900.6</span>
<span class="n">debug1</span><span class="o">:</span><span class="w"> </span><span class="n">Exit</span><span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="mi">0</span>
</code></pre></div>
<p><strong>Compressed: -C</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">C</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private1</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">~/</span><span class="n">private2</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="n">ubuntu1</span><span class="mf">@123.12.123</span><span class="o">:~/</span><span class="n">source</span><span class="w"> </span><span class="n">ubuntu2</span><span class="mf">@321.21.321</span><span class="o">:~/</span><span class="n">destination</span>
</code></pre></div>
<p>This option compresses transferring files only in the network, such that when they are received by the destination host, they would return to their original size. Thereby, transfer speed increases while large files are being transferred.</p>
<p><strong>Preserves: -p</strong></p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">scp</span><span class="w"> </span><span class="o">-</span><span class="n">p</span><span class="w"> </span><span class="o">~/</span><span class="n">newfile</span><span class="w"> </span><span class="n">ubuntu</span><span class="mf">@123.12.123</span><span class="o">:~/</span>
</code></pre></div>
<p>This option preserves modification times, access times, and modes from the original file.</p>
<p>If you want to explore more such parameters? Please refer SCP manual page with command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>man<span class="w"> </span>scp
</code></pre></div>
<h2>Conclusion</h2>
<p>SCP is a remote file copy program, that's suit perfectly in cases when you have to transfer data in a single pass. However, FTP is more recommended to transfer multiple files/directories, due to its better session-based control over the connection.</p>
<p>Thanks for reading! And in case of a query, please feel free to write in the comments section below.</p>Hands-on Guide on GPG Keys2018-03-05T22:35:00+05:302018-03-05T22:35:00+05:30GeekyShacklebolttag:thegeekyway.com,2018-03-05:/hands-on-guide-on-gpg-keys/<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/GPG_guide.png"></p>
</div>
<p>GPG or GNU Privacy Guard is a complete and free implementation of the OpenPGP standard as defined by <a href="https://www.ietf.org/rfc/rfc4880.txt">RFC4880</a> (also known as PGP). It is a tool that provides encryption and signing services, using which, you can encrypt or sign your files, messages, emails and can even sign your github …</p><div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/03/GPG_guide.png"></p>
</div>
<p>GPG or GNU Privacy Guard is a complete and free implementation of the OpenPGP standard as defined by <a href="https://www.ietf.org/rfc/rfc4880.txt">RFC4880</a> (also known as PGP). It is a tool that provides encryption and signing services, using which, you can encrypt or sign your files, messages, emails and can even sign your github commits. In this guide, let's get a walk through GPG keys and its use cases.<!--more--></p>
<p>GPG is basically an implementation of a cryptosystem that you can use to exchange encrypted or signed messages and it is based on <a href="https://en.wikipedia.org/wiki/Public-key_cryptography">asymmetric cryptography</a>. It fulfills the <a href="http://whatis.techtarget.com/definition/Confidentiality-integrity-and-availability-CIA">CIA of security</a> where <strong>"C"</strong> stands for "Confidentiality", <strong>"I"</strong> stands for "Integrity" and <strong>"A"</strong> stands for "Availability".</p>
<h2>Before Generating a Key Pair</h2>
<p>In this tutorial, I am using <strong>"gpg2"</strong>, but you may go with <strong>"gpg"</strong> as well, because most of the commands are same for both. However, a comparison between both the versions is available at the end of this post.</p>
<p>To generate a key pair, first make sure you can run the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--version
gpg<span class="w"> </span><span class="o">(</span>GnuPG<span class="o">)</span><span class="w"> </span><span class="m">2</span>.1.11
libgcrypt<span class="w"> </span><span class="m">1</span>.6.5
Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2016</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
License<span class="w"> </span>GPLv3+:<span class="w"> </span>GNU<span class="w"> </span>GPL<span class="w"> </span>version<span class="w"> </span><span class="m">3</span><span class="w"> </span>or<span class="w"> </span>later<span class="w"> </span><http://gnu.org/licenses/gpl.html>
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Home:<span class="w"> </span>~/.gnupg
Supported<span class="w"> </span>algorithms:
Pubkey:<span class="w"> </span>RSA,<span class="w"> </span>ELG,<span class="w"> </span>DSA,<span class="w"> </span>ECDH,<span class="w"> </span>ECDSA,<span class="w"> </span>EDDSA
Cipher:<span class="w"> </span>IDEA,<span class="w"> </span>3DES,<span class="w"> </span>CAST5,<span class="w"> </span>BLOWFISH,<span class="w"> </span>AES,<span class="w"> </span>AES192,<span class="w"> </span>AES256,<span class="w"> </span>TWOFISH,
<span class="w"> </span>CAMELLIA128,<span class="w"> </span>CAMELLIA192,<span class="w"> </span>CAMELLIA256
Hash:<span class="w"> </span>SHA1,<span class="w"> </span>RIPEMD160,<span class="w"> </span>SHA256,<span class="w"> </span>SHA384,<span class="w"> </span>SHA512,<span class="w"> </span>SHA224
Compression:<span class="w"> </span>Uncompressed,<span class="w"> </span>ZIP,<span class="w"> </span>ZLIB,<span class="w"> </span>BZIP2
</code></pre></div>
<p>It gives the version number of <strong>gpg</strong> like <strong>2.1.11</strong>. If not, then you need to install it, the package is <strong>gnupg</strong> (for gpg) or <strong>gnupg2</strong> (for gpg2).</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>sudo<span class="w"> </span>apt-get<span class="w"> </span>update
$<span class="w"> </span>sudo<span class="w"> </span>apt-get<span class="w"> </span>install<span class="w"> </span>gnupg2
</code></pre></div>
<h2>Generate a Key Pair</h2>
<p>Simply run the following command to generate a new key pair.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--full-gen-key
</code></pre></div>
<p>You may try this command with <strong>"sudo"</strong> if required.</p>
<p><strong>Note:- Either use "gpg", "gpg2", "sudo gpg" or "sudo gpg2". But whatever you use, keep that same.</strong> That means, if <strong>"sudo gpg2" </strong>worked for you, then keep it same for all the upcoming commands.</p>
<p>This command will ask you the following questions.</p>
<ul>
<li><strong><em>What kind of key do you want (RSA, DSA, etc)?</em></strong><br>
You should go with default selection.</li>
<li><strong><em>Key size?</em></strong><br>
The longer, the better.</li>
<li><strong><em>How long should the key be valid?</em></strong><br>
It is good to give here some value e.i <strong>"2y"</strong> (2 years) as it is suggested to all people that start with PGP to use an expiring key. Because at some point, you are likely to lose access to the private key, and then you can't revoke the key so that other people stop using it. But if it expires, everyone will stop using it at the expiration date.</li>
<li><strong><em>Your real name?</em></strong></li>
<li><strong><em>Your email address?</em></strong></li>
<li><strong><em>Any comment?</em></strong><br>
For now, you may leave the comment empty. But comments are used to distinctly identify a key, in case you have multiple GPG keys.</li>
<li><strong><em>Final confirmation (y/n)?</em></strong></li>
<li><strong><em>Your passphrase</em>?</strong><br>
Enter a strong passphrase twice and proceed ahead.</li>
</ul>
<p>At this point, <strong>GPG</strong> will generate the keys using entropy. <em>Entropy</em> describes the amount of unpredictability and nondeterminism that exists in a system. This entropy is needed to generate a secure set of keys. This process may take some time depending on how active your system is, and the key size you selected. The terminal may look like this.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--full-gen-key<span class="w"> </span>
gpg<span class="w"> </span><span class="o">(</span>GnuPG<span class="o">)</span><span class="w"> </span><span class="m">2</span>.1.11<span class="p">;</span><span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2016</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Please<span class="w"> </span><span class="k">select</span><span class="w"> </span>what<span class="w"> </span>kind<span class="w"> </span>of<span class="w"> </span>key<span class="w"> </span>you<span class="w"> </span>want:
<span class="o">(</span><span class="m">1</span><span class="o">)</span><span class="w"> </span>RSA<span class="w"> </span>and<span class="w"> </span>RSA<span class="w"> </span><span class="o">(</span>default<span class="o">)</span>
<span class="o">(</span><span class="m">2</span><span class="o">)</span><span class="w"> </span>DSA<span class="w"> </span>and<span class="w"> </span>Elgamal
<span class="o">(</span><span class="m">3</span><span class="o">)</span><span class="w"> </span>DSA<span class="w"> </span><span class="o">(</span>sign<span class="w"> </span>only<span class="o">)</span>
<span class="o">(</span><span class="m">4</span><span class="o">)</span><span class="w"> </span>RSA<span class="w"> </span><span class="o">(</span>sign<span class="w"> </span>only<span class="o">)</span>
Your<span class="w"> </span>selection?<span class="w"> </span><span class="m">1</span>
RSA<span class="w"> </span>keys<span class="w"> </span>may<span class="w"> </span>be<span class="w"> </span>between<span class="w"> </span><span class="m">1024</span><span class="w"> </span>and<span class="w"> </span><span class="m">4096</span><span class="w"> </span>bits<span class="w"> </span>long.
What<span class="w"> </span>keysize<span class="w"> </span><span class="k">do</span><span class="w"> </span>you<span class="w"> </span>want?<span class="w"> </span><span class="o">(</span><span class="m">2048</span><span class="o">)</span><span class="w"> </span><span class="m">4096</span>
Requested<span class="w"> </span>keysize<span class="w"> </span>is<span class="w"> </span><span class="m">4096</span><span class="w"> </span>bits
Please<span class="w"> </span>specify<span class="w"> </span>how<span class="w"> </span>long<span class="w"> </span>the<span class="w"> </span>key<span class="w"> </span>should<span class="w"> </span>be<span class="w"> </span>valid.
<span class="nv">0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>key<span class="w"> </span>does<span class="w"> </span>not<span class="w"> </span>expire
<n><span class="w"> </span><span class="o">=</span><span class="w"> </span>key<span class="w"> </span>expires<span class="w"> </span><span class="k">in</span><span class="w"> </span>n<span class="w"> </span>days
<n>w<span class="w"> </span><span class="o">=</span><span class="w"> </span>key<span class="w"> </span>expires<span class="w"> </span><span class="k">in</span><span class="w"> </span>n<span class="w"> </span>weeks
<n>m<span class="w"> </span><span class="o">=</span><span class="w"> </span>key<span class="w"> </span>expires<span class="w"> </span><span class="k">in</span><span class="w"> </span>n<span class="w"> </span>months
<n>y<span class="w"> </span><span class="o">=</span><span class="w"> </span>key<span class="w"> </span>expires<span class="w"> </span><span class="k">in</span><span class="w"> </span>n<span class="w"> </span>years
Key<span class="w"> </span>is<span class="w"> </span>valid<span class="w"> </span><span class="k">for</span>?<span class="w"> </span><span class="o">(</span><span class="m">0</span><span class="o">)</span><span class="w"> </span>2y
Key<span class="w"> </span>expires<span class="w"> </span>at<span class="w"> </span>Tuesday<span class="w"> </span><span class="m">03</span><span class="w"> </span>March<span class="w"> </span><span class="m">2020</span><span class="w"> </span><span class="m">02</span>:08:15<span class="w"> </span>PM<span class="w"> </span>IST
Is<span class="w"> </span>this<span class="w"> </span>correct?<span class="w"> </span><span class="o">(</span>y/N<span class="o">)</span><span class="w"> </span>y
GnuPG<span class="w"> </span>needs<span class="w"> </span>to<span class="w"> </span>construct<span class="w"> </span>a<span class="w"> </span>user<span class="w"> </span>ID<span class="w"> </span>to<span class="w"> </span>identify<span class="w"> </span>your<span class="w"> </span>key.
Real<span class="w"> </span>name:<span class="w"> </span>Your<span class="w"> </span>Name
Email<span class="w"> </span>address:<span class="w"> </span>your.email@example.com
Comment:<span class="w"> </span>
You<span class="w"> </span>selected<span class="w"> </span>this<span class="w"> </span>USER-ID:
<span class="s2">"Your Name <your.email@example.com>"</span>
Change<span class="w"> </span><span class="o">(</span>N<span class="o">)</span>ame,<span class="w"> </span><span class="o">(</span>C<span class="o">)</span>omment,<span class="w"> </span><span class="o">(</span>E<span class="o">)</span>mail<span class="w"> </span>or<span class="w"> </span><span class="o">(</span>O<span class="o">)</span>kay/<span class="o">(</span>Q<span class="o">)</span>uit?<span class="w"> </span>O
We<span class="w"> </span>need<span class="w"> </span>to<span class="w"> </span>generate<span class="w"> </span>a<span class="w"> </span>lot<span class="w"> </span>of<span class="w"> </span>random<span class="w"> </span>bytes.<span class="w"> </span>It<span class="w"> </span>is<span class="w"> </span>a<span class="w"> </span>good<span class="w"> </span>idea<span class="w"> </span>to<span class="w"> </span>perform
some<span class="w"> </span>other<span class="w"> </span>action<span class="w"> </span><span class="o">(</span><span class="nb">type</span><span class="w"> </span>on<span class="w"> </span>the<span class="w"> </span>keyboard,<span class="w"> </span>move<span class="w"> </span>the<span class="w"> </span>mouse,<span class="w"> </span>utilize<span class="w"> </span>the
disks<span class="o">)</span><span class="w"> </span>during<span class="w"> </span>the<span class="w"> </span>prime<span class="w"> </span>generation<span class="p">;</span><span class="w"> </span>this<span class="w"> </span>gives<span class="w"> </span>the<span class="w"> </span>random<span class="w"> </span>number
generator<span class="w"> </span>a<span class="w"> </span>better<span class="w"> </span>chance<span class="w"> </span>to<span class="w"> </span>gain<span class="w"> </span>enough<span class="w"> </span>entropy.
</code></pre></div>
<h2>Generate an Authentication Subkey</h2>
<p>An authentication subkey is used if you want to use your GPG key for SSH authentication. To generate this, just follow these steps:</p>
<ul>
<li><strong>\$ gpg2 --expert --edit-key your_key_id</strong></li>
<li>Enter <strong>addkey</strong> (to select the operation)</li>
<li>Select the type of key (e.i select "<strong>8</strong>" for RSA)</li>
<li>Enter <strong>'S'</strong> to toggle signing to OFF.</li>
<li>Enter <strong>'E'</strong> to toggle encrypting to OFF.</li>
<li>Enter <strong>'A'</strong> to toggle authentication to ON.</li>
<li>Enter <strong>'Q'</strong> to quit.</li>
<li>Enter key size. (e.i <strong>4096</strong>)</li>
<li>Enter validity period. (e.i <strong>"2y"</strong>)</li>
<li>Confirm your entries.</li>
<li>Confirm creation.</li>
<li>Enter Passphrase twice.</li>
</ul>
<p>After some time your authentication subkey would be generated. Output of the process is shown below.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span><span class="w"> </span><span class="n">gpg2</span><span class="w"> </span><span class="o">--</span><span class="n">expert</span><span class="w"> </span><span class="o">--</span><span class="n">edit</span><span class="o">-</span><span class="k">key</span><span class="w"> </span><span class="mi">123</span><span class="n">EE456</span>
<span class="n">gpg</span><span class="w"> </span><span class="p">(</span><span class="n">GnuPG</span><span class="p">)</span><span class="w"> </span><span class="mf">2.1.11</span><span class="p">;</span><span class="w"> </span><span class="n">Copyright</span><span class="w"> </span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="w"> </span><span class="mi">2016</span><span class="w"> </span><span class="k">Free</span><span class="w"> </span><span class="n">Software</span><span class="w"> </span><span class="n">Foundation</span><span class="p">,</span><span class="w"> </span><span class="n">Inc</span><span class="p">.</span>
<span class="n">This</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="k">free</span><span class="w"> </span><span class="nl">software</span><span class="p">:</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="k">are</span><span class="w"> </span><span class="k">free</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">change</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">redistribute</span><span class="w"> </span><span class="n">it</span><span class="p">.</span>
<span class="n">There</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="k">NO</span><span class="w"> </span><span class="n">WARRANTY</span><span class="p">,</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">extent</span><span class="w"> </span><span class="n">permitted</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">law</span><span class="p">.</span>
<span class="n">Secret</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">available</span><span class="p">.</span>
<span class="n">sec</span><span class="w"> </span><span class="n">rsa4096</span><span class="o">/</span><span class="mi">123</span><span class="n">EE456</span>
<span class="w"> </span><span class="nl">created</span><span class="p">:</span><span class="w"> </span><span class="mi">2017</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">03</span><span class="w"> </span><span class="nl">expires</span><span class="p">:</span><span class="w"> </span><span class="mi">2019</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">02</span><span class="w"> </span><span class="k">usage</span><span class="err">:</span><span class="w"> </span><span class="n">SC</span><span class="w"> </span>
<span class="w"> </span><span class="nl">trust</span><span class="p">:</span><span class="w"> </span><span class="n">ultimate</span><span class="w"> </span><span class="nl">validity</span><span class="p">:</span><span class="w"> </span><span class="n">ultimate</span>
<span class="n">ssb</span><span class="w"> </span><span class="n">rsa4096</span><span class="o">/</span><span class="mi">7891</span><span class="n">EEFF</span>
<span class="w"> </span><span class="nl">created</span><span class="p">:</span><span class="w"> </span><span class="mi">2017</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">03</span><span class="w"> </span><span class="nl">expires</span><span class="p">:</span><span class="w"> </span><span class="mi">2019</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">02</span><span class="w"> </span><span class="k">usage</span><span class="err">:</span><span class="w"> </span><span class="n">E</span><span class="w"> </span>
<span class="o">[</span><span class="n">ultimate</span><span class="o">]</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="w"> </span><span class="nc">Real</span><span class="w"> </span><span class="n">Name</span><span class="w"> </span><span class="n">your</span><span class="p">.</span><span class="n">email</span><span class="nv">@gmail</span><span class="p">.</span><span class="n">com</span>
<span class="n">gpg</span><span class="o">></span><span class="w"> </span><span class="n">addkey</span>
<span class="n">Please</span><span class="w"> </span><span class="k">select</span><span class="w"> </span><span class="n">what</span><span class="w"> </span><span class="n">kind</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="nl">want</span><span class="p">:</span>
<span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="n">DSA</span><span class="w"> </span><span class="p">(</span><span class="nf">sign</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="p">(</span><span class="nf">sign</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="n">Elgamal</span><span class="w"> </span><span class="p">(</span><span class="n">encrypt</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="p">(</span><span class="n">encrypt</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="n">DSA</span><span class="w"> </span><span class="p">(</span><span class="k">set</span><span class="w"> </span><span class="n">your</span><span class="w"> </span><span class="n">own</span><span class="w"> </span><span class="n">capabilities</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">8</span><span class="p">)</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="p">(</span><span class="k">set</span><span class="w"> </span><span class="n">your</span><span class="w"> </span><span class="n">own</span><span class="w"> </span><span class="n">capabilities</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="n">ECC</span><span class="w"> </span><span class="p">(</span><span class="nf">sign</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">11</span><span class="p">)</span><span class="w"> </span><span class="n">ECC</span><span class="w"> </span><span class="p">(</span><span class="k">set</span><span class="w"> </span><span class="n">your</span><span class="w"> </span><span class="n">own</span><span class="w"> </span><span class="n">capabilities</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="w"> </span><span class="n">ECC</span><span class="w"> </span><span class="p">(</span><span class="n">encrypt</span><span class="w"> </span><span class="k">only</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="mi">13</span><span class="p">)</span><span class="w"> </span><span class="n">Existing</span><span class="w"> </span><span class="k">key</span>
<span class="n">Your</span><span class="w"> </span><span class="n">selection</span><span class="vm">?</span><span class="w"> </span><span class="mi">8</span>
<span class="n">Possible</span><span class="w"> </span><span class="n">actions</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="k">key</span><span class="err">:</span><span class="w"> </span><span class="nf">Sign</span><span class="w"> </span><span class="n">Encrypt</span><span class="w"> </span><span class="n">Authenticate</span><span class="w"> </span>
<span class="k">Current</span><span class="w"> </span><span class="n">allowed</span><span class="w"> </span><span class="nl">actions</span><span class="p">:</span><span class="w"> </span><span class="nf">Sign</span><span class="w"> </span><span class="n">Encrypt</span>
<span class="w"> </span><span class="p">(</span><span class="n">S</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="nf">sign</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">encrypt</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">authenticate</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">Q</span><span class="p">)</span><span class="w"> </span><span class="n">Finished</span>
<span class="n">Your</span><span class="w"> </span><span class="n">selection</span><span class="vm">?</span><span class="w"> </span><span class="n">S</span>
<span class="n">Possible</span><span class="w"> </span><span class="n">actions</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="k">key</span><span class="err">:</span><span class="w"> </span><span class="nf">Sign</span><span class="w"> </span><span class="n">Encrypt</span><span class="w"> </span><span class="n">Authenticate</span><span class="w"> </span>
<span class="k">Current</span><span class="w"> </span><span class="n">allowed</span><span class="w"> </span><span class="nl">actions</span><span class="p">:</span><span class="w"> </span><span class="n">Encrypt</span>
<span class="p">(</span><span class="n">S</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="nf">sign</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">encrypt</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">authenticate</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">Q</span><span class="p">)</span><span class="w"> </span><span class="n">Finished</span>
<span class="n">Your</span><span class="w"> </span><span class="n">selection</span><span class="vm">?</span><span class="w"> </span><span class="n">E</span>
<span class="n">Possible</span><span class="w"> </span><span class="n">actions</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="k">key</span><span class="err">:</span><span class="w"> </span><span class="nf">Sign</span><span class="w"> </span><span class="n">Encrypt</span><span class="w"> </span><span class="n">Authenticate</span><span class="w"> </span>
<span class="k">Current</span><span class="w"> </span><span class="n">allowed</span><span class="w"> </span><span class="nl">actions</span><span class="p">:</span>
<span class="p">(</span><span class="n">S</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="nf">sign</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">encrypt</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">authenticate</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">Q</span><span class="p">)</span><span class="w"> </span><span class="n">Finished</span>
<span class="n">Your</span><span class="w"> </span><span class="n">selection</span><span class="vm">?</span><span class="w"> </span><span class="n">A</span>
<span class="n">Possible</span><span class="w"> </span><span class="n">actions</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">RSA</span><span class="w"> </span><span class="k">key</span><span class="err">:</span><span class="w"> </span><span class="nf">Sign</span><span class="w"> </span><span class="n">Encrypt</span><span class="w"> </span><span class="n">Authenticate</span><span class="w"> </span>
<span class="k">Current</span><span class="w"> </span><span class="n">allowed</span><span class="w"> </span><span class="nl">actions</span><span class="p">:</span><span class="w"> </span><span class="n">Authenticate</span>
<span class="p">(</span><span class="n">S</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="nf">sign</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">encrypt</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="n">Toggle</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">authenticate</span><span class="w"> </span><span class="n">capability</span>
<span class="w"> </span><span class="p">(</span><span class="n">Q</span><span class="p">)</span><span class="w"> </span><span class="n">Finished</span>
<span class="n">Your</span><span class="w"> </span><span class="n">selection</span><span class="vm">?</span><span class="w"> </span><span class="n">Q</span>
<span class="n">RSA</span><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="n">may</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="ow">between</span><span class="w"> </span><span class="mi">1024</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="mi">4096</span><span class="w"> </span><span class="n">bits</span><span class="w"> </span><span class="n">long</span><span class="p">.</span>
<span class="n">What</span><span class="w"> </span><span class="n">keysize</span><span class="w"> </span><span class="n">do</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">want</span><span class="vm">?</span><span class="w"> </span><span class="p">(</span><span class="mi">2048</span><span class="p">)</span><span class="w"> </span><span class="mi">4096</span>
<span class="n">Requested</span><span class="w"> </span><span class="n">keysize</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="mi">4096</span><span class="w"> </span><span class="n">bits</span>
<span class="n">Please</span><span class="w"> </span><span class="n">specify</span><span class="w"> </span><span class="n">how</span><span class="w"> </span><span class="n">long</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">should</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">valid</span><span class="p">.</span>
<span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="n">expire</span>
<span class="w"> </span><span class="o"><</span><span class="n">n</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="n">days</span>
<span class="w"> </span><span class="o"><</span><span class="n">n</span><span class="o">></span><span class="n">w</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="n">weeks</span>
<span class="w"> </span><span class="o"><</span><span class="n">n</span><span class="o">></span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="n">months</span>
<span class="w"> </span><span class="o"><</span><span class="n">n</span><span class="o">></span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="n">years</span>
<span class="k">Key</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">valid</span><span class="w"> </span><span class="k">for</span><span class="vm">?</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="mi">2</span><span class="n">y</span>
<span class="k">Key</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="k">at</span><span class="w"> </span><span class="n">Thursday</span><span class="w"> </span><span class="mi">05</span><span class="w"> </span><span class="n">March</span><span class="w"> </span><span class="mi">2020</span><span class="w"> </span><span class="mi">02</span><span class="err">:</span><span class="mi">30</span><span class="err">:</span><span class="mi">01</span><span class="w"> </span><span class="n">AM</span><span class="w"> </span><span class="n">IST</span>
<span class="k">Is</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">correct</span><span class="vm">?</span><span class="w"> </span><span class="p">(</span><span class="n">y</span><span class="o">/</span><span class="n">N</span><span class="p">)</span><span class="w"> </span><span class="n">y</span>
<span class="n">Really</span><span class="w"> </span><span class="k">create</span><span class="vm">?</span><span class="w"> </span><span class="p">(</span><span class="n">y</span><span class="o">/</span><span class="n">N</span><span class="p">)</span><span class="w"> </span><span class="n">y</span>
<span class="n">We</span><span class="w"> </span><span class="n">need</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">generate</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">lot</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">random</span><span class="w"> </span><span class="n">bytes</span><span class="p">.</span><span class="w"> </span><span class="n">It</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">good</span><span class="w"> </span><span class="n">idea</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">perform</span>
<span class="ow">some</span><span class="w"> </span><span class="n">other</span><span class="w"> </span><span class="k">action</span><span class="w"> </span><span class="p">(</span><span class="n">type</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">keyboard</span><span class="p">,</span><span class="w"> </span><span class="n">move</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">mouse</span><span class="p">,</span><span class="w"> </span><span class="n">utilize</span><span class="w"> </span><span class="n">the</span>
<span class="n">disks</span><span class="p">)</span><span class="w"> </span><span class="n">during</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">prime</span><span class="w"> </span><span class="n">generation</span><span class="p">;</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">gives</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">random</span><span class="w"> </span><span class="n">number</span>
<span class="n">generator</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">better</span><span class="w"> </span><span class="n">chance</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">gain</span><span class="w"> </span><span class="n">enough</span><span class="w"> </span><span class="n">entropy</span><span class="p">.</span>
<span class="n">sec</span><span class="w"> </span><span class="n">rsa4096</span><span class="o">/</span><span class="mi">123</span><span class="n">EE456</span>
<span class="w"> </span><span class="nl">created</span><span class="p">:</span><span class="w"> </span><span class="mi">2017</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">03</span><span class="w"> </span><span class="nl">expires</span><span class="p">:</span><span class="w"> </span><span class="mi">2019</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">02</span><span class="w"> </span><span class="k">usage</span><span class="err">:</span><span class="w"> </span><span class="n">SC</span><span class="w"> </span>
<span class="w"> </span><span class="nl">trust</span><span class="p">:</span><span class="w"> </span><span class="n">ultimate</span><span class="w"> </span><span class="nl">validity</span><span class="p">:</span><span class="w"> </span><span class="n">ultimate</span>
<span class="n">ssb</span><span class="w"> </span><span class="n">rsa4096</span><span class="o">/</span><span class="mi">7891</span><span class="n">EEFF</span>
<span class="w"> </span><span class="nl">created</span><span class="p">:</span><span class="w"> </span><span class="mi">2017</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">03</span><span class="w"> </span><span class="nl">expires</span><span class="p">:</span><span class="w"> </span><span class="mi">2019</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">02</span><span class="w"> </span><span class="k">usage</span><span class="err">:</span><span class="w"> </span><span class="n">E</span><span class="w"> </span>
<span class="n">ssb</span><span class="w"> </span><span class="n">rsa4096</span><span class="o">/</span><span class="mi">6</span><span class="n">A127E44</span>
<span class="w"> </span><span class="nl">created</span><span class="p">:</span><span class="w"> </span><span class="mi">2018</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">05</span><span class="w"> </span><span class="nl">expires</span><span class="p">:</span><span class="w"> </span><span class="mi">2020</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">04</span><span class="w"> </span><span class="k">usage</span><span class="err">:</span><span class="w"> </span><span class="n">A</span>
</code></pre></div>
<p>Last 2 lines of the output show newly created authentication [A] subkey.</p>
<h2>List Keys</h2>
<p>To list all the public GPG keys. Give the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--list-keys
</code></pre></div>
<p>To obtain the key id in LONG (complete) format, use this flag <strong>"--keyid-format LONG".</strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--list-keys<span class="w"> </span>--keyid-format<span class="w"> </span>LONG
</code></pre></div>
<p>To list all the private GPG keys.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--list-secret-keys
</code></pre></div>
<p>All these commands give you the following information about your GPG keys and subkeys.</p>
<ul>
<li>Which algorithm your keys use, e.i <strong>rsa.</strong></li>
<li>Key size, e.i <strong>4096 bits.<br>
</strong></li>
<li>Key IDs, e.i <strong>123AB456</strong> (this is the short key id of 8-bytes, in the long format you get the complete key id of 16-bytes).</li>
<li>Date of creation.</li>
<li>Their usage e.i <p .st><strong>'E'</strong>=encryption, <strong>'S'</strong>=signing, <strong>'C'</strong>=certification, <strong>'A'</strong>=authentication.</p></li>
<li>Their expiry date.</li>
<li>Their trust level, e.i <strong>Ultimate, unknown, etc.</strong></li>
<li>Name and Email associated with them that's also known as <strong>UID</strong> "User ID".</li>
</ul>
<h2>Exporting Keys</h2>
<p>Exporting means, to bring out the keys from GPG database so that we may see or store them in files.</p>
<p>If you want to see your GPG key in ASCII characters, then you need to export it to a file by the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--output<span class="w"> </span>public.key<span class="w"> </span>--armor<span class="w"> </span>--export<span class="w"> </span>your.email@example.com
</code></pre></div>
<p><strong>--output:</strong> specifies the output file.<br>
<strong>--export:</strong> to instruct GPG to export your public key.<br>
<strong>--armor:</strong> to export in ASCII characters.</p>
<p>Similarly, to export a private key directly on the terminal just change one flag.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--armor<span class="w"> </span>--export-secret-keys<span class="w"> </span>your.email@example.com
</code></pre></div>
<p>This will ask for your passphrase. Here you may also add the <strong>--output</strong> flag to put its output in a file as done in the previous command.</p>
<p><strong>Note: Never give/share your private key anywhere. Keep it as secure as you can.</strong> The private key is required to decrypt the data encrypted just for you. So, compromising this key may result in security issues whose severity depends on the data you deal with.</p>
<h2>Fingerprints</h2>
<p>A key can be verified by a number of ways. One such way is to use its fingerprint. The fingerprint is a shorter number (usually expressed as a 40-hexadecimal-digit number) that contains a cryptographically strong hash of the public key.</p>
<p>To see the fingerprint of any GPG key associated with an email. Use this command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--fingerprint<span class="w"> </span>email@example.com
</code></pre></div>
<p>You can cross check this fingerprint with the person to verify if it is trustworthy.</p>
<p>Now when you have seen and understand your GPG keys. It's time to generate a revocation certificate.</p>
<h2>Generating a Revocation Certificate</h2>
<p>It is vital to have revocation certificate to declare your keys invalid in case your private key has been compromised or lost. It should be generated as soon as you generate your key pair, not when you need it. So, let's generate it with the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--output<span class="w"> </span>~/revocation.crt<span class="w"> </span>--gen-revoke<span class="w"> </span>your.email@example.com
Please<span class="w"> </span><span class="k">select</span><span class="w"> </span>the<span class="w"> </span>reason<span class="w"> </span><span class="k">for</span><span class="w"> </span>the<span class="w"> </span>revocation:<span class="w"> </span>
<span class="w"> </span><span class="nv">0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>No<span class="w"> </span>reason<span class="w"> </span>specified
<span class="w"> </span><span class="nv">1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Key<span class="w"> </span>has<span class="w"> </span>been<span class="w"> </span>compromised
<span class="w"> </span><span class="nv">2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Key<span class="w"> </span>is<span class="w"> </span>superseded
<span class="w"> </span><span class="nv">3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Key<span class="w"> </span>is<span class="w"> </span>no<span class="w"> </span>longer<span class="w"> </span>used
<span class="w"> </span><span class="nv">Q</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>Cancel<span class="w"> </span>
<span class="o">(</span>Probably<span class="w"> </span>you<span class="w"> </span>want<span class="w"> </span>to<span class="w"> </span><span class="k">select</span><span class="w"> </span><span class="m">1</span><span class="w"> </span>here<span class="o">)</span><span class="w"> </span>
Your<span class="w"> </span>decision?<span class="w"> </span><span class="m">0</span>
</code></pre></div>
<p>It would ask for your confirmation for generating the certificate and the reason for which you are creating this. It is also suggested that you should have revocation certificates for every possibility listed there for maximum flexibility, but for now, you may go for <strong>"0: No reason specified"</strong>.</p>
<p>Now give a comment (I left it empty) and your passphrase. Soon, your certificate is generated. You should keep this as secure and hidden as you keep your private keys. Here is the output.</p>
<div class="highlight"><pre><span></span><code><span class="nt">Enter</span><span class="w"> </span><span class="nt">an</span><span class="w"> </span><span class="nt">optional</span><span class="w"> </span><span class="nt">description</span><span class="o">;</span><span class="w"> </span><span class="nt">end</span><span class="w"> </span><span class="nt">it</span><span class="w"> </span><span class="nt">with</span><span class="w"> </span><span class="nt">an</span><span class="w"> </span><span class="nt">empty</span><span class="w"> </span><span class="nt">line</span><span class="o">:</span>
<span class="o">></span><span class="w"> </span>
<span class="nt">Reason</span><span class="w"> </span><span class="nt">for</span><span class="w"> </span><span class="nt">revocation</span><span class="o">:</span><span class="w"> </span><span class="nt">No</span><span class="w"> </span><span class="nt">reason</span><span class="w"> </span><span class="nt">specified</span>
<span class="o">(</span><span class="nt">No</span><span class="w"> </span><span class="nt">description</span><span class="w"> </span><span class="nt">given</span><span class="o">)</span>
<span class="nt">Is</span><span class="w"> </span><span class="nt">this</span><span class="w"> </span><span class="nt">okay</span><span class="o">?</span><span class="w"> </span><span class="o">(</span><span class="nt">y</span><span class="o">/</span><span class="nt">N</span><span class="o">)</span><span class="w"> </span><span class="nt">y</span>
<span class="nt">ASCII</span><span class="w"> </span><span class="nt">armored</span><span class="w"> </span><span class="nt">output</span><span class="w"> </span><span class="nt">forced</span><span class="o">.</span>
<span class="nt">Revocation</span><span class="w"> </span><span class="nt">certificate</span><span class="w"> </span><span class="nt">created</span><span class="o">.</span>
<span class="nt">Please</span><span class="w"> </span><span class="nt">move</span><span class="w"> </span><span class="nt">it</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">a</span><span class="w"> </span><span class="nt">medium</span><span class="w"> </span><span class="nt">which</span><span class="w"> </span><span class="nt">you</span><span class="w"> </span><span class="nt">can</span><span class="w"> </span><span class="nt">hide</span><span class="w"> </span><span class="nt">away</span><span class="o">;</span><span class="w"> </span><span class="nt">if</span><span class="w"> </span><span class="nt">Mallory</span><span class="w"> </span><span class="nt">gets</span>
<span class="nt">access</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">this</span><span class="w"> </span><span class="nt">certificate</span><span class="w"> </span><span class="nt">he</span><span class="w"> </span><span class="nt">can</span><span class="w"> </span><span class="nt">use</span><span class="w"> </span><span class="nt">it</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">make</span><span class="w"> </span><span class="nt">your</span><span class="w"> </span><span class="nt">key</span><span class="w"> </span><span class="nt">unusable</span><span class="o">.</span>
<span class="nt">It</span><span class="w"> </span><span class="nt">is</span><span class="w"> </span><span class="nt">smart</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">print</span><span class="w"> </span><span class="nt">this</span><span class="w"> </span><span class="nt">certificate</span><span class="w"> </span><span class="nt">and</span><span class="w"> </span><span class="nt">store</span><span class="w"> </span><span class="nt">it</span><span class="w"> </span><span class="nt">away</span><span class="o">,</span><span class="w"> </span><span class="nt">just</span><span class="w"> </span><span class="nt">in</span><span class="w"> </span><span class="nt">case</span>
<span class="nt">your</span><span class="w"> </span><span class="nt">media</span><span class="w"> </span><span class="nt">become</span><span class="w"> </span><span class="nt">unreadable</span><span class="o">.</span><span class="w"> </span><span class="nt">But</span><span class="w"> </span><span class="nt">have</span><span class="w"> </span><span class="nt">some</span><span class="w"> </span><span class="nt">caution</span><span class="o">:</span><span class="w"> </span><span class="nt">The</span><span class="w"> </span><span class="nt">print</span><span class="w"> </span><span class="nt">system</span><span class="w"> </span><span class="nt">of</span>
<span class="nt">your</span><span class="w"> </span><span class="nt">machine</span><span class="w"> </span><span class="nt">might</span><span class="w"> </span><span class="nt">store</span><span class="w"> </span><span class="nt">the</span><span class="w"> </span><span class="nt">data</span><span class="w"> </span><span class="nt">and</span><span class="w"> </span><span class="nt">make</span><span class="w"> </span><span class="nt">it</span><span class="w"> </span><span class="nt">available</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">others</span><span class="o">!</span>
</code></pre></div>
<p>In order to save this file from unauthorized access, you may immediately put restrictions (if not present by default).</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>chmod<span class="w"> </span><span class="m">600</span><span class="w"> </span>~/revocation.crt
</code></pre></div>
<p>In case you want to revoke your key, then you would have to upload this revocation certificate to a public keyserver (assuming that you have already sent <em>your public key to a keyserver</em>. See below.)</p>
<h2>Sending and Importing Keys</h2>
<h4><strong>Sending:</strong></h4>
<p><strong><em>Why are you using GPG keys in the first place?</em></strong><br>
Because with GPG you can sign messages or other people can encrypt messages to you.</p>
<p>Therefore, to make your key publicly available might be of great help. So that, anyone can take your public key to encrypt messages to you. For this, we can send our public key to a keyserver.</p>
<p><strong><em>Would it be safe to upload your public key on keyservers?</em></strong><br>
Absolutely yes, as I have already mentioned before that GPG works on <a href="https://en.wikipedia.org/wiki/Public-key_cryptography">asymmetric cryptography</a>, therefore having your public key to someone doesn't make a threat to your security. Because it is practically impossible to generate a private key from a public key.</p>
<p>There are multiple keyservers available to hold public keys like, <strong><em><a href="https://keys.fedoraproject.org/">keys.fedoraproject.org</a>, <a href="http://keyserver.ubuntu.com/">keyserver.ubuntu.com, etc</a>.</em></strong><strong> </strong>and they regularly synchronize among themselves. So if you send your key on <em>keyserver.ubuntu.com, then</em> after some time, it would also be available at <em>keys.fedoraproject.org.</em></p>
<p>To send your key to the keyserver use the following command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--send-keys<span class="w"> </span>--keyserver<span class="w"> </span>keyserver_url<span class="w"> </span>key_id
</code></pre></div>
<p>If you don't want to upload your key to a keyserver (in case your communication is limited to a group of people) then you can also send your public key to them through email.</p>
<h4><strong>Importing:</strong></h4>
<p>Importing means to include the key in your GPG database, so that you may use them in future. For example, if you wanna send your friends an encrypted message, which can be done with their public keys. Therefore, first, you would need to import their keys into your GPG system and then only you may use them for encryption.</p>
<p>You can import the key of someone from a file, with the following command. For this, you must have their public key in a file say <strong>"their_gpg.key"</strong>.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span> <span class="n">gpg2</span> <span class="o">--</span><span class="kn">import</span> <span class="nn">their_gpg.key</span>
</code></pre></div>
<p>Now when you would list-keys with "<strong>gpg2 --list-keys</strong>", this imported key will also be listed.</p>
<p>But if the person's key is available at a keyserver, then you can also directly pull their keys from there. Because keyservers distribute public keys to anyone who requests them. Once you have sent your key to a keyserver, others can request your key using the command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--keyserver<span class="w"> </span>keyserver_url<span class="w"> </span>--recv-keys<span class="w"> </span>key_id
</code></pre></div>
<p>To refresh all your keys from a keyserver, to obtain new signatures, new UIDs and new key revocations, use this.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--refresh-keys
</code></pre></div>
<p><kbd>And to pull from specific servers, use this.</kbd></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--keyserver<span class="w"> </span>keyserver_url<span class="w"> </span>--refresh-keys
</code></pre></div>
<p>You should do this regularly. But you may receive error messages if any of your keys cannot be found on the key server.</p>
<h2>Signing Keys</h2>
<p>A major benefit of GPG is that you can sign a file to prove that it is genuinely coming form you. Because signs are created using private keys and only the person who owns the GPG keys, have its private key (if not compromised or lost).</p>
<p>Your key is already signed by you when it was created. That you may verify with.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--list-sig<span class="w"> </span>your.email@example.com
</code></pre></div>
<p>In the output, the line after <strong>"sig"</strong> shows the <strong>UID</strong> of those people who have signed your key.</p>
<p>You can also sign keys of other people (assuming that you have already imported the key of that person). Signing a key means you verify that you trust the person, who they claim to be. If you sign a person's key then sending the signed key back to him/her is a good idea, otherwise, they won't be able to take advantage of your sign. For this, you have 2 methods.</p>
<p><strong>Method 1:</strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--sign-key<span class="w"> </span>person.email@example.com
$<span class="w"> </span>gpg2<span class="w"> </span>--output<span class="w"> </span>~/signed.key<span class="w"> </span>--sign-key<span class="w"> </span>person.email@example.com
</code></pre></div>
<p>Then you can send this "<strong>signed.key</strong>" file to the person via mail.</p>
<p>Once that person receives your signed key, then s//he can update their own GPG database to include your signature.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span> <span class="n">gpg2</span> <span class="o">--</span><span class="kn">import</span> <span class="nn">signed.key</span>
</code></pre></div>
<p><strong>Method 2:</strong></p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--sign-key<span class="w"> </span>person.email@example.com
$<span class="w"> </span>gpg2<span class="w"> </span>--keyserver<span class="w"> </span>key_server<span class="w"> </span>--send-key<span class="w"> </span>person_keyid
</code></pre></div>
<p>This will directly upload your sign to the keyserver (assuming that the person has sent his key to a keyserver). So when next time that person would refresh his/her GPG database, your sign would be included.</p>
<p><strong><em>Why should you sign a person's key?</em><em><br>
</em>Because this can help other people decide whether to trust that person too or not. If someone trusts you, and they see that you've signed this person's key, they may be more likely to trust their identity too. There is a concept behind signing a key known as </strong><a href="https://en.wikipedia.org/wiki/Web_of_trust">Web of Trust</a>.** (see below)</p>
<h2>Trust Levels and Validity</h2>
<p>Trust levels is a medium by which we can inform GPG that how much we trust a UID of a person associated with a key.</p>
<p>There are following trust levels available</p>
<ul>
<li><strong>Unknown:</strong> This is the default level that is set on validity and trust.</li>
<li><strong>Undefined:</strong> This is explicitly defining trust level to be unknown. Means you would like to confirm it later on.</li>
<li><strong>Never:</strong> This indicates that you do not trust the UID of that key.</li>
<li><strong>Marginal:</strong> This means you have half trust on the UID of that key.</li>
<li><strong>Full:</strong> This means you fully trust that person.</li>
<li><strong>Ultimate:</strong> There is only one UID that is marked with <strong>Ultimate</strong> trust, and that is of yourself.</li>
</ul>
<p>More detail about trust-levels is available <a href="https://gpgtools.tenderapp.com/kb/faq/what-is-ownertrust-trust-levels-explained">here</a>.</p>
<p>You may check the <strong>trust</strong> and <strong>validity</strong> levels of any <strong>UID</strong> among the keys you have imported, with this command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--edit-key<span class="w"> </span>email@example.com
gpg<span class="w"> </span><span class="o">(</span>GnuPG<span class="o">)</span><span class="w"> </span><span class="m">2</span>.1.11<span class="p">;</span><span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2016</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
pub<span class="w"> </span>rsa2048/4A5N1031
<span class="w"> </span>created:<span class="w"> </span><span class="m">2016</span>-10-10<span class="w"> </span>expires:<span class="w"> </span><span class="m">2018</span>-10-10<span class="w"> </span>usage:<span class="w"> </span>SC<span class="w"> </span>
<span class="w"> </span>trust:<span class="w"> </span>unknown<span class="w"> </span>validity:<span class="w"> </span>unknown
sub<span class="w"> </span>rsa2048/73E6DB92
<span class="w"> </span>created:<span class="w"> </span><span class="m">2016</span>-10-10<span class="w"> </span>expires:<span class="w"> </span><span class="m">2018</span>-10-10<span class="w"> </span>usage:<span class="w"> </span>E<span class="w"> </span>
<span class="o">[</span><span class="w"> </span>unknown<span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="o">(</span><span class="m">1</span><span class="o">)</span>.<span class="w"> </span>Person<span class="w"> </span>Name<span class="w"> </span>email@example.com
gpg:<span class="w"> </span>Q
</code></pre></div>
<p>To modify <strong>trust</strong> of the key, enter "<strong>trust"</strong> instead of <strong>"Q"</strong> (in the last line of output). And you'll get a prompt like this.</p>
<div class="highlight"><pre><span></span><code><span class="nv">Please</span><span class="w"> </span><span class="nv">decide</span><span class="w"> </span><span class="nv">how</span><span class="w"> </span><span class="nv">far</span><span class="w"> </span><span class="nv">you</span><span class="w"> </span><span class="nv">trust</span><span class="w"> </span><span class="nv">this</span><span class="w"> </span><span class="nv">user</span><span class="w"> </span><span class="nv">to</span><span class="w"> </span><span class="nv">correctly</span><span class="w"> </span><span class="nv">verify</span><span class="w"> </span><span class="nv">other</span><span class="w"> </span><span class="nv">users</span><span class="err">' keys</span>
<span class="err">(by looking at passports, checking fingerprints from different sources, etc.)</span>
<span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I</span><span class="w"> </span><span class="nv">don</span><span class="s1">'t know or won'</span><span class="nv">t</span><span class="w"> </span><span class="nv">say</span>
<span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="nv">NOT</span><span class="w"> </span><span class="nv">trust</span>
<span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I</span><span class="w"> </span><span class="nv">trust</span><span class="w"> </span><span class="nv">marginally</span>
<span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I</span><span class="w"> </span><span class="nv">trust</span><span class="w"> </span><span class="nv">fully</span>
<span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I</span><span class="w"> </span><span class="nv">trust</span><span class="w"> </span><span class="nv">ultimately</span>
<span class="w"> </span><span class="nv">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">back</span><span class="w"> </span><span class="nv">to</span><span class="w"> </span><span class="nv">the</span><span class="w"> </span><span class="nv">main</span><span class="w"> </span><span class="nv">menu</span>
<span class="nv">Your</span><span class="w"> </span><span class="nv">decision</span>?<span class="w"> </span><span class="nv">m</span>
</code></pre></div>
<p>Enter your decision, and that trust level would be set for that key.</p>
<p><strong>Why do we have trust levels?</strong></p>
<p>To understand this, first, you must understand <strong>validity.</strong> When you sign a key, you say that the key is <strong>valid</strong>. But you sign a key if and only if you are sure, that the key belongs the person whose UID is attached to the key. This verification could be done by personally contacting that person and cross-checking the fingerprint of the key.</p>
<p>Isn't it a tedious task to every time contacting people just to confirm the fingerprint of their GPG key? Especially, in the case when you haven't met that person before. Therefore, we have something called <strong>"<a href="https://en.wikipedia.org/wiki/Web_of_trust">Web of Trust</a>".</strong></p>
<p>Suppose you trust "<strong>Mike</strong>" and <strong>"John"</strong> that they sign GPG keys of other people only after completely verifying a UID. Then giving following <strong>Trust-levels</strong> to them would affect your key as follows.</p>
<ul>
<li><strong>Unknown/Undefined</strong> to <strong>Mike:</strong> Keys signed by Mike won't be valid for you.</li>
<li><strong>Never</strong> to <strong>Mike:</strong> Same as above, but here you have explicitly given that you don't trust <strong>Mike.</strong></li>
<li><strong>Marginal</strong> to <strong>Mike:</strong> Keys signed by Mike will be marginally valid for you.</li>
<li><strong>Marginal</strong> to <strong>Mike</strong> and <strong>John:</strong> Keys signed by both Mike and John will be valid for you (if the marginal limit is set to 2, otherwise if the marginal limit is set to <strong>"n"</strong> then a key becomes valid if it is signed by <strong>"n"</strong> UIDs).</li>
<li><strong>Full</strong> to <strong>Mike:</strong> Keys signed by Mike will be valid for you as well.</li>
</ul>
<p>I recommend you to read <a href="https://www.gnupg.org/gph/en/manual/x334.html">this document</a> to get a better understanding of trust-levels and validity.</p>
<h2>Encryption-Decryption with GPG</h2>
<h4>Encrypting a file</h4>
<p>To encrypt and sign a file for a recipient you must have his/her public key imported.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>-r<span class="w"> </span>email.of.recipient@example.com<span class="w"> </span>-r<span class="w"> </span>your.email@example.com<span class="w"> </span>--encrypt<span class="w"> </span>--sign<span class="w"> </span>--armor<span class="w"> </span>file_name
</code></pre></div>
<ul>
<li><strong>gpg2:</strong> command</li>
<li><strong>-r:</strong> to give email_address to possible recipients (who can decrypt this file).</li>
<li><strong>-r:</strong> adding yourself as one of the recipients so that you can also decrypt the file otherwise after encrypting, even you won't be able to decrypt it.</li>
<li><strong>--encrypt:</strong> instructing to encrypt.</li>
<li><strong>--sign:</strong> instructing to sign the file as well.</li>
<li><strong>--armor:</strong> to encrypt in ASCII characters.<strong> </strong></li>
<li><strong>file_name:</strong> complete path of the file, If you omit this part, then after giving this command, the terminal will wait for you to enter the text. When you are done with entering your message, press <strong>CTRL + d</strong> (Linux). It would generate the encrypted form of your message, which you may copy to clipboard and paste in any file.</li>
</ul>
<p>If you haven't set any trust level for that recipient, then you are likely to get here a confirmation prompt for using the recipient's public key for encryption. And finally, it would prompt you for your passphrase.</p>
<p>Soon an encrypted file would be created with <strong>.asc</strong> extension. But still, that unencrypted file is available. You are yourself responsible to delete the original file.</p>
<h4>Decrypting a file</h4>
<p>To decrypt a file that was encrypted by your public key just use.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>file_name.asc
</code></pre></div>
<p>It would ask for your passphrase and a decrypted file would be available to you without "<strong>.asc"</strong> extension.</p>
<p>If the encrypted file was also <strong>signed</strong> by the sender, then the output of above command would also give information about signature like <strong><em>date_of_signature</em></strong> and <em><strong>status_of_signature</strong> (good or bad)</em> otherwise not.</p>
<h2>Signed messages</h2>
<h4><strong>Clearsign:</strong></h4>
<p>Using GPG you may send signed files without encryption. Create a file say "<strong>newfile</strong>", add some message to it and sign it.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Hello World"</span><span class="w"> </span>>><span class="w"> </span>newfile
$<span class="w"> </span>gpg2<span class="w"> </span>--clearsign<span class="w"> </span>newfile
</code></pre></div>
<p><strong>--clearsign:</strong> wraps the message without encryption with your <strong>armored</strong> signature, so that any non-GPG user can read the message and any GPG user can verify it (if needed).</p>
<p>Soon "<strong>newfile.asc</strong>" would be created and anyone can verify, that it is signed by you.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--verify<span class="w"> </span>newfile
</code></pre></div>
<p>But it can give you warning of not having a detached signature.</p>
<div class="highlight"><pre><span></span><code><span class="n">gpg</span><span class="o">:</span><span class="w"> </span><span class="n">WARNING</span><span class="o">:</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">detached</span><span class="w"> </span><span class="n">signature</span><span class="o">;</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="s1">'newfile'</span><span class="w"> </span><span class="n">was</span><span class="w"> </span><span class="n">NOT</span><span class="w"> </span><span class="n">verified</span><span class="o">!</span>
</code></pre></div>
<p>(if yes, see below)</p>
<h4><strong>Detached signature:</strong></h4>
<p>Till now newfile<strong>.</strong>asc consisted both unencrypted message and signature in the same file. To solve this issue, we can sign a file with a detached signature so that the file and the signature can be kept separately.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--detach-sign<span class="w"> </span>newfile
</code></pre></div>
<p>Now a new file with name "<strong>newfile.sig</strong>" is created which contains just your signature. You may verify that with.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>gpg2<span class="w"> </span>--verify<span class="w"> </span>newfile.sig
</code></pre></div>
<p>This will return verification statement and signature's information, together with the attached file's name. Now, if you change the file's (newfile) content and again check for verification, then it would fail. Because the new content wasn't signed by the signer. This way GPG maintains <strong>"Integrity"</strong>.</p>
<p>In case the signature verification failed, then understand that the content of the message has been modified in between.</p>
<h2>Integrated with GPG</h2>
<h4>Thunderbird</h4>
<p>You can use an email client like Thunderbird to automatically encrypt and sign your emails. For this, you would need to install an addon "<strong>Enigmail</strong>" in your Thunderbird and configure it. To configure Thunderbird proceed as follows.</p>
<ul>
<li>Open <strong>Thunderbird.</strong></li>
<li>From <strong>Menu Bar -> Tools -> Add-ons</strong>, install <strong>Enigmail.</strong></li>
<li>From <strong>Menu Bar -> Enigmail -> Setup Wizard</strong>, configure it.</li>
<li>Choose <strong>Standard Configuration</strong> then <strong>Next</strong>.</li>
<li>Now Select your GPG key and Complete the wizard.</li>
</ul>
<p>Now whenever you would send any mail to the email ID that you have already imported in GPG, your Thunderbird will itself encrypt it. You may explore more configuration options at <strong>Menu Bar -> Enigmail.</strong></p>
<h4>Github and Git</h4>
<p>You can add your GPG key to your <strong>GitHub profile</strong> and can configure your <strong>Git</strong> to sign your commits. Follow these steps or refer <a href="https://help.github.com/articles/signing-commits-with-gpg/">this</a>.</p>
<p><strong>To add your key to Github:</strong></p>
<ul>
<li>Login to your <strong>Github</strong> account.</li>
<li>At upper-right corner, click your profile photo and select <strong>Settings.</strong></li>
<li>From left panel select <strong>SSH and GPG keys</strong>.</li>
<li>Click on <strong>New GPG key</strong> button.</li>
<li>Paste your <strong>GPG public key</strong> there.<br>
You can export your GPG public key to your terminal with<br>
<strong>\$ gpg2 --armor --export<br>
</strong>Select your complete GPG public key and <strong>CTRL + SHIFT + C</strong> to copy.</li>
</ul>
<p><strong>To configure git with GPG key:</strong></p>
<ul>
<li><strong>\$ gpg2 --list-keys --keyid-format LONG</strong></li>
<li>Copy your complete key ID.</li>
<li><strong>\$ git config --global user.signingkey your_key_id</strong></li>
</ul>
<p>Now to sign your commits you may use <strong>-S</strong> flag in your <strong>git commit</strong> command.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>commit<span class="w"> </span>-S<span class="w"> </span>-m<span class="w"> </span><span class="s2">"commit message"</span>
</code></pre></div>
<p>You will be prompted to enter your passphrase while making commits.</p>
<h2>A Brief Comparison of GPG and GPG2</h2>
<ul>
<li>GPG2 is extended version of GPG. Where changes are mostly in internal levels, thus commands are same.</li>
<li>A major change between GPG and GPG2 is that all the crypto operations have been moved to the GPG agent, so without an agent, GPG2 can't work.</li>
<li><strong>man gpg2 </strong>command gives the following statement <em>"In contrast to the standalone version gpg, which is more suited for server and embedded platforms, this version is commonly installed under the name gpg2 and more targeted to the desktop as it requires several other modules to be installed."</em></li>
<li>An important change came with GPG 2.1, which combines the formerly separated public and private keyrings (pubring.gpg and secring.gpg) into the public keyring.</li>
<li>Explore more about what's new in GPG2 <a href="https://www.gnupg.org/faq/whats-new-in-2.1.html">here</a>.</li>
</ul>
<h2>Conclusion</h2>
<p>GPG is a recommended tool if you don't want to compromise security in your communication with people over an unsecured network like our Internet.</p>
<p>Regardless whether you are sending a sensitive information or a friendly message, using GPG keys is extremely helpful to verify that the author of the message is genuine. Thanks for reading, and in case of a query or suggestions to improve this post, please feel free to write in the comments section below.</p>Blue-Green Deployment2018-02-15T17:11:00+05:302018-02-15T17:11:00+05:30GeekyShacklebolttag:thegeekyway.com,2018-02-15:/blue-green-deployment/<p>A few glimpse of past reminds us that application deployment on clouds was really a cumbersome task. Not only because the rollback was a pretty much complex operation to carry out, but also, because of huge outage or downtime, which inevitably used to disappoint users. But as we can see …</p><p>A few glimpse of past reminds us that application deployment on clouds was really a cumbersome task. Not only because the rollback was a pretty much complex operation to carry out, but also, because of huge outage or downtime, which inevitably used to disappoint users. But as we can see nowadays, there are some simpler and more efficient ways to perform the same. We are going to discuss one such technique, that is called "Blue-Green Deployment".<!--more--></p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/02/Blue-green-deployment.png" title="Blue-Green Deployment"></p>
</div>
<p>Before we jump on to understand Blue-Green Deployment, let's first discuss some relevant terms in brief.</p>
<h3>Deployment:</h3>
<p>This term is often get confused with the word "release" due to their versatile usage. Thus, their definition may vary with the context of talk. But here, "release" is just a version number of an application, that always doesn't need to deploy. However, "deployment" is the process of installation and configuration of an application on a targeted hardware.</p>
<h3>Downtime or Outage:</h3>
<p>This is the period of time in which the system fails or it remains unavailable to reply to client's requests. This is either a manifestation of the system crash or during routine maintenance.</p>
<h3>Load Balancer:</h3>
<p>This can be a hardware device or a software component. It's main feature is to spread the traffic over the server pool, such that, no server is overly burdened while others remain idle. This can also be used to divert network traffic from one server pool to another. We'll see its usage in blue-green deployment. <a href="http://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html">Elastic load balancing</a> is also available to handle multiple elastic clouds.</p>
<p>Let's understand about Blue-Green Deployment technique now.</p>
<h2>Blue-Green Deployment:</h2>
<p>This is also known as <strong>Red-Black Deployment</strong>. This is a technique used in application deployment which reduces the risk of downtime and also facilitates the rollback (moving to the previous state). Let us see how it exactly happens.</p>
<p><strong>Workflow of blue-green deployment using a load balancer:</strong><br>
For this technique, we need 2 identically configured hardware components or server pools. Out of which:-<br>
- one is termed as <strong>"blue"</strong> that is going to be an <strong>active</strong> environment<br>
- another one is termed as <strong>"green"</strong> that would be an <strong>idle</strong> environment.</p>
<p>Initially, some applications <strong>(e.i app1 ver1, app2 ver1)</strong> are running in our blue environment. As shown in the picture.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/02/Active-blue-env.png" title="Active blue environment"></p>
</div>
<p>Suppose, now we come up with different versions of applications <strong>(i.e. app1 ver2, app2 ver2)</strong>. We want them to deploy and then test these applications. If everything goes right, we'll use these versions in future, but if not, then we would go for the "rollback".</p>
<p>Let's accomplish this using blue-green deployment:</p>
<p>1. Deploy new versions of application in idle environment<br>
2. Here a testing team may test the working of these new versions.<br>
3. Gradually, using load balancer we start diverting the traffic to this green environment. As shown in the picture. With this live testing is also possible.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/02/Active-blue-green-env.png" title="Diverting traffic from blue to green environment"></p>
</div>
<p>4. If now, these versions fail or crash due to any possible reason. We can quickly roll back to the previous versions in the blue environment using the load balancer.<br>
5. If new versions are found to be perfectly interacting with the user's traffic, then this <strong>"green"</strong> environment is kept to be <strong>active</strong> and <strong>"blue"</strong> becomes <strong>idle</strong>.</p>
<div style="text-align:center">
<p><img alt="" src="//thegeekyway.com/uploads/images/2018/02/Active-green-env.png" title="Active green environment"></p>
</div>
<p>After successful blue-green deployment, the idle environment is either used to deploy the next version of the application or is simply destroyed.</p>
<p><em><u></em><em>Some points to ponder:</em><em></u></em></p>
<ul>
<li>Blue-green deployment is widely used nowadays to get minimum or zero downtime. It is used by different cloud service providers like AWS (Amazon Web Services), DigitalOcean, etc.</li>
<li>Using load balancer is not the only way to implement blue-green environments. Different methods are also available like elastic IPs, DNS routing, etc.</li>
</ul>
<h5>Advantages of blue-green deployment:</h5>
<p>1. Fast rollback<br>
2. Reduced downtime<br>
3. Allow testing in a live production environment without affecting 100% user base.</p>
<h5>Disadvantages of blue-green deployment:</h5>
<p>1. Database dependent:<br>
Implementing blue-green deployment become complex with the database, especially if, the database schema is changed in the new version.<br>
2. Increasingly more difficult at scale</p>
<p>If you've any questions about Blue Green Deployment, please let us know in the comments section below.</p>What is Git Blame & it's usecases2018-02-06T18:50:00+05:302018-02-06T18:50:00+05:30storymode7tag:thegeekyway.com,2018-02-06:/what-is-git-blame-and-its-usecases/<div style="text-align:center">
<h3><img alt="Git Logo" src="//thegeekyway.com/uploads/images/2018/02/Git-logo-2.png"></h3>
</div>
<h3>Git Blame</h3>
<p>Have you ever wondered who touched a bunch of lines in this code last time? There have been lot of situations which can leave people baffled for a while to know who actually changed what. Blame labels each line in the code with the details of the last …</p><div style="text-align:center">
<h3><img alt="Git Logo" src="//thegeekyway.com/uploads/images/2018/02/Git-logo-2.png"></h3>
</div>
<h3>Git Blame</h3>
<p>Have you ever wondered who touched a bunch of lines in this code last time? There have been lot of situations which can leave people baffled for a while to know who actually changed what. Blame labels each line in the code with the details of the last revision that took place on that part of the file, including moving the line from one place to other.</p>
<h3>Ideology behind the concept</h3>
<p>When you study a code, a very large one. Like some parts of CPython repo. You will eventually find yourself at a stage where you do not understand what the code does there. Why is it there? Had it not been there would it affect the code in any manner? You need to guess it how it must have been used. But the author of that commit, the one who actually wrote it, must have known what he/she meant to do by introducing that tricky part. And that should be explained in the commit message that the person must have written. Here's when git blame plays it's card (also pickaxe, discussed later), and helps the user to identify who actually did the last revision on that commit. Also, it can easily help to seek an individual who is responsible for the current state of the code and hence can be blamed if the code breaks there.</p>
<p>To check it out, the simplest way is:</p>
<div class="highlight"><pre><span></span><code>git blame <filename>
</code></pre></div>
<p>This will give you result in the format</p>
<div class="highlight"><pre><span></span><code>(Short commit hash) (Author's name Date Time UTC-Time-Offset Line-Number)
</code></pre></div>
<p>The actual line goes in front of these.</p>
<h3>Refactoring Code</h3>
<p>It's the process referring to rearranging different parts of an already existing program to make it more readable. It also includes adding or modifying a part without changing the ultimate objective it was created to serve. That means in code refactoring you can modify a piece of code to make it more readable, given that you do not change what it was supposed to do. Which sometimes not only makes it look more appealing but also enables one to be able to see hidden bugs. One very interesting term named <a href="https://en.wikipedia.org/wiki/Code_smell">code smell</a> is also used in the similar sense sometimes. It means the sense that certain part of the code indicates a deeper problem. Though it may work, it is a weak point to the code, which can be dealt with decomposition (breaking a large problem into smaller pieces that are more understandable) and then refactor it.</p>
<p>But blame shows the last revision, so when a code piece is refactored, what happens is, the blame changes to the person who did the movement of the line from one place to other for the readability's sake. This is both good and bad.</p>
<p>Good because, if the code breaks due to improper refactoring, then the person responsible can be identified. It is very necessary to identify such person if the improper refactoring was intentional and was an attempt to hamper the project. But mostly this is not the case, since tests in the project repo breaks in most of the projects. From this comes the bad part. If the line moved or copied is blamed to the person refactoring the code, then it's almost useless. You cannot understand why the line was placed there seeing a refactoring commit.</p>
<p>This problem can be solved by the pickaxe.</p>
<h3>Pickaxe</h3>
<p>Pickaxe is not a new command. It's just git log with <a href="https://git-scm.com/docs/git-log#git-log--Sltstringgt">-S</a> argument. But it's not the syntax what's interesting. It's what pickaxe is capable of doing. It searches for a commit where a string was added or removed. And so you can easily reach to the actual author of the commit.</p>
<p>It's syntax is:</p>
<div class="highlight"><pre><span></span><code>git log -S <search-string> [path/file]
</code></pre></div>
<p>Another preferred way is to use -p option simultaneously which will also generate a patch or a diff of the commit. It's then easier to see what change took place in which commit.</p>
<div class="highlight"><pre><span></span><code>git log -p -S <search-string> [path/file]
</code></pre></div>
<p>In this, you can also specify the filename in which to search this specific string, but it loses when the filename is changed in a commit. So at times, it's better not to specify the filename and search among the results that are displayed.</p>
<p>Git blame comes with GUI for Tcl/Tk based interface versions, where it does both pickaxe and blame together! It lists the original commits, (the ones which created the said line), and the latest commit on that line including moving and copying it from one place to another in the file. You can further explore more on this.</p>
<p>If you've any questions, please feel free to ask in the comments section below.</p>What are dotfiles?2018-01-26T18:04:00+05:302018-01-26T18:04:00+05:30GeekyShacklebolttag:thegeekyway.com,2018-01-26:/what-are-dotfiles/<p>What happens when your hard disk fries, or eventually you want to get a new machine? You tend up to loose all your dev configuration after years of hard work tweaking things here and there according to your liking.</p>
<p>Configuring a dev machine according to your liking would've been a …</p><p>What happens when your hard disk fries, or eventually you want to get a new machine? You tend up to loose all your dev configuration after years of hard work tweaking things here and there according to your liking.</p>
<p>Configuring a dev machine according to your liking would've been a really daunting task until and unless there were dotfiles.<!--more--></p>
<h3>Introduction</h3>
<p>These are simply the files whose names start with a dot (.) like <strong><em>.vimrc, .bashrc, .ssh, </em></strong>etc. And any file that begins with a <strong><em>dot</em></strong> is treated as hidden in all *<strong>nix</strong> operating systems. Therefore, having lots of dotfiles even in your home directory does not make it looks clumsy.</p>
<h3>Where are dotfiles needed?</h3>
<p>They are mainly used for software configuration. Therefore, they are also known as <strong>configuration files</strong>. We can always configure different tools that we use daily, easily using dotfiles.<br>
They can also be used to automate a series of package installations. Their usefulness lies in the fact that anytime you can buy a new system and configure it like, you've been using it for years.</p>
<h3>How to use dotfiles?</h3>
<p>In order to maximize their utilization, we should save our dotfiles somewhere. That may be a USB drive, cloud storage and what most people use nowadays is, the GitHub repository. The idea is, they remain within our reach while switching between different machines.</p>
<p>There are different dotfiles for different purposes. For example, <strong><em>.bashrc</em></strong> to configure bash and similarly <strong><em>.zshrc</em></strong> to configure zsh. We can also create our own dotfiles for different purposes like <strong><em>.aliases </em></strong>to declare all modified commands and <strong><em>.export</em></strong> to set environment variables we need. In this tutorial, we'll see a couple of examples to show the working of these dotfiles. Let's go ahead!</p>
<h4>.bashrc</h4>
<p>This is the default file, that runs every time when we open our bash. Either we can directly write all the bash configurations in this file or we can use a distributive approach, like having <strong>functions</strong> in one file and OS specific settings in different files, etc. Following is an example of a <strong><em>.bashrc.</em></strong></p>
<div class="highlight"><pre><span></span><code>[ -n "$PS1" ] && source ~/.profile
</code></pre></div>
<p>This means that, if <strong>"\$PS1"</strong> will return a non-empty string, then load <strong><em>.profile.</em></strong></p>
<h4>.profile</h4>
<p>We may use this to load OS-specific settings, and all other dotfiles, like <strong><em>.functions, .aliases, .exports,</em></strong> etc. Let's take a look.</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Load ~/.extra, ~/.exports, ~/.aliases and ~/.functions</span>
<span class="c1"># ~/.extra can be used for settings you don’t want to commit</span>
<span class="k">for</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="o">~/.</span><span class="p">{</span><span class="n">exports</span><span class="p">,</span><span class="n">aliases</span><span class="p">,</span><span class="n">functions</span><span class="p">,</span><span class="n">extra</span><span class="p">};</span><span class="w"> </span><span class="n">do</span>
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="o">-</span><span class="n">r</span><span class="w"> </span><span class="s2">"$file"</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="s2">"$file"</span>
<span class="n">done</span>
<span class="n">unset</span><span class="w"> </span><span class="n">file</span>
<span class="c1"># Detect and load OS specific settigs</span>
<span class="n">platform</span><span class="o">=</span><span class="s1">'unknown'</span>
<span class="n">unamestr</span><span class="o">=</span><span class="err">`</span><span class="n">uname</span><span class="err">`</span>
<span class="k">if</span><span class="w"> </span><span class="p">[[</span><span class="w"> </span><span class="s2">"$unamestr"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'Linux'</span><span class="w"> </span><span class="p">]];</span><span class="w"> </span><span class="n">then</span>
<span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="o">~/.</span><span class="n">linux</span>
<span class="k">elif</span><span class="w"> </span><span class="p">[[</span><span class="w"> </span><span class="s2">"$unamestr"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'FreeBSD'</span><span class="w"> </span><span class="p">]];</span><span class="w"> </span><span class="n">then</span>
<span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="o">~/.</span><span class="n">freebsd</span>
<span class="k">elif</span><span class="w"> </span><span class="p">[[</span><span class="w"> </span><span class="s2">"$unamestr"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'Darwin'</span><span class="w"> </span><span class="p">]];</span><span class="w"> </span><span class="n">then</span>
<span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="o">~/.</span><span class="n">osx</span>
<span class="n">fi</span>
</code></pre></div>
<h4>.aliases</h4>
<p>An alias can either be a modified command, or a shorthand for it that we may define ourselves. Suppose, for git commit, we always write <em><code>git commit -m "commit message"</code>.</em> In this case, we can create an alias by <em><code>alias gcm=git commit -m</code></em> with this, <strong><em>gcm </em></strong>is same as <strong><em>git commit -m.</em></strong> Following is a gist of a <strong><em>.aliases</em></strong> file.</p>
<div class="highlight"><pre><span></span><code><span class="c"># </span><span class="nb">------------------------------------------------------------------------------</span>
<span class="c"># | Network |</span>
<span class="c"># </span><span class="nb">------------------------------------------------------------------------------</span>
<span class="c"># Enhanced WHOIS lookups</span>
<span class="c">alias whois="whois </span><span class="nb">-</span><span class="c">h whois</span><span class="nb">-</span><span class="c">servers</span><span class="nt">.</span><span class="c">net"</span>
<span class="c"># IP addresses</span>
<span class="c">alias ip="curl ipinfo</span><span class="nt">.</span><span class="c">io/ip"</span>
<span class="c">alias localip='python </span><span class="nb">-</span><span class="c">c "import socket; print(socket</span><span class="nt">.</span><span class="c">gethostbyname(socket</span><span class="nt">.</span><span class="c">gethostname()));"'</span>
<span class="c"># Copy my public key to my clipboard</span>
<span class="c">alias pubkey="more ~/</span><span class="nt">.</span><span class="c">ssh/id_rsa</span><span class="nt">.</span><span class="c">pub | pbcopy | echo '=</span><span class="nv">></span><span class="c"> Public key copied to pasteboard</span><span class="nt">.</span><span class="c">'"</span>
</code></pre></div>
<p>The complete file is <a href="https://github.com/CuriousLearner/dotfiles/blob/master/.aliases">here</a>.</p>
<h4>.exports</h4>
<p><strong>Export</strong> is widely used to set environment variables. Also, we <strong>export</strong> a variable, when we need it in other programs. That depends upon the shell as well. For bash, <strong>export</strong> makes a variable usable, in the environment for child processes. For more about export, you may see <a href="http://www.gnu.org/software/bash/manual/bash.html#index-export">GNU bash manual</a>. Your <strong><em>.export </em></strong>file may look like <a href="https://github.com/CuriousLearner/dotfiles/blob/master/.exports">this</a>.</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Make vim the default editor</span>
<span class="k">export</span><span class="w"> </span><span class="n">EDITOR</span><span class="o">=</span><span class="s2">"vim"</span>
<span class="c1"># Don’t clear the screen after quitting a manual page</span>
<span class="k">export</span><span class="w"> </span><span class="n">MANPAGER</span><span class="o">=</span><span class="s2">"less -X"</span>
<span class="c1"># Larger bash history (allow 32³ entries; default is 500)</span>
<span class="k">export</span><span class="w"> </span><span class="n">HISTSIZE</span><span class="o">=</span><span class="mi">32768</span>
<span class="k">export</span><span class="w"> </span><span class="n">HISTFILESIZE</span><span class="o">=$</span><span class="n">HISTSIZE</span>
<span class="k">export</span><span class="w"> </span><span class="n">HISTCONTROL</span><span class="o">=</span><span class="n">ignoredups</span>
</code></pre></div>
<h4>.functions</h4>
<p>If we want to create an alias that not just perform a shorthand or a group of commands but, also which behaves conditionally and can work with variables, then we have a better approach of <strong>"functions". </strong>A function is an independent block of script, which is more powerful than an alias because they can accept variables. We can create functions for various purposes like:</p>
<div class="highlight"><pre><span></span><code><span class="cp"># copy the work directory to clipboard</span>
<span class="n">function</span><span class="w"> </span><span class="nf">pwdc</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">pwd</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">tr</span><span class="w"> </span><span class="o">-</span><span class="n">d</span><span class="w"> </span><span class="s">"</span><span class="se">\r\n</span><span class="s">"</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">pbcopy</span>
<span class="p">}</span>
<span class="cp"># touch a file while creating directories</span>
<span class="n">supertouch</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="n">$</span><span class="err">#</span><span class="w"> </span><span class="o">-</span><span class="n">lt</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">];</span><span class="w"> </span><span class="n">then</span>
<span class="w"> </span><span class="n">echo</span><span class="w"> </span><span class="s">"Missing argument"</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="n">fi</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="s">"$@"</span><span class="p">;</span><span class="w"> </span><span class="k">do</span><span class="w"> </span>
<span class="w"> </span><span class="n">mkdir</span><span class="w"> </span><span class="o">-</span><span class="n">p</span><span class="w"> </span><span class="o">--</span><span class="w"> </span><span class="s">"$(dirname -- "</span><span class="n">$f</span><span class="s">")"</span>
<span class="w"> </span><span class="n">touch</span><span class="w"> </span><span class="o">--</span><span class="w"> </span><span class="s">"$f"</span><span class="w"> </span>
<span class="w"> </span><span class="n">done</span><span class="w"> </span>
<span class="p">}</span>
</code></pre></div>
<p>More functions are available <a href="https://github.com/CuriousLearner/dotfiles/blob/master/.functions">here</a>.</p>
<h4>.vimrc</h4>
<p>This dotfile is specifically used for "<strong>vim</strong>" configuration. All the necessary commands that we use very often, we can put them in this file. We can also set up different variables of vim, with the value we desire while working on it. For Unix and MacOS, this file is always used and is recommended at <code>~/.vimrc</code>. To know more about .vimrc you may open the "<strong>vim</strong>" and use the command <code>:help vimrc-intro</code>.</p>
<div class="highlight"><pre><span></span><code>set nocompatible
set et
set ai
set sw=4
set ts=8
set ruler
</code></pre></div>
<h4>.gitignore</h4>
<p>This file is used in the case, we want to avoid or ignore some files or folders while <strong>git</strong> makes a commit. These files may include <em>Thumbnail cache files, local virtual environment, etc.</em> If we want a particular file say <strong><em>Example.tmp</em></strong> to be ignored. Just give its name in this dotfile. And if we want to ignore all <strong><em>.tmp</em></strong> files then put <strong><em>*.tmp</em></strong> in <strong><em>.gitignore. </em></strong>A gist of this file is shown below and a complete file is available <a href="https://github.com/CuriousLearner/dotfiles/blob/master/.gitignore">here</a>.</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Files that might appear on external disks</span>
<span class="na">.Spotlight-V100</span>
<span class="na">.Trashesvenv</span>
<span class="c1"># Packages ignore</span>
<span class="nf">node_modules</span>
<span class="na">.tmp</span>
<span class="na">.sass-cache</span>
<span class="nf">app</span><span class="err">/</span><span class="no">bower_components</span>
</code></pre></div>
<h3>Conclusion</h3>
<p>We're keeping this post short, as the main motive was to explain how you can store the configuration of your machine. The whole purpose of dotfiles is to make our work as easy as possible. These hidden files give us a comfortable & quality working experience.<br>
Dotfiles are specific to an individual because of different people like different configuration for the same tool. Go ahead and explore some of the best dotfiles out there. You're free to take inspirations and create your own :)</p>
<p>You can have a look at one of the dotfiles repo here: <a href="https://github.com/CuriousLearner/dotfiles">https://github.com/CuriousLearner/dotfiles</a></p>
<p>If you've any queries, please ask in comment section below.</p>[Ultimate Guide] How SSH works?2017-11-30T08:58:00+05:302017-11-30T08:58:00+05:30GeekyShacklebolttag:thegeekyway.com,2017-11-30:/ultimate-guide-how-ssh-works/<p>SSH is quite an interesting protocol in itself. A lot of people are using it for a secure communication between two machines. This blog post hits behind the scenes that go on, between machines connected via SSH.</p>
<p>You might have an experience of remote login on SSH. But have you …</p><p>SSH is quite an interesting protocol in itself. A lot of people are using it for a secure communication between two machines. This blog post hits behind the scenes that go on, between machines connected via SSH.</p>
<p>You might have an experience of remote login on SSH. But have you ever wondered about how internally these ssh-client and ssh-server respond? And If you want to explore the same? You are on the right page!<!--more--></p>
<h2>Brief about SSH</h2>
<ul>
<li>SSH or we can say Secure Shell is a cryptographic network protocol that’s used to provide security in communication between two machines over an unsecured network like our Internet.</li>
<li>SSH uses different authentication and encryption-decryption methods to make our connection secure between two remote machines.</li>
<li>It is better than other communication protocols like FTP, telnet, etc. The picture below describes how SSH encryption doesn't let someone sniff in its signals. If someone tries to intercept the signal, s/he would receive nothing but an encrypted data packet. To know more about how SSH better than others visit <a href="https://www.yireo.com/blog/1452-why-ssh-instead-of-ftp">here</a>.</li>
</ul>
<div style="text-align:center">
<div class="highlight"><pre><span></span><code><span class="o">!</span><span class="p">[</span><span class="n">An</span><span class="w"> </span><span class="n">un</span><span class="o">-</span><span class="n">encrypte</span><span class="w"> </span><span class="n">telnet</span><span class="w"> </span><span class="n">connection</span><span class="p">]({</span><span class="k">static</span><span class="p">}</span><span class="o">/</span><span class="n">uploads</span><span class="o">/</span><span class="n">images</span><span class="o">/</span><span class="mi">2017</span><span class="o">/</span><span class="mi">11</span><span class="o">/</span><span class="n">howsshworks1</span><span class="o">-</span><span class="mi">300</span><span class="n">x220</span><span class="o">.</span><span class="n">png</span><span class="w"> </span><span class="s2">"An un-encrypte telnet connection"</span><span class="p">)</span>
</code></pre></div>
</div>
<div style="text-align:center">
<div class="highlight"><pre><span></span><code><span class="o">!</span><span class="p">[</span><span class="n">An</span><span class="w"> </span><span class="n">encrypted</span><span class="w"> </span><span class="n">SSH</span><span class="w"> </span><span class="n">sesion</span><span class="p">]({</span><span class="k">static</span><span class="p">}</span><span class="o">/</span><span class="n">uploads</span><span class="o">/</span><span class="n">images</span><span class="o">/</span><span class="mi">2017</span><span class="o">/</span><span class="mi">11</span><span class="o">/</span><span class="n">howsshworks2</span><span class="o">-</span><span class="mi">300</span><span class="n">x219</span><span class="o">.</span><span class="n">png</span><span class="w"> </span><span class="s2">"An encrypted SSH sesion"</span><span class="p">)</span>
</code></pre></div>
</div>
<ul>
<li>For use cases of SSH, you may see <a href="http://matt.might.net/articles/ssh-hacks/">this</a>.</li>
</ul>
<h1>How SSH works?</h1>
<p>Before we actually get into the protocol, there is something that needs to be discussed.</p>
<h4>Symmetric and Asymmetric Encryption:</h4>
<p><strong>Symmetric Encryption</strong> is that in which only one key (let's say private key or secret key) is used both for encryption and decryption of the data transferred between client and server.</p>
<p>However, <strong>Asymmetric Encryption</strong> is that in which both keys (private as well as public key) is used for encryption and decryption.</p>
<p>The server uses client's public key to encrypt data for it. The client, on the other hand, uses its private key to decrypt the data on the other end that was encrypted by its public key. Similarly, server's public key is used by the client to encrypt the signal and server's private key is used by the server to decrypt the signal.</p>
<p>So, which one is used in SSH?</p>
<p>The answer is, SSH uses both symmetric and asymmetric encryption. Since asymmetric encryption is more time consuming, most of the SSH connections use symmetric encryption. The idea behind is that asymmetric encryption is used only to share a secret key (session key) using which, symmetric encryption can be done for further communication.</p>
<p>Now there are 2 versions of SSH that are commonly used. These are SSH version 1 and SSH version 2. The overall architecture in both versions differs a bit, so we'll discuss the protocol version 1 in detail and then see how does it differ from version 2 protocol.</p>
<h2>SSH Protocol Version 1</h2>
<p>The SSH connection is always invoked by a client to a server. Therefore, server authentication comes first than client authentication.</p>
<p><strong><em>STEP 1: Connect to simple FTP connection</em></strong></p>
<p>For a successful SSH establishment. A simple connection between a client and a server is a must. For the same reason, first of all, a simple (FTP/Telnet) connection is created between the client and the server. And with telnet, we get details like this:</p>
<div class="highlight"><pre><span></span><code><span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~</span><span class="err">$</span><span class="w"> </span><span class="n">telnet</span><span class="w"> </span><span class="n">thegeekyway</span><span class="p">.</span><span class="n">com</span><span class="w"> </span><span class="mi">22</span>
<span class="n">Trying</span><span class="w"> </span><span class="mf">192.168.42.119</span><span class="p">...</span>
<span class="n">Connected</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">thegeekyway</span><span class="p">.</span><span class="n">com</span><span class="p">.</span>
<span class="k">Escape</span><span class="w"> </span><span class="k">character</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="s1">'^]'</span><span class="p">.</span>
<span class="n">SSH</span><span class="o">-</span><span class="mf">2.0</span><span class="o">-</span><span class="n">OpenSSH_7</span><span class="mf">.2</span><span class="n">p2</span><span class="w"> </span><span class="n">Ubuntu</span><span class="o">-</span><span class="mi">4</span><span class="n">ubuntu2</span><span class="mf">.2</span>
</code></pre></div>
<p>Here one can see that "<em><strong>SSH-2.0</strong>"</em> (in the last line). This is the version of SSH protocol that server is using. We also see the package version of OpenSSH is visible. This is how the client knows which version of SSH the server is using.</p>
<p>Now, it is the sole responsibility of the client to continue with the session if it supports the version shown by the server.</p>
<p><strong><em>STEP 2: Both server and client, now switch to a packet-based protocol</em></strong></p>
<p>In this, each packet consists of a 32-bit length field, 1-8 bytes of random padding to foil known-plaintext attacks (KPA), a one-byte packet type code, the packet payload data, and a four-byte integrity check field.</p>
<p>***STEP 3: Server provides session parameters to client </p>
<hr>
<p>As soon as the connection is established, server authentication starts. For this, the server will send some critical data to the client. This data includes:</p>
<ul>
<li>1) <strong>Server's host key</strong>: It is an RSA public key, which is stored in "known_hosts" file of the client (if you have already connected before with that server). You may see your known_hosts file here:</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~</span><span class="err">$</span><span class="w"> </span><span class="n">cd</span><span class="w"> </span><span class="p">.</span><span class="n">ssh</span><span class="o">/</span>
<span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~/</span><span class="p">.</span><span class="n">ssh</span><span class="err">$</span><span class="w"> </span><span class="n">ls</span>
<span class="n">authorized_keys</span><span class="w"> </span><span class="n">id_rsa</span><span class="w"> </span><span class="n">id_rsa</span><span class="p">.</span><span class="n">pub</span><span class="w"> </span><span class="n">known_hosts</span>
</code></pre></div>
<p>At this point, the client will search for this "rsa_public" key in client's `known_hosts` file and if the key is not already present, then it may be the case of connecting with that server for the first time. So, in this case, what you would get is a "WARNING!" like: "<em>Are you sure you want to continue connecting?</em>"</p>
<div class="highlight"><pre><span></span><code><span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~</span><span class="err">$</span><span class="w"> </span><span class="n">ssh</span><span class="w"> </span><span class="n">geekyshacklebolt</span>
<span class="n">The</span><span class="w"> </span><span class="n">authenticity</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">host</span><span class="w"> </span><span class="s1">'geekyshacklebolt (192.168.42.222)'</span><span class="w"> </span><span class="n">can</span><span class="err">'</span><span class="n">t</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">established</span><span class="p">.</span>
<span class="n">ECDSA</span><span class="w"> </span><span class="k">key</span><span class="w"> </span><span class="n">fingerprint</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="nl">SHA256</span><span class="p">:</span><span class="n">Ql</span><span class="o">/</span><span class="n">KnGlolY9eCGuYK3OX3opnSyJQzsbtM3DW</span><span class="o">/</span><span class="n">UZIxms</span><span class="p">.</span>
<span class="k">Are</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">sure</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">want</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">continue</span><span class="w"> </span><span class="n">connecting</span><span class="w"> </span><span class="p">(</span><span class="n">yes</span><span class="o">/</span><span class="k">no</span><span class="p">)</span><span class="vm">?</span>
</code></pre></div>
<p>If you're not connecting with the server for the first time but see this warning, then maybe the server's public key has been changed or you're trying to access a different machine altogether (may be going in hands of an attacker). Therefore this warning is quite useful.</p>
<ul>
<li>2) <strong>Server's server key</strong>: (There is no concept of server key in SSH-2) With host key, server also provide one server key.</li>
</ul>
<p>This server key goes on changing every hour or so. And this key may be of 768 bits or 1024 bits it depends on the configuration. You may find it at "/etc/ssh/" in "sshd_config" file. Let me see mine.</p>
<div class="highlight"><pre><span></span><code># Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
</code></pre></div>
<p>Yeah, mine is 1024 bits.</p>
<ul>
<li>3) <strong>8 Random Bytes</strong>: These random bytes are known as <em>check bytes</em>. It is necessary for the client to send these <em>check bytes</em> in its next reply.</li>
<li>4) <strong>Authentication methods and list of encryption algorithms</strong> <strong>supported by server:</strong> So, that client may decide which encryption algorithm it supports and which authentication methods the client has to follow. The symmetric encryption algorithm that is used to encrypt and decrypt large amounts of data is known as <strong>bulk cipher.</strong></li>
</ul>
<p>At this point, both sides also compute a common 128-bit <strong>session identifier</strong><em>,</em> which is used in some subsequent protocol operations to uniquely identify this SSH session. This is an MD5 hash of the <em>host key</em>, <em>server key</em>, and <em>check bytes</em> taken together.</p>
<p><strong><em>STEP 4: Session key generation and sending it to server</em></strong></p>
<p>Now the client has server's host key, server key, 8 random bytes, authentication methods and encryption algorithms list.</p>
<p>The client creates a symmetric session key for a bulk cipher that both client and server support. This key will be used for the entire session of SSH for both encryption and decryption. For a secure transmission, client double encrypts this key before sending it to server. First encryption is done by "server's host key" and second encryption is done by "server key". Encrypting the session key a second time with the server key provides a property called <strong><em>perfect forward secrecy</em></strong>. This double encryption enhances the security to great extent.</p>
<p>After double encrypting the session key, the client sends it to the server, along with the check bytes and a choice of algorithms.</p>
<p>Since it is difficult to have both server's private key and the server key (which keep changing in a fixed interval duration), it is very difficult to know the key. On the other hand, server has its private key and the "session key" that is shared by both client and server. With this data, the server can decrypt and get the session key for further communication.</p>
<p><strong><em>STEP 5: Confirmation from the server</em></strong></p>
<p>After sending the "session key" client just wait for the server to respond. Because only if the server is original and the one whose host key was provided to the client, then only it would be able to decrypt this session key using the server key, and the corresponding private key of server's host key.</p>
<p>The server receives this session key and decrypts it, to use it for further transmission of signals. This was the end of asymmetric cryptography. Now, when both server and client posses this symmetric key. The complete communication session would be dealing only with <em>this key</em>.</p>
<p><strong><em>STEP 6: Client Authentication begins</em></strong></p>
<p>To authenticates a client, the server may use the following methods:</p>
<ul>
<li>Password-based</li>
<li>Kerberos</li>
<li>Rhosts</li>
<li>Host keys based</li>
</ul>
<p>We'll explain each part in brief, and 'host keys based' in detail, as it is the most secured.</p>
<p>1. <strong>Password</strong>:</p>
<p>It is as simple as you log into any of your social websites. The server just asks you for your password to establish the SSH connection, and client takes the responsibility to transfer this password to the server. The session gets started as soon as you give your password. This method is not much recommended. Because if you are really security conscious that you are using SSH for remote login, then password-based authentication can be easily attacked. This type of authentication is convenient for the first time SSH users and those who travel a lot and don't carry their machines.</p>
<p>2. <strong>Kerberos</strong>:</p>
<p>It is a computer network authentication protocol that works on the basis of <em>tickets</em> to allow nodes communicating over a non-secure network to prove their identity to one another in a secure manner. It is not much used nowadays. OpenSSH provides Kerberos support only when using the SSH-1 protocol.</p>
<p>3. <strong>RHosts</strong>:</p>
<p>In this, the server has a list of host keys stored in /etc/ssh_known_host, and additionally, each user has host keys in \$HOME/.ssh/known_hosts. SSH uses the naming service to obtain the canonical name of the client host, looks for its public key in its known_host files, and requires the client to prove that it knows the private host key. This prevents IP and routing spoofing attacks (as long as the client machine private host key has not been leaked), but is still vulnerable to DNS (domain name server) attacks (to a limited extent), and relies on the integrity of the client machine as to who is requesting to log in. If maximal security is desired, only RSA authentication (host keys authentication) should be used.</p>
<p>4. <strong>Host keys based</strong>:</p>
<p>It is the most secure method of authentication for an SSH session. In this, client authentication is based on a pair of host keys i.e: the public key and the private key that can be seen in "\~/.ssh/" directory.</p>
<div class="highlight"><pre><span></span><code><span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~</span><span class="err">$</span><span class="w"> </span><span class="n">cd</span><span class="w"> </span><span class="p">.</span><span class="n">ssh</span><span class="o">/</span>
<span class="n">coderunner</span><span class="nv">@geekyshacklebolt</span><span class="err">:</span><span class="o">~/</span><span class="p">.</span><span class="n">ssh</span><span class="err">$</span><span class="w"> </span><span class="n">ls</span>
<span class="n">authorized_keys</span><span class="w"> </span><span class="n">id_rsa</span><span class="w"> </span><span class="n">id_rsa</span><span class="p">.</span><span class="n">pub</span><span class="w"> </span><span class="n">known_hosts</span>
</code></pre></div>
<p>Here, <em>id_rsa</em> is your private (secret) key which should be kept a secret (not to be shared with anyone ever) and id_rsa.pub is your public key, which should be present to every server's (to whom you want to connect to) "authorized_keys" file.</p>
<p>If you don't have these key pair, you may generate it using the command:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>ssh-keygen<span class="w"> </span>-t<span class="w"> </span>rsa<span class="w"> </span>-b<span class="w"> </span><span class="m">1024</span><span class="w"> </span>-C<span class="w"> </span><span class="s2">"your_email@example.com"</span>
</code></pre></div>
<p><em><strong>*note:</strong> these public and private keys are never used for either encryption or decryption of SSH session. These are only used to authenticate a client.</em></p>
<p>An RSA public key has 2 parts, the exponent, and the modulus.The modulus is the long number in the public key file.</p>
<ul>
<li>The client sends the modulus of its public key as an identifier, encrypted using the session key.</li>
<li>The server decrypts this identity and starts searching for its corresponding public key in "authorized_keys" file.</li>
<li>If the authorized public key is not found or is restricted to connect, then this authentication request fails. Otherwise, the process continues.</li>
</ul>
<p><strong><em>STEP 7: The Game of Challenge and Response</em></strong></p>
<ul>
<li>Now, server prepares a random 256 bit-string, and encrypt this string using the client's public key. Then this encrypted string is sent to the client as a <em>challenge. </em>The challenge for the client is to decrypt this random string and prove that it has the corresponding private key.</li>
<li>The client receives this encrypted string, and decrypts using its private key, and send it back to the server. But, this string should not be transmitted as it is. Therefore, client combines this challenge with session identifier to prepare an MD5 hash. And then sends its <em>response</em> to the server.</li>
<li>The server receives this <em>response</em> in the form of an MD5 hash. And, the server itself regenerate this, since it is already having the random string and the session identifier. The server compares the 2 hashes with each other, and if both come out to be same, then it means that client had successfully decrypted the string.</li>
</ul>
<p>The client is now authenticated, and SSH session starts with a terminal allotted to you to start writing commands.</p>
<p><strong>Integrity Checking:</strong></p>
<p>It is the process of comparing the state of currently stored data to the state of previously stored data. This protocol uses a weak integrity check e.i a 32-bit cyclic redundancy check or CRC-32. This sort of check is insufficient for detecting deliberate corruption but effective against accidental changes to data.</p>
<h2>SSH Protocol Version 2</h2>
<p>There are a lot of places where SSH-1 needed more improvements to make the protocol more secure. Therefore, this version 2 comes into play. It provides some necessary changes in protocol 1 to enhance its security. We will only discuss the changes and won't repeat the common steps.</p>
<h3>Protocol Differences</h3>
<p>SSH-1 consists of multiple functions in a single protocol. However, SSH-2 is build up in modules and consists of multiple protocols which work together like:</p>
<ul>
<li><strong>SSH transport layer protocol (SSH-TRANS)</strong></li>
<li><strong>SSH authentication protocol (SSH-AUTH)</strong></li>
<li><strong>SSH connection protocol (SSH-CONN)</strong></li>
</ul>
<p>Let's see their differences in brief.</p>
<p><u><strong>SSH-TRANS:</strong></u> This layer of protocol provides initial connection, packet-based protocol, server authentication, basic encryption and integrity check.</p>
<p><u><strong>SSH-AUTH:</strong></u> This layer of protocol is used by the client over SSH-TRANS to authenticate itself to server. And, it supports 3 authentication methods:</p>
<ul>
<li><strong>Public key:</strong> It is similar to "host based" of SSH-1. But, it is more general and can accommodate any public-key signature algorithm.</li>
<li><strong>Hostbased:</strong> It is similar to RhostsRSA of SSH-1 by providing cryptographic assurance of client's host identity.</li>
<li><strong>Password:</strong> It is similar to "Password Based" of SSH-1.</li>
</ul>
<h3>Key Differences</h3>
<p>The Key differences between SSH-1 and SSH-2 are as follows.</p>
<ul>
<li>Expanded algorithm negotiation</li>
<li>No server key</li>
<li>Certificates authorities for public keys</li>
<li>More flexible authentication</li>
<li>Stronger integrity checking</li>
<li>Periodic replacement of session key ("re-keying")</li>
</ul>
<p><strong>1. Expanded algorithm negotiation:</strong></p>
<p>In SSH-1, client used to select only one algorithm out of the list of algorithms supported by server, for all category works like, hash function, message authentication, session key exchange, etc. But, SSH-2 provides support for one algorithm per category.</p>
<p><strong>2. No server key:</strong></p>
<p>It was recommended that the session key shouldn't be developed alone by the client (as done in SSH 1). The idea was, neither the client nor the server should dictate the SSH session by giving a session key that is created only by one side. So, a new method was adopted in which both server and client equally contribute to the development of session key. This method was based on Diffie - Hellman algorithm.</p>
<p><em>The classic procedure of Diffie - Hellman algorithm to develop a session key is discussed below step-by-step:</em></p>
<ol>
<li>Firstly, both client and server agree on a large prime number, which will serve as a seed value.</li>
<li>Then, both parties agree on an encryption generator (typically AES), which will be used to manipulate the values in a predefined way. This encryption generator method is the one which will be supported by both server and client.</li>
<li>Independently, each party comes up with another prime number which is kept secret from the other party. This number is used as the private (secret) key for this interaction (this private key is different than the private SSH key used for authentication).</li>
<li>The generated private key (the secret to themselves), the encryption generator (common to both), and the shared prime number (common to both) are used to generate a public key, but which can be shared with the other party.</li>
<li>Both participants then exchange their generated public keys.</li>
<li>The receiving party uses their own private key, the other party's public key, and the original shared prime number to compute a shared secret key. Although this is independently computed by each party, using opposite private and public keys, will result in the <em>same</em> shared secret key.</li>
<li>The shared secret is then used to encrypt all communication that follows. This key is again known as a session key or symmetric key.</li>
</ol>
<p><strong>3. Certificate authorities for public keys:</strong></p>
<p>In SSH-1, there was no method to verify the ownership of the key. But, in SSH-2 we have a room for this as well. This is another layer of security in SSH-2. In which, the public keys of users are signed by some certificate authorities. The certificate attests and confirms the binding between a public key and a particular identity. The attestation is represented by a digital signature from a trusted third party.</p>
<p><strong>4. More flexible authentication:</strong></p>
<p>In SSH-1, client could authenticate itself to server using any one of the allowed authentication methods. And if it fails, then it means the connection is refused. But in SSH-2, if initial attempts of any authentication method (say public key) are failed, then the protocol may switch to other methods of authentication (say password). That's why SSH-2 gives more chances to establish a connection.</p>
<p><strong>5. Stronger Integrity checking:</strong></p>
<p>SSH-1 uses CRC-32 integrity check which is really weak and ineffective against deliberate corruption. On the other hand, SSH-2 uses cryptographically strong Message Authentication Code (MAC) algorithms to provide strong integrity and data origin assurance.</p>
<p><strong>6. Periodic replacement of session key ("re-keying")</strong></p>
<p>The concept was improved with an idea that the session key should not remain same for the complete session as we see in SSH-1. Therefore, in SSH 2, we may have a no. of session keys that keeps on changing with sessions, periodically.</p>
<p><strong>7. Implementation Differences</strong></p>
<p>There are mainly 4 Implementation differences in SSH-1 and SSH-2.</p>
<ul>
<li><strong>Host keys</strong>: SSH-1 and SSH-2 both manage host keys in different directory structures.</li>
<li><strong>No fallback to rsh:</strong> If a remote host has no SSH server running, then SSH can invoke <code>rsh</code> automatically, because SSH-1 supports <code>rsh</code>-style authentication as well. But SSH-2 don't have this feature, due to poor security of <code>rsh</code>.</li>
<li><strong>Setuid client:</strong> In SSH-1, the client needs to be <code>setuid</code> root, so that it has access to <em>read</em> for the <em>private host key</em> file, which is usually installed as <em>readable only</em> by the root user. But in SSH-2, the client doesn't need to be <code>setuid</code>root, due to its programs like <code>ssh-signer2</code>.</li>
<li><strong>SSH-1 backward compatibility:</strong> SSH-2 provide backward compatibility to SSH-1, if and only if <em>SSH-1 package</em> is installed on the same machine. The SSH-2 client and server simply run their SSH-1 counterparts, when they connect to a partner running the older protocol.</li>
</ul>
<p>We'll leave the rest for readers to explore on their own. If you've any questions, please feel free to ask in the comments section below.</p>Have you ever heard of OG-TAGS?2017-11-30T08:20:00+05:302017-11-30T08:20:00+05:30storymode7tag:thegeekyway.com,2017-11-30:/have-you-ever-heard-of-og-tags/<p>Ever noticed how Facebook shows an excerpt from a website with its image, or how a tweet with just a URL changes into a short window to the actual website? It's because of og-tags. Let's understand more about it!</p>
<!--more-->
<div style="text-align:center">
<p><img alt="Open Graph Protocol Logo" src="https://i2.wp.com/storymode7.files.wordpress.com/2017/11/open-graph-protocol.png" title="Open Graph Protocol Logo"></p>
</div>
<h2>Overview of OG Tags</h2>
<p>The <a href="http://ogp.me/">Open Graph Protocol</a> is what works …</p><p>Ever noticed how Facebook shows an excerpt from a website with its image, or how a tweet with just a URL changes into a short window to the actual website? It's because of og-tags. Let's understand more about it!</p>
<!--more-->
<div style="text-align:center">
<p><img alt="Open Graph Protocol Logo" src="https://i2.wp.com/storymode7.files.wordpress.com/2017/11/open-graph-protocol.png" title="Open Graph Protocol Logo"></p>
</div>
<h2>Overview of OG Tags</h2>
<p>The <a href="http://ogp.me/">Open Graph Protocol</a> is what works behind og-tags. It was introduced by Facebook in 2010 to better integrate a website with Facebook. It turns websites into intractable objects, and makes it easier to share. Like on a website when you choose to share via Facebook, it creates a summary of the URL you wish to share. Like the one below:</p>
<div style="text-align:center">
<p><img alt="" class="aligncenter" src="https://i1.wp.com/storymode7.files.wordpress.com/2017/11/screenshot-2017-11-27-may-jay1.png"></p>
</div>
<p>See how beautifully it creates the summary of the content! Similarly, it works on Twitter. The level up to which you can interact depends on the type of data shared like:</p>
<div style="text-align:center">
<p><img alt="" class="aligncenter" src="https://i2.wp.com/storymode7.files.wordpress.com/2017/11/screenshot-2017-11-27-mayank-mayank_at_tgw-twitter.png"></p>
</div>
<p>Here, twitter even allows playing videos in this twitter post itself.</p>
<p>Now, Let's see some of the details, how it works, basic syntax etc.</p>
<p>It's based on <a href="https://en.wikipedia.org/wiki/RDFa">RDFa: Resource Description Framework in attributes.</a> Hence, it uses metadata from a page to turn them into these graph objects. You can think of object as the website being displayed, and Graph as the small summary style window created using og-tags. And for it, <code><meta></code> tags are required. In each meta tag, a property can be specified. A sample og tag might look like :</p>
<p>For Facebook:</p>
<div class="highlight"><pre><span></span><code><meta property="og:title" content=" The Geeky Way"/>
</code></pre></div>
<div>
</div>
<p>For Twitter:</p>
<div class="highlight"><pre><span></span><code><meta name="twitter:title" content="The Geeky Way"/>
</code></pre></div>
<div>
</div>
<p>This will set the Title of the object as it should appear in Graph. For example, in the first image that we created using Facebook share as shown above, we have <code>WordPress.com: Create a website or a blog</code> as the title for the og tag.</p>
<h2>How does it work?</h2>
<p>There are a number of <a href="http://ogp.me/#implementations">parsers</a> that parse information from these og tags and render them as the social graph that we've seen above.</p>
<p>Facebook also has its <a href="https://developers.facebook.com/tools/debug/">official parser</a> using which you can see how your website will look like after setting all the nice og tags. It'll tell you the value set for the og tags that it read.</p>
<p>Like if you do that for TheGeekyWay.com you can see the og:title tag as :</p>
<p><code>og:title | The Geeky Way</code></p>
<p>And that's just what the image above shows!</p>
<h2>Some basic OG tags</h2>
<h3>For Facebook</h3>
<ul>
<li><code>og:title</code> - The Title of the object, web page.</li>
<li><code>og:type</code> - The <a href="http://ogp.me/#types">type of content</a> of that object, like website, music.[song| album| playlist ... ]</li>
<li><code>og:image</code> - URL of an image that represents the object.</li>
<li><code>og:url</code> - The <a href="https://en.wikipedia.org/wiki/Canonical_link_element">canonical</a> URL of the object, i.e the one you want to provide to a user. Not necessarily the one which created the object.</li>
</ul>
<p>These should get you going with a perfectly working object graph.</p>
<p>But, that's not all! Extra metadata can be attached to each property. For instance, for an image you can specify width, height, alt text etc. You can refer to <a href="http://ogp.me/#types">this</a> link to see how much metadata can be added.</p>
<h3>For Twitter</h3>
<p>It's called <a href="https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/abouts-cards">Twitter cards</a> on Twitter.</p>
<p>Basic tags for twitter graph can be summarized as :</p>
<p><code><meta name="twitter:card" content="summary"/></code><br>
<code><meta name="twitter:site" content="@username"/> <meta name="twitter:title" content="Title Of The Page goes here"/> <meta name="twitter:description" content="This contains a brief description of the page"/> <meta name="twitter:image" content="www.url.of/the/image.jpg"/></code></p>
<p>But, It might happen that you inserted the OG tags for the Facebook graph, but you forgot the to add the twitter version of it that helps create twitter card. Well, Twitter takes care of it. If it's not able to find the necessary meta tags, then it uses OG tags, and according to a predefined relationship between them, it uses the value from those OG tags to create the twitter card.</p>
<p>Hope this quick tip about og tags helps you better set up and optimize your website. If you've any questions, please feel free to ask in the comments section below.</p>How to Recover Deleted SMS from Samsung Galaxy Phone2017-10-18T16:06:00+05:302017-10-18T16:06:00+05:30sponsored_posttag:thegeekyway.com,2017-10-18:/how-to-recover-deleted-sms-from-samsung-galaxy-phone/<p .s1>How to recover deleted SMS from Samsung Galaxy has always been one of the frequently asked on the forums and the internet by Samsung users. No matter how wise and cautious person you are, still once in a while you will end up deleting some important message and will instantly …</p><p .s1>How to recover deleted SMS from Samsung Galaxy has always been one of the frequently asked on the forums and the internet by Samsung users. No matter how wise and cautious person you are, still once in a while you will end up deleting some important message and will instantly regret it. There are lots of other reasons to get your SMS deleted for example, factory reset, system crash or installation of a suspicious virus. </p>
<p .s1>With the advancement of technology, recovering deleted SMS from Android phones is not a difficult task anymore. Software developers have designed many Android data recovery tools to retrieve the deleted data from your phone to save your day. Likewise, after testing and researching a lot of Android data recovery tools, we have discovered that Android Data Recovery is the best and most efficient tool among all of them.</p>
<p .s1>**What is Android Data Recovery?**</p>
<p>It is a marvelous and creative Android data recovery desktop program that can <a href="https://www.androidphonesoft.com/resources/recover-lost-sms-from-android-phone.html"> <p .s2>retrieve deleted data </p></a>from your Android device including Samsung Galaxy Phones. It can recover your erased SMS within seconds by scanning your phone deeply from Windows or Mac computer. According to over 4 million customers, Android Data Recovery is the best program and most efficient program available on the software market.</p>
<p .s1>**Highlighted features of Android Data Recovery** </p>
<ul>
<li><p .s1>Recover 17+ data types from any Android smartphone.</p></li>
<li><p .s1>Scan your entire Android phone and tablet to retrieve data.</p></li>
<li><p .s1>Preview each and every item before recovering them so that you can only retrieve the desirable data.</p></li>
<li><p .s1>Supported data types – Contacts, SMS, music, videos, audio, documents, Whatsapp, etc.</p></li>
<li><p .s1>Works with over 3500+ smartphone brands such as Samsung, Asus, Micromax, Motorola, Google, and LG and so on.</p></li>
<li><p .s1>Compatible with both Windows and Mac.</p></li>
<li><p .s1>Trusted and download by over 4 million customers.</p></li>
<li><p .s1>Free lifetime support and upgrades.</p></li>
</ul>
<p .s1>**How to use Android Data Recovery tool to Retrieve Deleted SMS?**</p>
<p .s1>Although, this program requires no instruction at all but yet, we will write down a step by step tutorial so that novice users can use this program effortlessly.</p>
<p>Step 1. <a href="https://www.androidphonesoft.com/android-data-recovery.html"> <p .s2>Download Android Data Recovery</p></a> on your Windows or Mac computer and install it like regular software. Next, launch the tool to see its attractive user interface.</p>
<div style="text-align:center">
<p><img alt="Step 1: How to recover deleted SMS " src="//thegeekyway.com/uploads/images/2017/10/1-300x182.png" title="Step 1: How to recover deleted SMS"></p>
</div>
<p .s1>Step 2. Now, get your Samsung Galaxy phone connected to PC and enable USB debugging mode on your phone.</p>
<p .s1>To enable USB debugging mode on your Samsung Galaxy, kindly follow this path –</p>
<p .s1>Open Menu \> Settings \> Developer Tools \> Check USB Debugging mode</p>
<div style="text-align:center">
<p><img alt="Step 2: How to recover deleted SMS " src="//thegeekyway.com/uploads/images/2017/10/2-300x153.png" title="Step 2: How to recover deleted SMS"></p>
</div>
<p .s1>Step 3. The tool should recognize your phone now and you will be asked what kind of data to retrieve from your PC. Simply, select “Messaging” and hit the next button.</p>
<div style="text-align:center">
<p><img alt="Step 3: How to recover deleted SMS " src="//thegeekyway.com/uploads/images/2017/10/3-300x182.png" title="Step 3: How to recover deleted SMS"></p>
</div>
<p .s1>Step 4. Android data recovery tool provides 2 amazing ways to scan your device named Standard Mode and Advanced Mode. It is recommended to use Standard Mode which uses a powerful algorithm to search your entire phone and Advanced Mode is used for deeper scanning and usually takes longer time to finish the process.</p>
<div style="text-align:center">
<p><img alt="Step 4: How to recover deleted SMS " src="//thegeekyway.com/uploads/images/2017/10/4-300x182.png" title="Step 4: How to recover deleted SMS"></p>
</div>
<p .s1>Step 5. Let the program analyze your complete device and kindly have patience because it may take several minutes. Once the scanning is completed, it will let you preview each and every deleted item on your phone. Click on “Messages” from the left sidebar to preview all erased messages.</p>
<div style="text-align:center">
<p><img alt="Step 5: How to recover deleted SMS " src="//thegeekyway.com/uploads/images/2017/10/5-300x182.png" title="Step 5: How to recover deleted SMS"></p>
</div>
<p .s1>Finally choose the messages that you wish to retrieve and click on “Recover” to save them on your PC.</p>
<p .s1>After recovering the messages successfully, you can copy the database file on your Samsung Galaxy phone to read them on your mobile device.</p>
<p .s1>**Conclusion**</p>
<p .s1>Say goodbye to those days when erasing messages was considered to be a permanent action. With Android Data Recovery tool, you can relax and feel free to recover all the deleted data from your Android device seamlessly. Android Data Recovery tool has 4+ million downloads who approved the simplicity and efficiency of this lavishing tool. It is free to try, so go ahead and install it on your computer to see the real power of this program. It is recommended that you guys backup your Android phones regularly once per week to avoid any data loss.</p>Introduction to React Native - Part 12017-10-07T14:11:00+05:302017-10-07T14:11:00+05:30realslimshankytag:thegeekyway.com,2017-10-07:/introduction-to-react-native-part-1/<div style="text-align:center">
<p><img alt="Introduction to React Native - Part 1" class="aligncenter" src="https://i.imgur.com/7o0tXgF.png"></p>
</div>
<h3>What is all the fuzz about Hybrid Apps?</h3>
<p><a href="https://facebook.github.io/react-native/">React Native</a> is a framework build over the native development kit of both Android and iOS with which you can create mobile apps for both the platforms by just using JavaScript.</p>
<!--more-->
<p>React Native was developed by some group of people working in …</p><div style="text-align:center">
<p><img alt="Introduction to React Native - Part 1" class="aligncenter" src="https://i.imgur.com/7o0tXgF.png"></p>
</div>
<h3>What is all the fuzz about Hybrid Apps?</h3>
<p><a href="https://facebook.github.io/react-native/">React Native</a> is a framework build over the native development kit of both Android and iOS with which you can create mobile apps for both the platforms by just using JavaScript.</p>
<!--more-->
<p>React Native was developed by some group of people working in the Facebook and later it was released as an open source project for all the world to use and contribute.</p>
<h3>Why should you care?</h3>
<p>Think about being in a hackathon and feeling the need of creating a mobile app for your solution. What should be your quickest solution for a mobile app? A native app would take a lot of work to just get it look good. That's where react-native kicks in. This is one such scenario, if you speak JS and have come from the world of the web then you might love it as well.</p>
<p>Apart from all of this, many bigger companies have started using react native for their production apps. You can see the complete list <a href="https://facebook.github.io/react-native/showcase.html">here</a>.</p>
<h3>Getting down to business</h3>
<p>The prerequisites of developing a react native application are very simple to begin with. Let's look at them one by one.</p>
<p><strong>Development Part</strong><br>
Step 1 - Install <a href="https://nodejs.org/en/download/">Node</a>.</p>
<p>Step 2 - Install Create React Native App using npm by running following command on a terminal.<br>
<p .lang:default .decode:true .crayon-inline>npm install -g create-react-native-app</p>
</p>
<p>Step 3 - Create a new react native app using the following command. <p .lang:default .decode:true .crayon-inline>newApp</p> can be any name you want to give your project.<br>
<p .lang:default .decode:true .crayon-inline>create-react-native-app newApp</p>
</p>
<p>Step 4 - Get inside the project directory and run the server.<br>
<p .lang:default .decode:true .crayon-inline>cd newApp && npm start</p>
</p>
<p>After the last step, you'll have to wait for some time until a QR code appears on the terminal window and then move on to the deployment part.</p>
<p><strong>Deployment Part<br>
</strong>Step 1 - Install <a href="https://expo.io/">Expo</a> application from <a href="https://play.google.com/store/apps/details?id=host.exp.exponent&referrer=www">Google Play Store</a> or <a href="https://itunes.apple.com/app/apple-store/id982107779?ct=www&mt=8">Apple App Store</a>.</p>
<p>Step 2 - Connect to the same network as you system onto which you completed development part.</p>
<p>Step 3 - Scan QR code form the terminal window using Expo app on your smartphone. You can also input the URL given below the QR code on the search box into the explore menu of expo app.</p>
<p>After the last step, the terminal window will also display the name of the device to which your server is connected. You'll see the expo icon which loads your application afterward on your smartphone which will look like the picture below. </p>
<div style="text-align:center">
<p><img alt="Output to Expo app after first run" class="aligncenter" src="https://i.imgur.com/W6gCzmR.png?1"></p>
</div>
<h3>Let's understand what just happened</h3>
<p>By running <p .lang:default .decode:true .crayon-inline>npm start</p> we started the node server, as well as some other magic processes who's job is mainly to gather all the code inside your project directory, reduce it into a single file namely <p .lang:default .decode:true .crayon-inline>index.android.bundle</p> or <p .lang:default .decode:true .crayon-inline>index.ios.bundle</p> for Android and iOS respectively. So all the code you have written inside your project directory which is nothing but a bunch of <p .lang:default .decode:true .crayon-inline>.js</p> files are only the ones you require at this point to define how your app looks like.</p>
<p>Once you scan the QR code using Expo app, the <p .lang:default .decode:true .crayon-inline>.bundle</p> is sent over to the smartphone with some other configuration files. Now Expo app has a JSCore which understands the js code you have written, maps to whichever native component you wanted your app to show with the appropriate style you provided.</p>
<p>Now, when you look into the contents of <p .lang:default .decode:true .crayon-inline>App.js</p> in out project directory, it would be like the one as shown below.</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">React</span> <span class="kn">from</span> <span class="s1">'react'</span><span class="p">;</span>
<span class="kn">import</span> <span class="p">{</span> <span class="n">StyleSheet</span><span class="p">,</span> <span class="n">Text</span><span class="p">,</span> <span class="n">View</span> <span class="p">}</span> <span class="kn">from</span> <span class="s1">'react-native'</span><span class="p">;</span>
<span class="n">export</span> <span class="n">default</span> <span class="k">class</span> <span class="nc">App</span> <span class="n">extends</span> <span class="n">React</span><span class="o">.</span><span class="n">Component</span> <span class="p">{</span>
<span class="n">render</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">(</span>
<span class="o"><</span><span class="n">View</span> <span class="n">style</span><span class="o">=</span><span class="p">{</span><span class="n">styles</span><span class="o">.</span><span class="n">container</span><span class="p">}</span><span class="o">></span>
<span class="o"><</span><span class="n">Text</span><span class="o">></span><span class="n">Open</span> <span class="n">up</span> <span class="n">App</span><span class="o">.</span><span class="n">js</span> <span class="n">to</span> <span class="n">start</span> <span class="n">working</span> <span class="n">on</span> <span class="n">your</span> <span class="n">app</span><span class="err">!</span><span class="o"></</span><span class="n">Text</span><span class="o">></span>
<span class="o"><</span><span class="n">Text</span><span class="o">></span><span class="n">Changes</span> <span class="n">you</span> <span class="n">make</span> <span class="n">will</span> <span class="n">automatically</span> <span class="n">reload</span><span class="o">.</</span><span class="n">Text</span><span class="o">></span>
<span class="o"><</span><span class="n">Text</span><span class="o">></span><span class="n">Shake</span> <span class="n">your</span> <span class="n">phone</span> <span class="n">to</span> <span class="nb">open</span> <span class="n">the</span> <span class="n">developer</span> <span class="n">menu</span><span class="o">.</</span><span class="n">Text</span><span class="o">></span>
<span class="o"></</span><span class="n">View</span><span class="o">></span>
<span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">const</span> <span class="n">styles</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">container</span><span class="p">:</span> <span class="p">{</span>
<span class="n">flex</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="n">backgroundColor</span><span class="p">:</span> <span class="s1">'#fff'</span><span class="p">,</span>
<span class="n">alignItems</span><span class="p">:</span> <span class="s1">'center'</span><span class="p">,</span>
<span class="n">justifyContent</span><span class="p">:</span> <span class="s1">'center'</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">};</span>
</code></pre></div>
<p>In the next post, we'll discuss each and every element of `App.js` file in-depth to get the most out of it.</p>
<h3>Prerequisites to learn further</h3>
<p>We'll break down the above code into different segments and I'll try to explain each one of them using a different blog. To understand the next set of blog posts in the series and get the most out of react native you should understand a little something of topics below.</p>
<p>1. ES6 - We'll be using goodness of a bunch of ES6 statements which will make our application simple and effective in fewer lines of code. Since we'll only deal with JavaScript side of things it'll be better for you to have an intermediate intellect in the same.</p>
<p>2. NodeJS - All the packages in react native are dealt with using NodeJS and npm packages. Get acquainted with how it works.</p>
<p>3. ReactJS (optional) - Entire framework is build using ReactJS to provide you with components having their own dedicated life cycle. It's optional since we'll go though how those components work ourselves and you'll learn to adore ReactJS starting from there.</p>
<p>I know this blog doesn't give it all away but this is how I wanted it to be, a spark so that I can explain to you all I've learned with a chain of posts instead of sharing some key elements in just one blog post. If you've any questions, please feel free to ask in the comments section below.</p>How to Make Bootable Linux Pendrive2016-01-01T17:04:00+05:302016-01-01T17:04:00+05:30sanyam_khuranatag:thegeekyway.com,2016-01-01:/how-to-make-bootable-linux-pendrive/<p>Well, you may not realize today, but it is very essential to have a Bootable Linux Pen Drive with you. Many times, your system may crash unexpectedly, or even your DVD ROM faces some problem or you might just want to dual boot Linux with Windows to give it a …</p><p>Well, you may not realize today, but it is very essential to have a Bootable Linux Pen Drive with you. Many times, your system may crash unexpectedly, or even your DVD ROM faces some problem or you might just want to dual boot Linux with Windows to give it a try? In these scenarios a bootable pen drive might come to rescue. It is the most easy and efficient way to install Linux on your computer. You may also use bootable drive to run a live image of the OS just like we have Live CD.</p>
<p><strong>LinuxLive USB Creator</strong></p>
<p>In this tutorial, we’ll learn how to make a bootable linux pen drive quickly and keep it ready to fight with unexpected tragedies that might occur with computers or even just to have a taste of new Linux Distros?<br>
Steps to Make Bootable Linux Live USB:</p>
<p>1) Download your favourite Linux Distro (.iso file) from their official site and keep it ready with you.</p>
<p>Quick links for Ubuntu, Fedora 20 (x86_64).</p>
<p>2) Go to LinuxLive USB Creator website and download it.</p>
<p>3) Now run the LinuxLive USB Creator as shown:</p>
<p>LinuxLive USB Creator steps</p>
<p>All the steps are indicated in the above image along with step no.</p>
<p>4) Select the USB drive/ CD (preferably do it on a USB) you want to install the image on.</p>
<p>5) Now choose the source of image file downloaded earlier. Alternatively, you can download the iso file in this step too or even select a CD as a source.</p>
<p>The system would analyze the image. This would take a few moments.</p>
<p>6) Now you can select the Persistence. Keep it more than 300 MB.</p>
<p>What is Persistence?</p>
<p>On Live USB or Live CD, after you reboot, all the preferences would be lost. The persistence data helps in storing these preferences so that they won’t be discarded after you reboot your live machine.</p>
<p>7) Now you can select three easy options as per your requirements:</p>
<p>Hide Created files on key.<br>
Format the key in FAT32.<br>
Enable launching LinuxLive in Windows (This would require Internet)</p>
<p>You should select all the three options.</p>
<p>8) Now click on the lightening button and the process will begin. It would take some time depending on the size of the OS. Once the process is finished, you have a bootable linux USB key/ CD with you.</p>
<p>Special Feature: If you install Ubuntu for instance, you’ll find wubi.exe file in your key which you can execute inside windows to set up Ubuntu.</p>
<p>Have you faced any problem in executing the above steps? or have something to say? Do let us know through the comments section below or give me a shout on Twitter \@ErSanyamKhurana</p>Understanding Complete Boot Process In Linux2014-09-11T14:48:00+05:302014-09-11T14:48:00+05:30sanyam_khuranatag:thegeekyway.com,2014-09-11:/understanding-complete-boot-process-linux/<p>Ever wondered, what happens behind the scenes to boot Linux? Knowing about what happens from the time you press the power button to the time when you get the login screen on Linux, i.e. boot process in Linux is very important. It can come very handy when you're debugging …</p><p>Ever wondered, what happens behind the scenes to boot Linux? Knowing about what happens from the time you press the power button to the time when you get the login screen on Linux, i.e. boot process in Linux is very important. It can come very handy when you're debugging why isn't a system booting properly or even when it refuses to boot completely.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/09/Linux_boot.png"><img alt="Boot Process in Linux" src="//thegeekyway.com/uploads/images/2014/09/Linux_boot.png"></a></p>
</div>
<p>So, let's start to understand the boot process in Linux in a step-by-step manner.</p>
<h2>1) POST (POWER ON SELF TEST)</h2>
<p>When you press the Power Button, the <em>POST operation (Power on Self Test)</em> occurs when <em>BIOS (Basic Input / Output System)</em> initializes the hardware, testing the main memory of your system and looking for all other hardware device including keyboard, screen, mouse etc. The BIOS is actually stored on the <em>ROM ( Read Only Memory)</em> on the Motherboard and after BIOS, all the operation is controlled by the Operating System itself.</p>
<h2>2) Boot Loader - First & Second stage Boot Loader</h2>
<p>The control from BIOS is then passed to the Boot Loader once the POST operation is completed as explained above. The Boot Loader is actually stored in the boot sector, often the first sector of your disk for BIOS or <strong><em>MBR (Master Boot Records) System</em></strong> . The boot loader then searches for a bootable partition, and once it finds the bootable partition, it transfers control to (second stage) boot loader like GRUB and then loads it into the memory. This process can be carried out by <strong><em>EFI partition for Unified Extensible Firmware Interface (UEFI) systems.</em></strong> These systems reads the Boot Manager data to determine which UEFI application to load and from which partition on the disk. And then it launches the UEFI application like GRUB. This method is complicated yet versatile than the previous MBR method.</p>
<p>The second stage boot loader is found in /boot directory and it presents a UI (splash screen) to the user where they can choose which OS to boot, in case of Multi-Boot system. The main aim of the boot loader is to lead kernel image along with initializing the RAM to load all the device drivers needed to start the system. There are many boot loaders available for Linux. However the most important one include <em>GRUB (Grand Unified Boot Loader) & ISOLINUX</em>.</p>
<h2>3) CMOS comes into play</h2>
<p>Then the most crucial information about peripherals and current date and time are then loaded form CMOS, which is actually a battery-powered small store capable of storing and keeping track of date and time of the system even when it is turned off.</p>
<h2>4) initramfs & Kernel loaded</h2>
<p>The boot loader loads the <strong><em>initramfs (initial RAM-based file system)</em></strong> and kernel into the main memory. Once the kernel is loaded into the memory, it configures the hardware that is attached to the system and loads important applications for users.</p>
<p>The initramfs filesystem consists of Program and binary files which is essential for mounting proper root filesystem, providing kernel functionality, locating of devices, locating of drivers for devices and loading them and checking of errors in the root filesystem.</p>
<p>The mount system actually instructs if the filesystem is ready to be used and then associates it with particular point in the overall hierarchy of the filesystem or the mount point. If this step is successfully carried out, then the initramfs would be cleared out from the RAM and then the init program from <strong>/sbin/init</strong> is executed. The init actually handles the mounting and pivoting of the final root system. This is the initial process of the system which then helps in starting other processes to get system running. If there are special device drivers needed to be accessed, then they must be included in the initramfs image.</p>
<hr>
<p>When you trace the path of origin for the processes, you’ll find all of them originated from init itself expect the kernel process which are started by kernel itself for managing of internal operating system details.</p>
<hr>
<h2>5) Runlevels in Linux</h2>
<p>In the traditional System V Unix, there was a sequence of runlevels containing different scripts to start and stop services. There are a total of 6 runlevels:</p>
<p>Runlevel 0</p>
<p>Runlevel 1</p>
<p>Runlevel 2</p>
<p>Runlevel 3</p>
<p>Runlevel 4</p>
<p>Runlevel 5</p>
<p>Within each runlevel, user can set each individual services to run or to shut down (We would understand about RunLevels in detail in the next post). Most distributions are now moving away from this standard of System V standard. init is responsible for system running and shutting it down for cleaning up the non-kernel processes whenever necessary. It also restarts the login services for users as needed when users login and logout.</p>
<h2>6) Text-Mode Login</h2>
<p>Next, there is a text-mode login prompt started by init (done by program getty). After entering the username and password, user is offered with a command shell which is often <strong><em>(Bourne After Shell) BASH</em></strong>, in most of the linux distros which is ready to take in commands and then execute the operation associated with each command.</p>
<h2>7) X Window System</h2>
<p>At the end of the boot process, the X Windows system is loaded which starts a display manager, loads x server for the x clients and manage graphical logins by starting with a desktop environment like Gnome, KDE etc. depending on the system.</p>
<hr>
<p>And Linux is now up and running for use. I hope this would be bit informative for you and would help you. Please let us know your thoughts through comments below.</p>How To Save Logs In IRC using XChat2014-08-24T22:21:00+05:302014-08-24T22:21:00+05:30sanyam_khuranatag:thegeekyway.com,2014-08-24:/save-logs-irc-using-xchat/<p>Howdy Readers! Wonder if you're facing any problem regarding any FOSS project, the first thing you'll do is Google it to solve your problem. But sometime it can happen, you couldn't find it on the net, may be you discovered a bug? or let's say you just want to contribute …</p><p>Howdy Readers! Wonder if you're facing any problem regarding any FOSS project, the first thing you'll do is Google it to solve your problem. But sometime it can happen, you couldn't find it on the net, may be you discovered a bug? or let's say you just want to contribute or discuss about any FOSS Projects. You can communicate directly with Developers of that specific project mainly via two mediums, IRC (Internet Relay Chat) or Mailing List. But don't just rush to mailing list, first ask on IRC itself, if you didn't get any reply or no one is available, then you can drop a mail to the mailing list.</p>
<p>There are various Clients you can use for IRC, and sometimes it's crucial to have logs with you. They can be important to refer back in near future, about what conversation you had. So, today, we'll see how you can save logs via <strong>xChat (an IRC client)</strong>.</p>
<h2><strong>Here is a quick step by step guide to save your logs in xChat:</strong></h2>
<p>Open up XChat, on whatever OS you are using. Currently, this tutorial would include images from Fedora.</p>
<p>Connect to any Server and channel of your choice.</p>
<p>From the Menu at the Title Bar, select Settings -> Preferences.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/Preferences_XChat.png" title="Select Settings -> Preferences from xchat"><img alt="Select Settings -> Preferences from xchat" src="//thegeekyway.com/uploads/images/2014/08/Preferences_XChat.png"></a></p>
</div>
<p>A window will pop up like this.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/Logging_xchat.png" title="Selecting Logging Option from left window pane"><img alt="Selecting Logging Option from left window pane" src="//thegeekyway.com/uploads/images/2014/08/Logging_xchat.png"></a></p>
</div>
<p>Now select the Logging tab from the Left pane, and you'll see something like the one shown below. Now select the option of "<u><strong>Enable Logging of conversations to Disk</strong></u>".</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/enablelogging_xchat.png" title="Enabling logging in xChat"><img alt="Enabling logging in xChat" src="//thegeekyway.com/uploads/images/2014/08/enablelogging_xchat.png"></a></p>
</div>
<p>(Optional): You can select how the name of the log file should be by entering in the text box shown above.</p>
<p>You can use these options to save the logs : <strong>%s = server, %n = Network, %c = Channel</strong>, or just leave the default and you're good to go.</p>
<p>That's it !</p>
<p>Now you can see all your logs via the <strong>Open Data Folder</strong> button.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/xchat_enable_login.png" title="View the logs by clicking on Open Data Folder"><img alt="View the logs by clicking on Open Data Folder" src="//thegeekyway.com/uploads/images/2014/08/xchat_enable_login.png"></a></p>
</div>
<p>The logs would be saved exactly in the format you chose.</p>
<p>If you have any difficulty in the procedure, please leave a comment below. We'll help you at the earliest.</p>Structures And User Defined Data Types (Part - I)2014-08-05T00:17:00+05:302014-08-05T00:17:00+05:30sanyam_khuranatag:thegeekyway.com,2014-08-05:/structures-c-programming-language-user-defined-data-types/<p>Howdy folks, hope you're doing well. We've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D Arrays and their memory organization</a> and further about <a href="http://www.thegeekyway.com/learning-2d-multidimensional-arrays/" title="Learning About 2D And Multidimensional Arrays">2D arrays and multidimensional arrays</a>. In case you missed it, read them first. Coming to this tutorial, we'll be discussing the way to develop our own data types in the C …</p><p>Howdy folks, hope you're doing well. We've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D Arrays and their memory organization</a> and further about <a href="http://www.thegeekyway.com/learning-2d-multidimensional-arrays/" title="Learning About 2D And Multidimensional Arrays">2D arrays and multidimensional arrays</a>. In case you missed it, read them first. Coming to this tutorial, we'll be discussing the way to develop our own data types in the C programming language. It is done through something known as structures, and that's what we'll explore about here.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/08/structures-and-user-defined-data-types-I.png" title="Structure and User Defined Data Types in C Programming"><img alt="Structure and User Defined Data Types in C Programming" src="//thegeekyway.com/uploads/images/2014/08/structures-and-user-defined-data-types-I.png"></a></p>
</div>
<h2>What is a structure in C ?</h2>
<p>Structure is basically a derived data type in C programming language. It is defined as a collection of variables (which may be of different data types) to be grouped together and referred to as a single entity. This gives you ability to define your own data types i.e. user defined data types.</p>
<h2>Difference between a structure and an array?</h2>
<p>The next question which may arise, would be what's the difference between array and structure. Well, an array is homogeneous set of elements i.e. array is capable of holding different values but of same data type. On the other hand, structure is combination of variables of different data types.</p>
<p>Structures can be used to track information which is common to large group of entities.</p>
<p><strong>How to declare a structure?</strong></p>
<p>Let us first see the basic semantics of declaring a structure. It is much like defining ordinary variable like:</p>
<div class="highlight"><pre><span></span><code><span class="o"><</span><span class="n">data_type</span><span class="o">></span><span class="w"> </span><span class="o"><</span><span class="n">variable_name</span><span class="o">></span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">For</span><span class="w"> </span><span class="n">variable</span>
</code></pre></div>
<p>Now for structure, it is done through keyword struct like this:</p>
<div class="highlight"><pre><span></span><code>struct <struct_name>
{
...
};
</code></pre></div>
<p>Let us take an example to track record of students in a high school. Each student would have certain set of properties/attributes which would differentiate them from one another. We'll consider a student has a roll number of type integer , name of type string and marks of type float for the time being. Now, let's define our structure named student with these properties.</p>
<div class="highlight"><pre><span></span><code>struct student
{
int rollno;
char name[20]; // Taking an array of character (string)
float marks;
// You are free to declare as many members as you want, but we're restricting our example to 3 data members (properties)
// Size of the structure should be known to the compiler
};
</code></pre></div>
<p>Now that we have our basic structure with it's data members ready, we can declare instances of it like:</p>
<div class="highlight"><pre><span></span><code>struct student George;
</code></pre></div>
<p>Now, all members of the structure can be accessed via our instance named George using membership access or dot operator like:</p>
<div class="highlight"><pre><span></span><code>George.rollno = 91;
George.marks = 78.6;
</code></pre></div>
<p>and so on...</p>
<p>It's essential to remember here that the members we declare in a structure would take the memory only when instances are declared. You're free to name your instance anything. It's just like declaring a variable whose data type would be now your own defined data type, which is struct student here.</p>
<p>Notice above how we've defined the instances of a structure, another way of declaring them can be like this:</p>
<div class="highlight"><pre><span></span><code><span class="n">struct</span><span class="w"> </span><span class="o"><</span><span class="n">struct</span><span class="o">-</span><span class="n">name</span><span class="o">></span>
<span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Data</span><span class="w"> </span><span class="n">Members</span>
<span class="p">}</span><span class="w"> </span><span class="o"><</span><span class="n">name</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">structure</span><span class="o">-</span><span class="n">variable</span><span class="o">></span><span class="w"> </span><span class="o">...</span><span class="w"> </span><span class="p">;</span>
</code></pre></div>
<p>So, for the above example, it can be done as follows:</p>
<div class="highlight"><pre><span></span><code>struct student
{
int rollno;
char name[20]; // Taking an array of character (string)
float marks;
}George,Henry,Steve; // Declare as many as you want.
</code></pre></div>
<p>Next, you can access each of the properties for a particular structure variable like :</p>
<div class="highlight"><pre><span></span><code>George.rollno = 50;
George.marks = 98.6;
strycpy(George.name, "George");
</code></pre></div>
<p>Similarly all other structure variables' data members can be defined.</p>
<p>That's solve for this tutorial, a lot more is there to know about structure, which we'll cover in the second part.</p>Learning About 2D And Multidimensional Arrays2014-07-24T13:04:00+05:302014-07-24T13:04:00+05:30sanyam_khuranatag:thegeekyway.com,2014-07-24:/learning-2d-multidimensional-arrays/<p>Well, I'm glad you reached at this point, we've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D arrays and the memory organization</a>, and in this post, we'll know about 2D arrays and Multidimensional Arrays. They are not very much different from 1 D arrays as we looked in the last tutorial. So, let's start …</p><p>Well, I'm glad you reached at this point, we've already discussed about <a href="http://www.thegeekyway.com/learning-1-dimensional-arrays/" title="Learning About 1 Dimensional Arrays (1 D Arrays)">1D arrays and the memory organization</a>, and in this post, we'll know about 2D arrays and Multidimensional Arrays. They are not very much different from 1 D arrays as we looked in the last tutorial. So, let's start:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/2D_mutlidimensional_arrays.png" title="Learning about 2D and multidimensional arrays in C"><img alt="Learning about 2D and multidimensional arrays in C" src="//thegeekyway.com/uploads/images/2014/07/2D_mutlidimensional_arrays.png"></a></p>
</div>
<p>In this tutorial, we'll cover the following topics:</p>
<ul>
<li>
<p><a href="http://www.TheGeekyWay.com/learning-2d-multidimensional-arrays/#2d_arrays"><strong><em>2 Dimensional Arrays (2D Arrays)</em></strong></a></p>
<ul>
<li><a href="http://www.TheGeekyWay.com/learning-2d-multidimensional-arrays/#2d_declare"><strong><em>Declaration of 2D Arrays</em></strong></a></li>
<li><a href="#2d_input"><strong><em>Inputting Data in 2D Arrays</em></strong></a></li>
<li><a href="#2d_output"><strong><em>Traversing and Output from 2D Array</em></strong></a></li>
</ul>
</li>
<li>
<p><a href="#multidimensional_arrays"><strong><em>Multidimensional Arrays</em></strong></a></p>
<ul>
<li><a href="#multidimensional_declare"><strong><em>Declaration of Multidimensional Arrays</em></strong></a></li>
<li><a href="#multidimensional_input"><strong><em>Inputting Data in Multidimensional Arrays</em></strong></a></li>
<li><a href="#multidimensional_output"><strong><em>Traversing and output from Multidimensional Arrays</em></strong></a></li>
</ul>
</li>
</ul>
<h2 id="dimensional-arrays" style="text-align: center;"><strong>2 Dimensional Arrays</strong></h2>
<p>Without saying anything much about 2D arrays here, let's first have a look at the memory organization in 2D arrays:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/2DArrays_Representation.png" title="Memory Organization of 2D Arrays in memory"><img alt="Memory Organization of 2D Arrays in memory" src="//thegeekyway.com/uploads/images/2014/07/2DArrays_Representation.png"></a></p>
</div>
<p>Clearly, it's much like 1D arrays but this time the contiguous memory blocks have to be occupied in two directions considered as along x and y co-ordinates.</p>
<p>With this basic memory organization picture in mind, let's now see how we declare a 2D Array.</p>
<p #2d_declare></p>
<p><strong>Declaration of 2D Array:</strong></p>
<p>Let's take an example that you want to create a 3 x 3 Matrix named mat of integer data type, so we would declare as:</p>
<div class="highlight"><pre><span></span><code>int mat[3][3];
</code></pre></div>
<p>The first number in square brackets is for the number of rows and the next one is for number of columns. For instance, now say we need to create a matrix of say 4 rows and 6 columns, of float data type. it would look like this:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/mat_4x6.png" title="Matrix memory representation 4 X 6"><img alt="Matrix memory representation 4 X 6" src="//thegeekyway.com/uploads/images/2014/07/mat_4x6.png"></a></p>
</div>
<p>Now, it would be declared as follows:</p>
<div class="highlight"><pre><span></span><code>float mat[4][6];
</code></pre></div>
<p>Now let us come back to our original mat, which is a 3 x 3 matrix and see how to fill it.</p>
<p #2d_input></p>
<p><strong>Putting in data to 2D Arrays:</strong></p>
<p>Since we need to refer a location by both the row number and the column number, we need to fill in each block of memory by traversing through each row first and then each column in that row, then come to next row, traverse each column and so on...</p>
<p>Here we would need two loops, one for updating the row number and the next to update the column number. Since we are moving from left to right for each row, remember that we will update the row value less frequently than the column value.</p>
<p>So, basic syntax would be something like this:</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="k">row</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">col</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="o">&</span><span class="n">matrix</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>Here row and col would correspond to the total number of rows and columns in the matrix. Inside the loop, we're just taking input from the user such that the value of <strong>j</strong> i.e. our column would update more frequently from left to right than the value of <strong>i</strong> i.e. our row.</p>
<p>Now, let's start accepting values from user for our for our matrix named mat which is a 3 x 3 matrix.</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">mat</span><span class="o">[</span><span class="n">3</span><span class="o">][</span><span class="n">3</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="n">D</span><span class="w"> </span><span class="n">matrix</span>
<span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="n">variables</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">used</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">loop</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">updating</span><span class="w"> </span><span class="k">row</span><span class="w"> </span><span class="n">number</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">updating</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="n">number</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="o">&</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p #2d_output></p>
<p><strong>Traversing and Printing 2D Array:</strong></p>
<p>Next, we need to traverse this array, it would be done as:</p>
<p>Traversing means just processing through each element of the array and apply an operation.</p>
<div class="highlight"><pre><span></span><code><span class="o">//</span><span class="w"> </span><span class="n">We</span><span class="w"> </span><span class="n">have</span><span class="w"> </span><span class="n">assumed</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">initialized</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="k">as</span>
<span class="o">//</span><span class="w"> </span><span class="n">shown</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">code</span><span class="w"> </span><span class="n">snippet</span><span class="w"> </span><span class="n">above</span><span class="p">.</span>
<span class="o">//</span><span class="w"> </span><span class="n">Traversing</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Traversing</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">applying</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">printing</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">console</span><span class="w"> </span><span class="k">operation</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<h2 id="multidimensional-array" style="text-align: center;"><strong>Multidimensional Array :</strong></h2>
<p>Extending what we've learned about 1D and 2D arrays, there can be 3D, 4D, 5D and even 100D arrays, though the complexity would multiply at each level, so we don't use beyond 2D arrays generally. In some complex application, we may use 3D arrays, but beyond that, because of the increase in the complexity, they are hardly used.</p>
<p #multidimensional_declare></p>
<p><strong>Declaration of Multidimensional Arrays:</strong></p>
<p>So, for declaring a 3 D array, you just have to define one more dimension as follows:</p>
<div class="highlight"><pre><span></span><code>int matrix [3][3][3]; //Declaring 3D Array which is 3 x 3 x 3
</code></pre></div>
<p>Similarly, for a 4 D array, declaration would be :</p>
<div class="highlight"><pre><span></span><code>int matrix [3][4][5][6]; // Declaring Matrix named matrix of type int and
// size 3 x 4 x 5 x 6
</code></pre></div>
<p>So, generally, whatever dimension you want, you can add while declaring.</p>
<p #multidimensional_input></p>
<p><strong>Putting in data to Multidimensional Array:</strong></p>
<p>Very similar to filling up a 2 D array, a 3D array can be filled up by using 3 loops (one for iterating through each dimension). Remember, the inner loop will always has a fast frequency to iterate than the loop enclosing it and so on...</p>
<p>Let's fill up a 3D array of type float here as an example:</p>
<div class="highlight"><pre><span></span><code><span class="nc">float</span><span class="w"> </span><span class="n">mat</span><span class="o">[</span><span class="n">3</span><span class="o">][</span><span class="n">4</span><span class="o">][</span><span class="n">5</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Declaring</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="n">mat</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="nc">float</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="n">dimensions</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="mi">5</span>
<span class="o">//</span><span class="w"> </span><span class="n">Filling</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="k">data</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">taking</span><span class="w"> </span><span class="k">values</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="k">corresponding</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">dimension</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%f"</span><span class="p">,</span><span class="o">&</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">][</span><span class="n">k</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>Similarly, just increase the number of loops depending on the dimensions. Thus, if you have created an N dimension array, it would take N loops to fill it.</p>
<p #multidimensional_output></p>
<p><strong>Traversing and Output of a Multidimensional Array:</strong></p>
<p>In the same way as explained above, you can print a multidimensional array by iterating through it using the same no. of loops as the dimension of the array as:</p>
<div class="highlight"><pre><span></span><code><span class="o">//</span><span class="w"> </span><span class="n">This</span><span class="w"> </span><span class="n">snippet</span><span class="w"> </span><span class="n">assumes</span><span class="w"> </span><span class="n">that</span><span class="w"> </span><span class="n">we</span><span class="err">'</span><span class="n">ve</span><span class="w"> </span><span class="n">declared</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">filled</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">shown</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">above</span><span class="p">.</span>
<span class="o">//</span><span class="w"> </span><span class="n">Traversing</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">output</span>
<span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">Taking</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="k">corresponding</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">dimension</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%f"</span><span class="p">,</span><span class="n">mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">][</span><span class="n">k</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>I hope this had been a bit informative for you and would help you. Next, we'll be going to discuss about Structures in C language.</p>
<p>Have any doubts? Just leave a comment below or give me a shout on twitter \@ErSanyamKhurana</p>ASP.Net Basics : Populate checkboxlist from database using C#2014-07-19T12:31:00+05:302014-07-19T12:31:00+05:30Deep_Singhtag:thegeekyway.com,2014-07-19:/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/<p>In this tutorial we will be discussing how to populate data in checkboxlist or radiobutton list from the values taken from database and display them on the screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp-net11.jpg" title="Populate checkboxlist from database using C#"><img alt="Populate checkboxlist from database using C#" src="//thegeekyway.com/uploads/images/2014/03/asp-net11.jpg"></a></p>
</div>
<p>Let us assume a database with following table definitions (from my <a class="external" href="http://myCampusNotes.com">next project</a>) and table values. </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp1.png"><img alt="populate checkboxlist from database using c# and .net" src="//thegeekyway.com/uploads/images/2014/03/asp1.png"></a> </p>
</div>
<p>and the output should be</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp2.png"><img alt="populate checkboxlist from database using c# and .net" src="//thegeekyway.com/uploads/images/2014/03/asp2.png"></a></p>
</div>
<p>Various …</p><p>In this tutorial we will be discussing how to populate data in checkboxlist or radiobutton list from the values taken from database and display them on the screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp-net11.jpg" title="Populate checkboxlist from database using C#"><img alt="Populate checkboxlist from database using C#" src="//thegeekyway.com/uploads/images/2014/03/asp-net11.jpg"></a></p>
</div>
<p>Let us assume a database with following table definitions (from my <a class="external" href="http://myCampusNotes.com">next project</a>) and table values. </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp1.png"><img alt="populate checkboxlist from database using c# and .net" src="//thegeekyway.com/uploads/images/2014/03/asp1.png"></a> </p>
</div>
<p>and the output should be</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp2.png"><img alt="populate checkboxlist from database using c# and .net" src="//thegeekyway.com/uploads/images/2014/03/asp2.png"></a></p>
</div>
<p>Various steps involved in populating data from database to CheckBoxList (You can directly jump to that link by clicking on the following links)</p>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_1">Step 1 : Select the checkBoxList control from the toolbox.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_2">Step 2 : On the C# side coding create a SQL connection.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_3">Step 3 : Write your SQL Query.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_4">Step 4 : Create a SQL Adapter.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_5">Step 5 : Create a Dataset.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_6">Step 6 : Fill the dataset with the values from the SqlDataAdapter.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_7">Step 7 : Check if the resultant dataset produce rows or not.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_8">Step 8 : Provide the dataSet to the Checkbox list.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_9">Step 9 : Bind the data of the checkboxlist.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_10">Step 10: The final step : Provide the DataTextField and DataValueField to the checkbox list.</a></h5>
<h5><a href="http://www.thegeekyway.com/asp-net-basics-populate-checkboxlist-database-using-c-dotnet/#h_4_11">Step 11: The complete code</a></h5>
<hr>
<h5><p #h_4_1>Step 1 : <p style="text-decoration: underline; color: black;">Select the checkBoxList control from the toolbox.</p></p></h5>
<p>You can get to the toolbox by pressing ctrl W,X and typing checkBoxList you can drag drop the checkBoxList control on the aspx page. You will get the following code :-</p>
<div class="highlight"><pre><span></span><code><span class="nt"><asp:CheckBoxList</span><span class="w"> </span><span class="na">ID=</span><span class="s">"CheckBoxList1"</span><span class="na">runat=</span><span class="s">"server"</span><span class="nt">></asp:CheckBoxList></span>
</code></pre></div>
<p><strong>ID = “CheckBoxList1″</strong> means that the CheckBoxList has been allotted a specific ID so that we can work on the C# page using that reference.<br>
<strong>runat=”server”</strong> means that the code will run on the server side (i.e. on the C# end) also, if we remove the code then we are left with the client side scripting only.</p>
<hr>
<h5><p #h_4_2>Step 2 : <p style="text-decoration: underline; color: black;">On the C# side coding create a SQL connection.</p></p></h5>
<p>Sql connection can be created using the following asp (C#) snippet</p>
<div class="highlight"><pre><span></span><code>using System.Data.SqlClient;
SqlConnection con=newSqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].Conn
</code></pre></div>
<p>and in web.config add the following lines under configurations tab</p>
<div class="highlight"><pre><span></span><code><span class="nt"><connectionStrings></span>
<span class="w"> </span><span class="nt"><add</span><span class="w"> </span><span class="na">name=</span><span class="s">"ConnectionString"</span><span class="na">connectionString=</span><span class="s">"Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|(your database name);Integrated Security=True"</span>
<span class="w"> </span><span class="na">providerName=</span><span class="s">"System.Data.SqlClient"</span><span class="nt">/></span>
<span class="w"> </span><span class="nt"></connectionStrings></span>
</code></pre></div>
<p>you need to include the System.Data.SqlClient class inorder to work with sql.</p>
<hr>
<h5><p #h_4_3>Step 3 : <p style="text-decoration: underline; color: black;">Write your SQL Query.</p></p></h5>
<p>The next step after creating the connection is to open the connection we can use con.open() [here con is the object of SqlConnection class created in Step2]. The SQL query for this table will be :-</p>
<div class="highlight"><pre><span></span><code><span class="n">stringquery</span><span class="o">=</span><span class="ss">"SELECT distinct[Clg_name] FROM [Complete_table]"</span><span class="p">;</span>
</code></pre></div>
<hr>
<h5><p #h_4_4>Step 4 : <p style="text-decoration: underline; color: black;">Create a SQL Adapter.</p></p></h5>
<p>SQL adapter is derived from calss System.Data.SqlClient.SqlDataAdapter which represents a set of data commands and a database connection that are used to fill the System.Data.Dataset and update a SQL Server database. This class cannot be inherited.</p>
<p>
The following C\# snippet creates a SQL adapter
<textarea class="crayon-plain print-no" style="-moz-tab-size: 4; font-size: 16px ! important; line-height: 16px ! important; opacity: 0; overflow: hidden; z-index: 0;" readonly="readonly" wrap="soft" data-settings="dblclick">
</textarea>
</p>
<div class="highlight"><pre><span></span><code>SqlDataAdapter da=newSqlDataAdapter(query,con);
</code></pre></div>
<hr>
<h5><p #h_4_5>Step 5 : <p style="text-decoration: underline; color: black;">Create a Dataset.</p></p></h5>
<p>Dataset represents an in-memory cache of data, i.e. the data which is being retrieved from database and stored into checkboxlist is being stored in cache and data is retreived from cache using Dataset.</p>
<p>The C# snippet to create a Dataset is as follows:</p>
<div class="highlight"><pre><span></span><code>DataSet ds=newDataSet();
</code></pre></div>
<hr>
<h5><p #h_4_6>Step 6 : <p style="text-decoration: underline; color: black;">Fill the dataset with the values from the SqlDataAdapter.</p></p></h5>
<p>Now we need to fill in the data from sql dataAdapter using dataset, the C# snippet for the same is :-</p>
<div class="highlight"><pre><span></span><code>da.Fill(ds);
</code></pre></div>
<hr>
<h5><p #h_4_7>Step 7 : <p style="text-decoration: underline; color: black;">Check if the resultant dataset produce rows or not.</p></p></h5>
<p>We can check if the dataset which populates checkboxlist from database produce rows or not, if it doesnt produce rows we can simply give an error message that no results available.<br>
Here is the C# snippet for the same :-</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">ds</span>.<span class="nv">Tables</span>[<span class="mi">0</span>].<span class="nv">Rows</span>.<span class="nv">Count</span><span class="o">!=</span><span class="mi">0</span><span class="ss">)</span>
{
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="nv">more</span><span class="w"> </span><span class="nv">coding</span><span class="w"> </span><span class="nv">will</span><span class="w"> </span><span class="nv">be</span><span class="w"> </span><span class="nv">discussed</span><span class="w"> </span><span class="nv">in</span><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="nv">steps</span>
}<span class="k">else</span>{
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="nv">Give</span><span class="w"> </span><span class="nv">an</span><span class="w"> </span><span class="nv">error</span><span class="w"> </span><span class="nv">message</span><span class="w"> </span><span class="nv">that</span><span class="w"> </span><span class="nv">no</span><span class="w"> </span><span class="nv">results</span><span class="w"> </span><span class="nv">found</span>
}
</code></pre></div>
<hr>
<h5><p #h_4_8>Step 8 : <p style="text-decoration: underline; color: black;">Provide the dataSet to the Checkbox list.</p></p></h5>
<p>Next step is to point the checkBoxList towards the SQL database for that we will use the dataset to point to the database. Here is the following C# snippet.</p>
<div class="highlight"><pre><span></span><code>CheckBoxList1.DataSource=ds;
</code></pre></div>
<hr>
<h5><p #h_4_9>Step 9 : <p style="text-decoration: underline; color: black;">Bind the data of the checkboxlist.</p></p></h5>
<p>Now the next and the second last step is to bind the data. It can be done using following C# snippet</p>
<div class="highlight"><pre><span></span><code>CheckBoxList1.DataBind();
</code></pre></div>
<p>on writing till here we get the following output :-</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/asp4.png"><img alt="populate checkboxlist from database using c# and .net" src="//thegeekyway.com/uploads/images/2014/03/asp4.png"></a></p>
</div>
<hr>
<h5><p #h_4_10>Step 10: <p style="text-decoration: underline; color: black;">The final step : Provide the DataTextField and DataValueField to the checkbox list.</p></p></h5>
<p>Here the DataTextField and DataValueField is the name of the column in our database, the C# snippet for the same is :-</p>
<div class="highlight"><pre><span></span><code>CheckBoxList1.DataTextField="Clg_Name";
CheckBoxList1.DataValueField="Clg_Name";
</code></pre></div>
<p>Thats it, the data in database will be binded to checkbox list and will be displayed.</p>
<hr>
<h5><p #h_4_11>Step 11: <p style="text-decoration: underline; color: black;">The complete code.</p></p></h5>
<p>here is the complete code :-</p>
<div class="highlight"><pre><span></span><code><span class="n">protectedvoidPage_Load</span><span class="p">(</span><span class="n">objectsender</span><span class="p">,</span><span class="n">EventArgse</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">SqlConnection</span><span class="w"> </span><span class="n">con</span><span class="o">=</span><span class="n">newSqlConnection</span><span class="p">(</span><span class="k">System</span><span class="p">.</span><span class="n">Web</span><span class="p">.</span><span class="n">Configuration</span><span class="p">.</span><span class="n">WebConfigurationManager</span><span class="p">.</span><span class="n">ConnectionStrings</span><span class="o">[</span><span class="n">"ConnectionString"</span><span class="o">]</span><span class="p">.</span><span class="n">ConnectionString</span><span class="p">);</span>
<span class="w"> </span><span class="n">stringquery</span><span class="p">;</span>
<span class="w"> </span><span class="k">try</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">query</span><span class="o">=</span><span class="ss">"SELECT distinct[Clg_name] FROM [Complete_table]"</span><span class="p">;</span>
<span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="k">Open</span><span class="p">();</span>
<span class="w"> </span><span class="n">SqlDataAdapter</span><span class="w"> </span><span class="n">da</span><span class="o">=</span><span class="n">newSqlDataAdapter</span><span class="p">(</span><span class="n">query</span><span class="p">,</span><span class="n">con</span><span class="p">);</span>
<span class="w"> </span><span class="n">DataSet</span><span class="w"> </span><span class="n">ds</span><span class="o">=</span><span class="n">newDataSet</span><span class="p">();</span>
<span class="w"> </span><span class="n">da</span><span class="p">.</span><span class="n">Fill</span><span class="p">(</span><span class="n">ds</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">ds</span><span class="p">.</span><span class="n">Tables</span><span class="o">[</span><span class="n">0</span><span class="o">]</span><span class="p">.</span><span class="k">Rows</span><span class="p">.</span><span class="nf">Count</span><span class="o">!=</span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">CheckBoxList1</span><span class="p">.</span><span class="n">DataSource</span><span class="o">=</span><span class="n">ds</span><span class="p">;</span>
<span class="w"> </span><span class="n">CheckBoxList1</span><span class="p">.</span><span class="n">DataTextField</span><span class="o">=</span><span class="ss">"Clg_Name"</span><span class="p">;</span>
<span class="w"> </span><span class="n">CheckBoxList1</span><span class="p">.</span><span class="n">DataValueField</span><span class="o">=</span><span class="ss">"Clg_Name"</span><span class="p">;</span>
<span class="w"> </span><span class="n">CheckBoxList1</span><span class="p">.</span><span class="n">DataBind</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Response</span><span class="p">.</span><span class="k">Write</span><span class="p">(</span><span class="ss">"No Results found"</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">catch</span><span class="p">(</span><span class="k">Exception</span><span class="w"> </span><span class="n">ex</span><span class="p">)</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Response</span><span class="p">.</span><span class="k">Write</span><span class="p">(</span><span class="ss">"<br>"</span><span class="o">+</span><span class="n">ex</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="n">finally</span>
<span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="k">Close</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>Have any doubt or stuck at any step? Feel free to ask in the comments section below.</p>Android Activity LifeCycle2014-07-18T00:35:00+05:302014-07-18T00:35:00+05:30gagan93tag:thegeekyway.com,2014-07-18:/android-activity-lifecycle/<p><strong>Activity</strong> is the base class of an Android <strong>Activity (package android.app). </strong>An Activity represents a view to the user. For eg. When we open Messaging app to write a new message, it is an Activity. After writing the message, when we open contacts to select the person, it can …</p><p><strong>Activity</strong> is the base class of an Android <strong>Activity (package android.app). </strong>An Activity represents a view to the user. For eg. When we open Messaging app to write a new message, it is an Activity. After writing the message, when we open contacts to select the person, it can be considered as another activity and so on. So in simpler words, anything which completely occupies the screen and is different from the previous screen is called an Activity. In this tutorial we'll get to know about Android Activity Lifecycle.</p>
<div style="text-align:center">
<p><a href="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/Example_sketch.jpg"><img alt="Example_sketch" src="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/Example_sketch.jpg"></a></p>
</div>
<p>Two screens showing different activities</p>
<p>As shown in previous tutorials, each android activity <strong>extends </strong>the <strong>android.app.Activity </strong>class. Activity is a concrete class and contains many methods which can be overwritten so that the activity behaves as per our choice. Such methods are called as lifecycle methods (discussed below).</p>
<p><strong>Note :</strong> Each new activity must extend the Activity class (or its direct subclasses) and have corresponding entry in the AndroidManifest.xml file as shown below</p>
<div style="text-align:center">
<p><a href="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/12.jpg"><img alt="1" src="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/12.jpg"></a></p>
</div>
<p>There are many methods to go from one activity to another. Also, we can either choose to <strong>finish </strong>the previous activity, or leave it as it is.</p>
<p>LifeCycle of Android Activity</p>
<div style="text-align:center">
<p><a href="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/rVnSi.jpg"><img alt="rVnSi" src="http://blog.mycampusnotes.com/wp-content/uploads/2014/07/rVnSi.jpg"></a></p>
</div>
<p>Source : Android Developers Website</p>
<p>Some of you may have seen this screenshot in various books or on websites. Lifecycle is a fundamental concept in android which consists of many methods from <strong>Activity</strong> class. Any practical application uses at least one lifecycle method. <strong>onCreate() </strong>is one such method. People knowing about <strong>Java Applets </strong>must be knowing what a lifecycle methods are, for others you’ll get to know shortly.</p>
<p>The image shown above gives an overview of basic lifecycle methods. When an application starts, few of them are called. When it exits, different set of methods are called. Similarly when you navigate from one activity to another or come back, some methods are called. Below we have summarized what all methods are called in which situations.</p>
<p>(The parameters, return types and access specifiers are not shown for now. )</p>
<p>Below I have summarized what all methods are called in which situations.</p>
<p>1. When an application starts, these methods are called :</p>
<p><strong>a) onCreate()</strong></p>
<p><strong>b) onStart()</strong></p>
<p><strong>c) onResume().</strong></p>
<p>2. When some other application is called, while your application was running :</p>
<p><strong>a) onPause()</strong></p>
<p><strong>b) onStop()</strong> are called as new activity completely blocks the screen.</p>
<p>3. When you go back to your application, which was running previously,</p>
<p><strong>a) onRestart()</strong></p>
<p><strong>b) onStart()</strong></p>
<p><strong>c) onResume()</strong> are called. Here,it is similar to 1st one except for the difference that <strong>onRestart()</strong> is called rather than <strong>onCreate()</strong>. This is because the app exists in memory and just need to be loaded from it.</p>
<p>4. When the user presses back button to exit the application or there is no memory for the app, the <strong>onDestroy() </strong>method is called. In normal case, the order of execution for a running application would be,</p>
<p><strong>a) onPause()</strong></p>
<p><strong>b) onStop()</strong></p>
<p><strong>c) onDestroy().</strong></p>
<p>Difference between <strong>“onPause()”</strong> and <strong>“onPause() + onStop()” </strong></p>
<p>There are some situations when some new activity partially occupies the screen. In such cases, some part of our activity is still visible to the user and is not shifted to <strong><a href="http://developer.android.com/guide/components/tasks-and-back-stack.html">BackStack</a>. </strong>On the other hand, there are some cases when some new activity or application completely blocks the GUI (or screen), our activity’s GUI is shifted to BackStack. So in the first case (partially occupied), only the <strong>onPause()</strong> method is called. Whereas in the second case (completely blocked GUI), <strong>onPause() + onStop()</strong> methods are called. I hope you have now understood the difference between the situations where one or both of them are called.</p>
<p>In the next tutorial, I’ll show the code for the same thing which we have discussed till now. It will also cover debugging in android using a special class (<strong>android.util.Log</strong>). Stay tuned for more tutorials.</p>Android Activity lifecycle (Code) & Debugging using LogCat (Android Part – 8)2014-07-17T12:37:00+05:302014-07-17T12:37:00+05:30gagan93tag:thegeekyway.com,2014-07-17:/activity-lifecycle-and-debugging/<p>Hello Geeks, hope you have read the <a href="http://www.thegeekyway.com/android-activity-lifecycle/">previous article</a> and are clear with the theoretical concepts of an <strong>Android Activity</strong> and its <strong>Lifecycle.</strong> Now we will create a code for the same to see what all methods are called in which cases. This code is similar to what you might …</p><p>Hello Geeks, hope you have read the <a href="http://www.thegeekyway.com/android-activity-lifecycle/">previous article</a> and are clear with the theoretical concepts of an <strong>Android Activity</strong> and its <strong>Lifecycle.</strong> Now we will create a code for the same to see what all methods are called in which cases. This code is similar to what you might have created to study lifecycle of an <strong>Applet in Java (</strong>for those who are from java background). Create a new Android Application in Eclipse IDE and paste <a class="external" href="http://pastebin.com/pN7bx4ii">this</a> code.</p>
<div style="text-align:center">
<p><img alt="Android Lifecycle and Debugging" src="//thegeekyway.com/uploads/images/2014/03/command_line_android_debugging.jpg" title="Android Lifecycle and Debugging"></p>
</div>
<p>As you can see in the code, that we have extended the <strong>Activity</strong> class and overwritten all the methods which we had discussed in previous tutorial. In each method, we first call the super class version of the method (using <strong>super</strong> keyword) and then we write our own statement. Here we use a special class from android.util package. This is the <strong>Log</strong> class which is used for debugging in android. We use Log.d method in this program which has the following signature.</p>
<p><strong>static void d ( String tag, String message )</strong></p>
<p>First parameter takes the tag (you will shortly get to know what it is) and second takes the message to be printed. Anything printed using <strong>Log.d</strong> method is shown on the LogCat. LogCat is generally visible at the bottom as shown below:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/logcat.jpg"><img alt="logcat" src="//thegeekyway.com/uploads/images/2014/03/logcat.jpg"></a></p>
</div>
<p>The above screenshot shows the LogCat under the text area where we code. We can use the position marked as “Extend” in above screenshot to extend the logcat upwards so that it more messages are visible without scrolling.</p>
<p>Moreover, we also have a <strong>Debug perspective</strong> in which tabs like Console, Logcat, Variables and program breakpoints as shown here :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/debug_perspective.jpg"><img alt="debug_perspective" src="//thegeekyway.com/uploads/images/2014/03/debug_perspective-1024x545.jpg"></a></p>
</div>
<p>The default perspective is <strong>Java</strong> (extreme left). In the above screenshot, the <strong>Tag</strong> is nothing but the string visible inside “Tag” column (Lifecycle here) and <strong>Message</strong> is the string visible inside “Text” column (inside onCreate(), inside onStart() etc).</p>
<p>For those who are from pure java background, we can also use System.out.println() for debugging, and it will be visible in <strong>Console</strong> tab, but it is not recommended by Android Developers. Moreover, it may or may not work for some versions.</p>
<p>When we execute a typical android application in android virtual device, we get many messages from the system (or the virtual device). So sometimes it may become hard to locate our own messages in the LogCat. To overcome this problem, we can create custom LogCat filters so that our custom LogCat reads only specific tags or messages.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/11.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/03/11.jpg"></a></p>
</div>
<p>In the above screenshot, we need to focus only on the tags marked above, but the LogCat shows all the tags which are coming from AVD. So we can create custom LogCat filter by clicking on this icon present on left side of LogCat filter.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/add_filter.jpg"><img alt="add_filter" src="//thegeekyway.com/uploads/images/2014/03/add_filter.jpg"></a></p>
</div>
<p>Then this dialog box appears</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/create_filter.jpg"><img alt="create_filter" src="//thegeekyway.com/uploads/images/2014/03/create_filter.jpg"></a></p>
</div>
<p>I have all tags with name “LifeCycle”, so I have named the tag “LifeCycle”. The message doesn’t matter so I keep it blank. Click OK to create the filter.Now we are clear with basics of debugging and will execute our application with our virtual device and notice the output in LogCat when the Lifecycle methods are called.Execute the application and select your custom filter.</p>
<p>As per the <a href="http://www.thegeekyway.com/android-activity-lifecycle/">previous tutorial</a>, when the application starts, the methods shown below are called:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/111.jpg"><img alt="11" src="//thegeekyway.com/uploads/images/2014/03/111.jpg"></a></p>
</div>
<p>When we come back to home screen (using Home button on emulator), the GUI is updated and our activity is no more visible so these methods are called:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/2.jpg"><img alt="2" src="//thegeekyway.com/uploads/images/2014/03/2.jpg"></a></p>
</div>
<p>The application still remains in memory, so onDestroy() is not called yet ! Now we come back to our application using Menu and these methods are called in quick succession :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/3.jpg"><img alt="3" src="//thegeekyway.com/uploads/images/2014/03/3.jpg"></a></p>
</div>
<p>Now we use <strong>BACK</strong> button to exit the application and these methods are called :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/4.jpg"><img alt="4" src="//thegeekyway.com/uploads/images/2014/03/4.jpg"></a></p>
</div>
<p>So this completes the 7 basic methods in Android Lifecycle. Comment if you have any doubt !</p>Resetting Windows Password2014-07-17T12:22:00+05:302014-07-17T12:22:00+05:30gagan93tag:thegeekyway.com,2014-07-17:/resetting-windows-password/<p>In past, some of you have used clever utilities like Kon Boot Utility to reset your window password, or asked your engineer to format C:/ drive if there was no way out. This article will introduce various ways to reset password in Windows xp,vista, 7,8 & 8.1. Please …</p><p>In past, some of you have used clever utilities like Kon Boot Utility to reset your window password, or asked your engineer to format C:/ drive if there was no way out. This article will introduce various ways to reset password in Windows xp,vista, 7,8 & 8.1. Please note that this knowledge is shared for educational purpose only. Resetting passwords in windows is resetting the SAM (Security Account Manager)</p>
<div style="text-align:center">
<p><img alt="Windows Password Recovery" src="//thegeekyway.com/uploads/images/2014/04/rsz_1wvn_copy.png" title="Windows Password Recovery"></p>
</div>
<ul>
<li>
<h3><strong>Bootable disks (<a class="external" href="http://www.hirensbootcd.org/download/">Hiren’s Boot CD</a> / <a class="external" href="http://download.cnet.com/Windows-Password-Unlocker-Enterprise/3000-18501_4-75285788.html">Windows Unlocker Enterprise</a> / <a class="external" href="http://pcsupport.about.com/gi/o.htm?zi=1/XJ&zTi=1&sdn=pcsupport&cdn=compute&tm=6&f=00&su=p284.13.342.ip_&tt=65&bt=9&bts=9&zu=http%3A//www.piotrbania.com/all/kon-boot/index2.html%23free">Kon Boot</a> etc.)</strong></h3>
</li>
</ul>
<p>These disks are Windows XP based operating systems, even lighter than that in size. The size is kept to its lowest and more utilities like Registry Scanners, Password Reset / Bypass Utilities, File Explorer etc. Using GRUB loaders, we have option to install these OS on a USB Diskette (pen drive). We can create Bootable CD / pendrive to boot this temporary OS directly from USB to RAM and reset password or fix a broken system. Such CD’s are more popularly known as <strong>LIVE CD’s,</strong> but <strong>It is important to note</strong> that such Disks can be used to repair only <strong>Windows xp,Vista and 7. </strong></p>
<p>The reason behind this is, that in most of the laptops in which windows 8 comes pre-installed, the BIOS is configured in <strong>UEFI mode</strong> (Unified Extensible Firmware Interface). The UEFI mode does not allow to boot legacy OS like Windows xp, DOS etc. If you enable the LegacyBoot option, it will boot your legacy OS (like xp, DOS etc) but will not allow you to access the Hark Disk. A typical BIOS screen of a windows 8 system is shown here :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/20121207140026232.jpg"><img alt="20121207140026232" src="//thegeekyway.com/uploads/images/2014/04/20121207140026232.jpg"></a></p>
</div>
<p>This screenshot explain the aim of having <strong>Legacy mode</strong> in newer BIOS systems. So here we have two Boot orders (or Boot device priorities), one for UEFI or newer systems, & one for older or Legacy systems. So to reset Windows 8/8.1 password, our conventional methods do not work. We’ll study resetting Windows 8 / 8.1 password in the last section of this article, methods for older Windows systems are described here.</p>
<p><strong>a) <a class="external" href="http://www.hirensbootcd.org/download/">Hiren’s Boot CD Series</a>:</strong> It is a set of powerful utilities. The oldest edition was around 186 MB, including a rich set of utilities (shown below). The executables are compressed using <strong>UHA compression standard </strong>and extracted on the go when user executes a particular program. The newer version includes many On-Boot utilities and a richer set of windows utilities so the size is now 700 MB. Screenshot of Utilities available in Hiren’s 15.2 (Size : 594 MB) is shown below : </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/hirens.jpg"><img alt="hirens" src="//thegeekyway.com/uploads/images/2014/04/hirens.jpg"></a></p>
</div>
<p><strong>b) <a class="external" href="http://download.cnet.com/Windows-Password-Unlocker-Enterprise/3000-18501_4-75285788.html">Windows Unlocker Enterprise</a> :</strong> This is another Windows xp based light OS but its current version is only 43 MB in size because it includes only windows xp image + password unlocker software. It locates the partition containing windows installed, and at the same time gives option to choose another partition (in case it has located wrong partition or in case of multi-boot system). As it is also a windows xp based utility, it doesn’t work for windows 8/8.1 A screenshot of this utility is shown below :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/136093-windows-password-unlocker-enterprise.jpg"><img alt="136093-windows-password-unlocker-enterprise" src="//thegeekyway.com/uploads/images/2014/04/136093-windows-password-unlocker-enterprise.jpg"></a></p>
</div>
<p><strong>c)<a class="external" href="http://pcsupport.about.com/gi/o.htm?zi=1/XJ&zTi=1&sdn=pcsupport&cdn=compute&tm=6&f=00&su=p284.13.342.ip_&tt=65&bt=9&bts=9&zu=http%3A//www.piotrbania.com/all/kon-boot/index2.html%23free">Kon Boot</a></strong> : It is a DOS based utility used to Reset / Bypass / Remove Windows password. It is even lighter than the other two due to the fact that it contains image of DOS based OS. As it is also a legacy OS based utility, it doesn’t work for windows 8/8.1. Screen shot of KonBoot Reset tool is shown below :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/booting-konboot-11.png"><img alt="booting-konboot-11" src="//thegeekyway.com/uploads/images/2014/04/booting-konboot-11.png"></a></p>
</div>
<ul>
<li>
<h3>Net user command Method</h3>
</li>
</ul>
<p>Net user is one of the oldest commands used to reset Windows password without using any external utility. Its idea is simple, we can reset password of any user if we are one of the Admins of system. The general syntax of this command is :</p>
<p><strong>net user \<user-name> * (Press Enter)</strong></p>
<p>Open CMD (command prompt) <strong>with administrative rights</strong> to type this command.</p>
<p>For eg : <strong>net user gagan * (Enter)</strong></p>
<p>To view list of users on the system, use \<<strong>net user> </strong>command.</p>
<ul>
<li>
<h3>Resetting Windows 8/8.1 passwords</h3>
</li>
</ul>
<p>When we want to reset password of a Windows 8.1 based system (where windows 8/8.1 was pre-installed), the conventional methods described above do not work due to UEFI firmware settings. We need Windows 8/8.1 based Live CD to reset such passwords (or Linux based Live CDs). The most popular and easy to use Windows 8.1 based Live CD is <strong><a class="external" href="https://onedrive.live.com/?cid=c3a5af1919cddbc6&id=C3A5AF1919CDDBC6%216863&ithint=folder,.ISO&authkey=%21AP0ZfWlWP9BUHQc">Gandalf Windows 8 PE</a>. </strong>It allows to boot using <strong>Legacy mode </strong>and still accesses Hard Disk because its not a legacy OS. We can easily boot using this disk and reset it using any utility from Hiren’s collection (like Windows Gate) or any other available utility. Please note that here we are referring to Windows 8/8.1 <strong>local account password</strong> and not <strong>Microsoft account password.</strong></p>
<p>We can also Reset Windows 8/8.1 password using our vendor’s <strong>Recovery module. </strong>For Eg. in <strong>HP </strong>based laptops, F11 is the recovery key. To enter Recovery mode, we can press F11 continuously as soon as system starts. After that, windows starts in recovery mode. Similarly in some Dell based laptops, to enter recovery mode, restart with <strong>Shift key</strong> pressed. The following screens appear one after another, after following your vendor’s method of entering into recovery mode.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/keyboard-layout.jpg"><img alt="keyboard-layout" src="//thegeekyway.com/uploads/images/2014/04/keyboard-layout.jpg"></a></p>
</div>
<p>My keyboard layout is US (top left), choose yours accordingly.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/jTw1r.png"><img alt="jTw1r" src="//thegeekyway.com/uploads/images/2014/04/jTw1r.png"></a></p>
</div>
<p>Choose Troubleshooting option</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/ZOfn7.png"><img alt="ZOfn7" src="//thegeekyway.com/uploads/images/2014/04/ZOfn7.png"></a></p>
</div>
<p>Choose Advanced option</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/uVNVJ.png"><img alt="uVNVJ" src="//thegeekyway.com/uploads/images/2014/04/uVNVJ.png"></a></p>
</div>
<p>Choose Command Prompt option.</p>
<p>After that we’ll use a trick called utilman trick. We know that when logon screen opens and prompts for password, we have Utility Manager setting on the left which can be used to open Narrator and other things as shown</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/04/login-screen-windows-8-Custom.jpg"><img alt="login-screen-windows-8 (Custom)" src="//thegeekyway.com/uploads/images/2014/04/login-screen-windows-8-Custom.jpg"></a></p>
</div>
<p>The trick is simple, we will enter command prompt mode of recovery mode and rename <strong>command prompt</strong> to <strong>utility manager</strong> so that when we click on this button (left bottom corner of above screenshot), it opens cmd and we can use our regular <strong>net user </strong>command to reset the password. When we open command prompt mode, we have a command prompt pointing to</p>
<p><strong>X:/Windows/System32</strong></p>
<p>First of all, we’ll change the prompt to C:/ drive (or drive containing installed windows) because X is our recovery drive. then we’ll use commands to enter into system32 directory of c: drive using <strong>cd</strong> (change directory) command.</p>
<p><strong>cd Windows</strong></p>
<p><strong>cd System32</strong></p>
<p>Now our prompt is on desired directory. We’ll rename Utility Manager to some other name and command prompt to utility manager using these commands :</p>
<p><strong>rename utilman.exe utilman2.exe</strong></p>
<p><strong>rename cmd.exe utilman.exe</strong></p>
<p>Now restart your computer, at the logon screen, click on bottom left icon and command prompt will open. Now use the <strong>net user </strong>trick (described above).</p>
<p>Post your queries in comments section (if any)</p>Learning About 1 Dimensional Arrays (1 D Arrays)2014-07-12T02:55:00+05:302014-07-12T02:55:00+05:30sanyam_khuranatag:thegeekyway.com,2014-07-12:/learning-1-dimensional-arrays/<p>You may have heard of arrays or "tuples" before, if you've learnt any programming language. If not, then don't worry, you'll learn everything about them in this tutorial and how are they used in C to handle <strong>large amounts of data of same data type</strong> effectively and easily. So, let's …</p><p>You may have heard of arrays or "tuples" before, if you've learnt any programming language. If not, then don't worry, you'll learn everything about them in this tutorial and how are they used in C to handle <strong>large amounts of data of same data type</strong> effectively and easily. So, let's know about them. In this tutorial we'll learn about 1 Dimensional Arrays and in later tutorials we'll look at 2 Dimensional Arrays and Multidimensional arrays.</p>
<h2 id="what-is-an-array" style="text-align: center;"><strong>What is an array?</strong></h2>
<p><em>An array can be defined as a way to store list of large no. of items of same data type in contiguous memory location</em>. Whenever we need to store some information about the same data type, we use arrays. For instance, consider a case when you need to store roll numbers of all students in the class. One way to do this is, I can define separately variables for all roll numbers like:</p>
<div class="highlight"><pre><span></span><code>...
int rollno1 = 1;
int rollno2 = 2;
int rollno3 = 3;
int rollno4 = 4;
.
.
.
.
.
.
.
.
.
int rollnon = n; //Here n is the last roll number
</code></pre></div>
<p>Surely, this would be an inefficient approach, and thus, a better way to do this is would be through arrays like this:</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">n</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Here</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="n">number</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">students</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">roll</span><span class="w"> </span><span class="n">numbers</span><span class="w"> </span><span class="k">are</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">stored</span><span class="p">.</span>
</code></pre></div>
<p>I know, you haven't been introduced to "How to declare arrays", which we would see in just a moment, but I just wanted to show how systematically you can use array to store and manage data efficiently. Below is an image describing how array is stored in the memory location. Note here, that each item can be accessed via index number (explained in detail later), and remember, this index value as shown would always start from 0. Many errors, newbie programmers fall trap to, is because they confuse the first index of the array to be 1, which is not the case, as the first index is always 0.</p>
<p>The contiguous memory means, the data cells of memory would be one after the other, so if we need to store 60 items, we need a location in memory with at least 60 cells one after the other. Consider contiguous as being "continuous".</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/07/arrays11.png" title="Array representation in memory."><img alt="Array representation in memory." src="//thegeekyway.com/uploads/images/2014/07/arrays11.png"></a></p>
</div>
<h2 id="how-to-declare-a-1-dimensional-array" style="text-align: center;">How to declare a 1 Dimensional array?</h2>
<p>Now let us see how you can declare an array :</p>
<div class="highlight"><pre><span></span><code><data type> <array name>[<size of array>];
</code></pre></div>
<p>Here data type is the actually the type of data of the elements you would store in your array, array name is simply the name you give to your array (similar to the name of a variable, in order to access it later), which would follow the general rules of naming a variable which we have discussed previously. After the name of the array, we would have a set of square brackets in which we would mention the size of the array.</p>
<p>Now, let's again quickly take a look at our rollno array :</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">n</span><span class="o">]</span><span class="p">;</span>
</code></pre></div>
<p>Technically, before declaring this statement, you should first define the value of variable n, or else you'll face an error.</p>
<p>Here, n is just use to make you first comfortable with basic declaration of array. More sophisticatedly, you can simply use a definite value of n to define an array as follows:</p>
<div class="highlight"><pre><span></span><code>int rollno[60]; //Enough space to store roll no of 60 students
</code></pre></div>
<p>Next, we need to fill in the values for our array. Traditionally, following an approach as we've seen in the beginning would not only take a lot of time to define each variable corresponding to each student, but makes it a hectic process.</p>
<p>But first, we need to explore more about what exactly the index no or indices of the array is?</p>
<p>Well, typically, the computer memory is divided into small data cells (as shown above in the picture), occupying contiguous memory location. To access any element stored in an array we need something to reference it, like a variable name. In case we followed, the very first traditional approach defining 60 variables, they would have different names, which would be difficult to remember, but we could refer a single element. Using array, makes this more efficient by providing us with index no. or indices, as each subsequent element can be addressed / referred to by it's index value, which we would see in just a moment.</p>
<h2 id="what-would-be-the-size-of-the-array" style="text-align: center;">What would be the size of the array?</h2>
<p>An array would consist of many elements and hence the size of the array would be the sum of size of all elements in the array. For instance, in our example of rollno array for 60 student, which is of type int, the memory locations are occupied such that each cell has capacity to store an integer. We know, that in C , integer takes two bytes, so total memory occupied by rollno array would be 2 * 60 = 120.</p>
<p>Thus, you can easily calculate the amount of memory an array occupies by using the following formula:</p>
<blockquote>
<p>Size of array = Size of data type * Total no. of elements in the array</p>
</blockquote>
<p>Now let's see how to fill up the array:</p>
<h2 id="putting-values-in-array" style="text-align: center;">Putting values in array</h2>
<p>Filling up an array is very simple using for loop, as we would see here :</p>
<div class="highlight"><pre><span></span><code><span class="nc">int</span><span class="w"> </span><span class="n">rollno</span><span class="o">[</span><span class="n">60</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="n">Declaring</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="mi">60</span><span class="w"> </span><span class="n">elements</span><span class="w"> </span><span class="n">storage</span><span class="w"> </span><span class="nf">space</span>
<span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">60</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="n">scanf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">rollno</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="n">Here</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">increments</span><span class="p">,</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">allow</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">store</span><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="k">input</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">very</span><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="nf">space</span><span class="w"> </span><span class="n">till</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">60</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="n">terminates</span><span class="p">.</span>
<span class="err">}</span>
</code></pre></div>
<p>The working is very straight forward as explained in the comments.</p>
<p>One important thing to note here is that, as previously mentioned the index of array would start from 0 not 1. You should always remember this thing while handling data with arrays and your loops should also work in the same fashion.</p>
<p><strong>NOTE</strong> : Newbies mistake, assuming array to be start from 1. Remember, it would be started from 0.</p>
<h2 id="printing-out-elements-of-array" style="text-align: center;">Printing out elements of Array</h2>
<p>Now, after filling up the array, let's see how we can access one particular element. For instance, let's say I need to look for roll no. of 11th student, so I know it would be stored as 10th element in memory (not 11th) as indices start from 0. So, let's print the value of 11th student.</p>
<div class="highlight"><pre><span></span><code>...
printf("%d",rollno[10]);
...
</code></pre></div>
<p>In case you want to print all values stored in the array, we simply apply a loop to loop through the all the values in the array from indices 0 to indices 59 (a total of 60 values in this case).</p>
<div class="highlight"><pre><span></span><code><span class="p">...</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">59</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="err">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="ss">"%d"</span><span class="p">,</span><span class="n">rollno</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span>
<span class="err">}</span>
<span class="p">...</span>
</code></pre></div>
<p>So, now that you've learned about 1 dimensional arrays, it's time to look for two dimensional arrays.</p>
<p>Have any doubts till now? Please feel free to mention in comments section below, or give me a shout on twitter \@ErSanyamKhurana.</p>Getting Started With Git2014-04-02T22:24:00+05:302014-04-02T22:24:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-04-02:/getting-started-git/<p>Git is the most popular version control system now a days. The reason behind popularity is its simple yet tremendous functionality. Unlike other VCSs (Version Control Systems), Git has some unique ways to control and share your data. Because of its huge functionality it also tends to become a lot …</p><p>Git is the most popular version control system now a days. The reason behind popularity is its simple yet tremendous functionality. Unlike other VCSs (Version Control Systems), Git has some unique ways to control and share your data. Because of its huge functionality it also tends to become a lot complex for advanced users. As they say a thousand miles journey starts with a single step, we are going to understand the very basics of git in this article!</p>
<p>So, before starting anything we need to understand what Git is and what it does. Git is a version control system, which records changes in your code/files over a period of time. You can recall older version and manage changes. The working of Git is a little different than other VCSs. Most of the VCSs store the information about the data as the changes that have been made in different files. If you have 3 files in a project and you change any one of them, the VCSs will keep record of the change. That’s where Git differs, it keeps record of the changes along with the new changed file. Moreover, other VCSs generally operate online, storing all the data at the remote servers, while Git stores all the data on the local system as well as the remote server, which makes it easy for the user to make changes even when she is offline and commit the changes when she gets the network connection. In other benefits of Git, you don’t need to go to remote server to check for your project history, it’s all there on your local machine.</p>
<p>Before getting started, I would like to discuss one more feature of Git and that is repositories. There are two kind of repositories in Git, namely, local repositories and remote repositories. And believe me, they are the exact same thing! So, what exactly is a repository? It’s the place where you store all of your working data, your project files etc. Local repository is the one that is set up on your system and the remote is the one that is hosted on a server, probably on another continent! And here is the beauty of a distributed version control system, you can work with both of them just as they are the same thing! You can make changes to your local repository, add/delete files etc. and push them to remote repository.</p>
<p>Enough of concepts, let’s get started! The very basic thing to do with Git is to create a local repository, of-course, first you have to install it on your system! Type this in your RHEL based system to install git (make appropriate changes for Debian based systems):</p>
<div class="highlight"><pre><span></span><code>sudo yum install git
</code></pre></div>
<p>And the next step is:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>init
Initialized<span class="w"> </span>empty<span class="w"> </span>Git<span class="w"> </span>repository<span class="w"> </span><span class="k">in</span><span class="w"> </span>/home/peeyush/git_test/.git/
</code></pre></div>
<p style="line-height: 1.5em;">And that’s it, there is your local repository. You can add files, make changes to them, commit the changes etc. (How? We will see shortly!). Now, generally after this people keep going on about working with local repository, but as I am getting you started, I am going to take a little different but practical approach. We are going to start with a remote repository! Now, where do you get a remote repository? Well, GitHub, of-course! Go to </p>
<p><a href="http://www.github.com">www.github.com</a>
<p style="line-height: 1.5em;"> and create a account there if you don’t already have one. Now, create a new repository by clicking on the link on the top left corner:</p>
</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-04-01 15:59:59.png" src="https://lh6.googleusercontent.com/dMsAdtBhU35X5J11FkmC4QSU8DwHfHx1bF-IHDLutUsI3qdpSmzP3JM9Kn274A0QQnnAyNhU2iLU99j-ADvlPd4CDGDrr87X7Fv6Eozdr4080iTPi8WCti132SMCIA"></strong></p>
</div>
<p>Now, enter a name for the repository and click on the “create repository” button. And your remote repository is ready! First of all you need to copy you remote repository to your local repository. Copy the http address and you can copy the repository using git protocol, like this:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-04-01 16:21:57.png" src="https://lh3.googleusercontent.com/k2Lo1r1Z3uaUXlu9rf-h9cAphKIemCUq9dBQSO3KOSDJaDzX6j25um04Iyi5L0HmLk7PFo-L7Wci6p0Nu77vuslWDB9PtPX0ZPeyux2UcV9xe6XP2VqHn3V2G1cUZw"></strong></p>
</div>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/Pensu/test.git
Cloning<span class="w"> </span>into<span class="w"> </span><span class="s1">'test'</span>...
warning:<span class="w"> </span>You<span class="w"> </span>appear<span class="w"> </span>to<span class="w"> </span>have<span class="w"> </span>cloned<span class="w"> </span>an<span class="w"> </span>empty<span class="w"> </span>repository.
</code></pre></div>
<p>So, there appears to be a warning, but we already know that as we haven’t added anything to the repository! Now you will see a directory by the name of remote repository in your current directory. That is your local repository. To verify try this on your system:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span><span class="nb">cd</span><span class="w"> </span>test/
$<span class="w"> </span>git<span class="w"> </span>remote<span class="w"> </span>-v
origin<span class="w"> </span>https://github.com/Pensu/test.git<span class="w"> </span><span class="o">(</span>fetch<span class="o">)</span>
origin<span class="w"> </span>https://github.com/Pensu/test.git<span class="w"> </span><span class="o">(</span>push<span class="o">)</span>
</code></pre></div>
<p>Here fetch and push are the repositories where your local repository gets the data from and where it sends the changes respectively. As I just cloned a remote repository, I can fetch and push from this repository. Now, we are going to add a file and pushing it to the remote repository.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>touch<span class="w"> </span>newfile.txt
$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Getting Started With Git"</span><span class="w"> </span>><span class="w"> </span>newfile.txt<span class="w"> </span>
$<span class="w"> </span>git<span class="w"> </span>status
<span class="c1"># On branch master</span>
<span class="c1">#</span>
<span class="c1"># Initial commit</span>
<span class="c1">#</span>
<span class="c1"># Untracked files:</span>
<span class="c1"># (use "git add <file>..." to include in what will be committed)</span>
<span class="c1">#</span>
<span class="c1"># newfile.txt</span>
nothing<span class="w"> </span>added<span class="w"> </span>to<span class="w"> </span>commit<span class="w"> </span>but<span class="w"> </span>untracked<span class="w"> </span>files<span class="w"> </span>present<span class="w"> </span><span class="o">(</span>use<span class="w"> </span><span class="s2">"git add"</span><span class="w"> </span>to<span class="w"> </span>track<span class="o">)</span>
</code></pre></div>
<p style="line-height: 1.5em;">I created a new file, echoed some content and I checked the status of my repository. Git can track what new files have been added and what files have changed using checksum. So, it shows me that my “newfile.txt” is untracked. Before committing any changes to the repositories, we need to add the files/changes to Git.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>add<span class="w"> </span>newfile.txt
$<span class="w"> </span>git<span class="w"> </span>status
<span class="c1"># On branch master</span>
<span class="c1">#</span>
<span class="c1"># Initial commit</span>
<span class="c1">#</span>
<span class="c1"># Changes to be committed:</span>
<span class="c1"># (use "git rm --cached <file>..." to unstage)</span>
<span class="c1">#</span>
<span class="c1"># new file: newfile.txt</span>
<span class="c1">#</span>
</code></pre></div>
<p style="line-height: 1.5em;">As you can see the file is ready to be committed now. Let’s do it:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>commit<span class="w"> </span>-m<span class="w"> </span><span class="s2">"Added new file"</span><span class="w"> </span>
<span class="o">[</span>master<span class="w"> </span><span class="o">(</span>root-commit<span class="o">)</span><span class="w"> </span>0c53cdc<span class="o">]</span><span class="w"> </span>Added<span class="w"> </span>new<span class="w"> </span>file
<span class="m">1</span><span class="w"> </span>file<span class="w"> </span>changed,<span class="w"> </span><span class="m">1</span><span class="w"> </span>insertion<span class="o">(</span>+<span class="o">)</span>
create<span class="w"> </span>mode<span class="w"> </span><span class="m">100644</span><span class="w"> </span>newfile.txt
</code></pre></div>
<p style="line-height: 1.5em;">Here -m denotes the message which you will need to remember the changes you made in this commit. I am adding newfile.txt here, so the message says that a new file has been added. You local repository is fully updated now, with all the changes. If you want to check, what all changes you have made to the local repository before pushing it to remote repository, just run “git log”.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>log
commit<span class="w"> </span>0c53cdc27eda90c67219ba15ff8457f337c4aa14
Author:<span class="w"> </span>Peeyush<span class="w"> </span>Gupta<span class="w"> </span><peeyushgupta91@gmail.com>
Date:<span class="w"> </span>Tue<span class="w"> </span>Apr<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">16</span>:37:59<span class="w"> </span><span class="m">2014</span><span class="w"> </span>+0530
<span class="w"> </span>Added<span class="w"> </span>new<span class="w"> </span>file
</code></pre></div>
<p style="line-height: 1.5em;">It will show you the commit checksum, author name/email, date and time of the commit and the message you provided at the time of commit. Well, it seems alright, so now we are gonna push the changes to remote repository.</p>
<div class="highlight"><pre><span></span><code><span class="n">$</span><span class="w"> </span><span class="n">git</span><span class="w"> </span><span class="n">push</span><span class="w"> </span><span class="n">origin</span><span class="w"> </span><span class="n">master</span>
<span class="n">Username</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="err">'</span><span class="n">https</span><span class="o">:</span><span class="c1">//github.com': Pensu</span>
<span class="n">Password</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="err">'</span><span class="n">https</span><span class="o">:</span><span class="c1">//Pensu@github.com': </span>
<span class="n">Counting</span><span class="w"> </span><span class="n">objects</span><span class="o">:</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">.</span>
<span class="n">Writing</span><span class="w"> </span><span class="n">objects</span><span class="o">:</span><span class="w"> </span><span class="mi">100</span><span class="o">%</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span><span class="w"> </span><span class="mi">249</span><span class="w"> </span><span class="n">bytes</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">.</span>
<span class="n">Total</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">(</span><span class="n">delta</span><span class="w"> </span><span class="mi">0</span><span class="p">),</span><span class="w"> </span><span class="n">reused</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">(</span><span class="n">delta</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="n">To</span><span class="w"> </span><span class="n">https</span><span class="o">:</span><span class="c1">//github.com/Pensu/test.git</span>
<span class="o">*</span><span class="w"> </span><span class="p">[</span><span class="n">new</span><span class="w"> </span><span class="n">branch</span><span class="p">]</span><span class="w"> </span><span class="n">master</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">master</span>
</code></pre></div>
<p style="line-height: 1.5em;">So, what’s origin and master? Here master is the branch where I am working on my local repository and origin is the branch where master is going to be merged at the remote repository. Wait, what is a branch? Branch is basically the context that you are working in. You can have many branches in the same project and merge them in the remote repository branches. For our purpose we only need one branch and that is the master one. You can verify it using:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>git<span class="w"> </span>branch
*<span class="w"> </span>master
</code></pre></div>
<p style="line-height: 1.5em;">Now, as it’s all done and dusted, time to check the remote repository if the changes were successfully committed. Open up your browser and go to your repository, you should see a newfile.txt there. </p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-04-01 16:55:31.png" src="https://lh4.googleusercontent.com/yktfgORxxyXn4QDOho-38Ow0kd99LnUalRvlzOk-8AF_m-PsufDyGXPB0VRgLTIWDJlmD5_GVZgMMEP8pnrD-mWQViYKM5usgeWhNrVO2TydcF6PEctybVm0shyNeA"></strong></p>
</div>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-04-01 16:55:40.png" src="https://lh6.googleusercontent.com/qwKYV6kTOGF7TFLW5q-Pim8eQ_55ZRNkV7eTHFAg4cJ1Qnw-IvbBLq3Frult5WPCdbZoOoYqRLztdAw0uQsgATHUhO2IBC-FOom5p-qx59L8_yBVt1DQtRFx520W4g"></strong></p>
</div>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-04-01 16:55:40.png" src="https://lh6.googleusercontent.com/qwKYV6kTOGF7TFLW5q-Pim8eQ_55ZRNkV7eTHFAg4cJ1Qnw-IvbBLq3Frult5WPCdbZoOoYqRLztdAw0uQsgATHUhO2IBC-FOom5p-qx59L8_yBVt1DQtRFx520W4g"></strong></p>
</div>
<p>Git is a very huge topic, but this article should get you started to clone remote repositories and push the changes. Happy sharing!</p>How to copy camera photos to Android phone directly without any app2014-03-30T20:27:00+05:302014-03-30T20:27:00+05:30Anooptag:thegeekyway.com,2014-03-30:/android-4-4-2-how-to-copy-camera-photos-to-android-phone-directly/<p>There are some of application to control/Copy your photos directly from DSLR from your phone like DSLR dashboard app but for Point and Shoot (P&S) .But now with latest android release you can copy any camera photos to Android Phone directly.</p>
<p><strong>Why this is great trick?</strong> No matter …</p><p>There are some of application to control/Copy your photos directly from DSLR from your phone like DSLR dashboard app but for Point and Shoot (P&S) .But now with latest android release you can copy any camera photos to Android Phone directly.</p>
<p><strong>Why this is great trick?</strong> No matter which phone you have ,even a point and shoot gives you better great result. This is very handy while travelling and even general, you can copy your photos to phone just in time and share with your friends. No need to buy expensive Eye-Fi card.</p>
<p>I have tested to import photo using <strong><em>default Gallery/Photo importer app</em></strong> from android with <strong><em>Canon Powershot Sx40Hs</em></strong> and <em><strong>Nikon D7000.</strong> </em>No additional app required, just additional <a href="http://www.amazon.in/APS-Micro-Mobile-Phones-Tablets/dp/B00GTADSXQ/" title="http://www.amazon.in/APS-Micro-Mobile-Phones-Tablets/dp/B00GTADSXQ/">On-The-Go connector.</a> You can buy from nearby local store for \$1-\$2. I brought in India for INR. 70/-.</p>
<p>Here the full tutorial to copy any camera photos directly to your phone without any app.</p>
<p><u><strong>What do you need:</strong></u></p>
<p><strong>1. Camera:</strong> Should work with any camera.<br>
<strong>2. Android Phone:</strong> <em>Running on </em><em>KitKat 4.4.2 /CM11</em><strong>, (should work with <em>updated Google Gallery/Google+ app</em>, needs to verify on other version of Android)<br>
</strong>3. Camera PC connector :** <p style="line-height: 1.5em;"> <p style="line-height: 1.5em;"> <p style="line-height: 1.5em;"> <p style="line-height: 1.5em;"> Usually mini usb cable of you camera which you use to connect with computer</p></p></p></p></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/images1.jpg" title="Mini USB Data Cable"><img alt="Mini USB Data Cable" src="//thegeekyway.com/uploads/images/2014/03/images1.jpg"></a></p>
</div>
<p><strong>4. On-The-Go connector :</strong> <p style="line-height: 1.5em;"> <p style="line-height: 1.5em;"> <p style="line-height: 1.5em;">Basically a micro USB with female connector</p></p></p> <p style="line-height: 1.5em; text-align: center;"> </p></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/41+N9v0CUjL1.jpg" title="On-The-Go micro USB"><img alt="On-The-Go micro USB" src="//thegeekyway.com/uploads/images/2014/03/41+N9v0CUjL1-300x300.jpg"></a></p>
</div>
<p>Now,<strong> how to copy camera photos to Android phone directly</strong> <strong>:</strong></p>
<p>i. Connect your camera to phone using both cable like following setup</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/SX40ConnectionWithAndroid1.jpg"><img alt="SX40ConnectionWithAndroid" src="//thegeekyway.com/uploads/images/2014/03/SX40ConnectionWithAndroid1-300x211.jpg"></a></p>
</div>
<p>ii. You will get pop-up to choose from USB device. Select Camera importer or Gallery (If any other popup appears cancel that).</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/ChooseApptoImport1.jpg" title="Choose App to Import"><img alt="Choose App to Import" src="//thegeekyway.com/uploads/images/2014/03/ChooseApptoImport1-180x300.jpg"></a></p>
</div>
<p>iii. You will get following window on selecting Camera Importer</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/CameraThumNails1.jpg" title="Camera Thumbnails"><img alt="Camera ThumNails" src="//thegeekyway.com/uploads/images/2014/03/CameraThumNails1-180x300.jpg"></a></p>
</div>
<p>iv. Now select directory or images to import.</p>
<p>v. Images will be saved in your SDCard --> {CameraNameDirectory}</p>
<p>Enjoy.</p>
<p>PS: I have tested this with Galaxy SII running CM11 (Android 4.4.2) and Canon SX40Hs/Nikon D7000. I have to test with other version of android and with other Point & Shoot camera. If you happen to test please update here.</p>[Ultimate Guide] What are loops in programming and how are they important?2014-03-27T21:40:00+05:302014-03-27T21:40:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-27:/loops-programming-important/<p>Welcome everyone to yet another tutorial on C Programming language. In this tutorial we'll be learning about a great concept of loops. Most of the programming languages would use exactly the same syntax for loops, so understand this carefully. Loops provide with great utility and power and saves from a …</p><p>Welcome everyone to yet another tutorial on C Programming language. In this tutorial we'll be learning about a great concept of loops. Most of the programming languages would use exactly the same syntax for loops, so understand this carefully. Loops provide with great utility and power and saves from a lot of overhead which may be there without them.</p>
<p>In this tutorial, we'll be discussing about various topics which revolves around concept of loops and understand them. They are :</p>
<ul>
<li><a href="#Basic-Definition-and-Types-of-Loops" title="ASP.Net Basics : Populate checkboxlist from database using C#">Basic Definition and Types of Loops</a></li>
<li><a href="#Basic-section-of-Loop" title="[Ultimate Guide] What are loops in programming and how are they important?">Basic Section of Loop</a></li>
<li><a href="#For-While-and-do-whlie-loop">For, While and Do-While Loops</a><ul>
<li><a href="#for-loop">For Loop</a></li>
<li><a href="#while-loop">While Loop</a></li>
<li><a href="#do-while-loop">Do While loop</a></li>
</ul>
</li>
<li><a href="#Difference-between-entry-controlled-and-exit-controlled-loop" title="ASP.Net Basics : Populate checkboxlist from database using C#">Difference between Entry Controlled and exit controlled loop</a></li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/Ultimage-Guide-on-Loops-in-programming1.png" title="Understand about Loops in Programming, for, while, do while"><img alt="Understand about Loops in Programming, for, while, do while " src="//thegeekyway.com/uploads/images/2014/03/Ultimage-Guide-on-Loops-in-programming1.png"></a></p>
</div>
<p #Basic-Definition-and-Types-of-Loops></p>
<hr>
<h2>Basic Definition and Types of Loops</h2>
<p>The basic concept behind <strong><em>"Loop"</em></strong> is to <u><em>execute certain set of statements as far as a given condition is true, or in many cases a counter variable gets out of a specific range defined, or we use may use some Logical operators to control the flow in our loop.</em> </u>There are basically three types of loops in the C language. They are :</p>
<ul>
<li><strong>for loop</strong></li>
<li><strong>while loop</strong></li>
<li><strong>do while loop</strong></li>
</ul>
<p>Now, let's look at the syntax of each of these and how they can be used, in detail. To understand all these in a better way, let's consider a common and simple example to print numbers from 1 to let's say 500.</p>
<p>The most basic approach to carry out this task is to keep printing each line having different numbers from 1 to 500 which would look something like this :</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">void</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"1 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"2 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"3 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"4 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"5 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"6 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"7 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>.<span class="w"> </span>
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"499 n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"500 n"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>Now, let's try to improve upon this a little bit.<br>
We can take a counter variable and after each step just update it's value, then it would be something like this :</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">void</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">counter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">;</span>
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">counter</span><span class="ss">)</span><span class="c1">; /* Carry on this </span>
<span class="w"> </span><span class="nv">counter</span><span class="o">++</span><span class="c1">; 500 times */</span>
}
</code></pre></div>
<p>In this simple approach although we used a counter variable to avoid writing different numbers and updating the value of counter is left to the computer. But, it lead to an increase in the Lines Of Code (LOC) which is considered as an important parameter to compare any software.</p>
<p>So, it would be good, if we could use this counter variable approach but reduce the LOC. We can do it by using loops, now let's carry this out using different loops.</p>
<p #Basic-section-of-Loop></p>
<h2><strong>Basic sections of the loops</strong></h2>
<p>All loops have three basic sections which are :</p>
<ul>
<li><strong>Initialization</strong> : we need to initialize our counter variable in this statement.</li>
<li><strong>Test Condition</strong> : is the condition which needs to be true for the loop to keep running, as soon as it becomes false, the loop would stop executing, and the control would be directed to the next statement.</li>
<li><strong>Updation</strong> : In this statement the value of counter variable is updated (either incremented or decremented )</li>
</ul>
<p #For-While-and-do-whlie-loop></p>
<h2>For, While and Do While Loops :</h2>
<p>Now, we'll look here how we can print numbers from 1 to 500 using for, while and do-while loops by first understanding their syntax and then understanding the flow of control of these loops.</p>
<p #for-loop></p>
<h3><u><strong>Understanding and using for loop</strong></u></h3>
<p>* <strong>Syntax</strong> :*</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">initialization</span><span class="w"> </span><span class="c1">; test condition ; updation )</span>
{
<span class="w"> </span>.
<span class="w"> </span>.
<span class="w"> </span>.
}
</code></pre></div>
<p>Now let's carry out the given task to print numbers from 1 to 500.</p>
<p>The for loop for this would be something like this, here we have a counter variable named i which would be incremented from 1 to 500.</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="ss">(</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">; i<=500 ; i++ )</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>We can also define the counter variable inside the loop, and then it would have<a href="http://www.thegeekyway.com/what-is-scope-and-lifetime-of-a-variable-in-c-language/" title="What is scope and lifetime of a variable in C language">scope of the loop as explained in tutorial on Scope and lifetime of variables.</a></p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="ss">(</span><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">; i<=500 ; i++ )</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p><strong>Understanding flow of for loop</strong></p>
<p>First time, when the control enters the loop, value of counter i is 1. The test condition would be checked, it would be true in this case, as 1 is less than or equal to 500. Hence control enters the loop and the statement to print value of i would be executed. We don't have more statements here, so control would go to updation, and update the value of i, it would be incremented.</p>
<p>Next the test condition would be again checked, here value of i is 2. So, test condition would be true in this case. Hence, control enters the loop and again print statement would be executed. This time value 2 would be printed. Once again, the control goes to updation statement, and value of i is incremented. It becomes 3 now, and again test condition would be checked.</p>
<p>This would continue till the value reaches 499, after updation, the value of i would be 500. The control again enters the loop, and value of 500 would be printed. Next updation takes place and value of i becomes 501, the test condition here becomes false, because, 501 is not less than or equal to 500. So, the loop would terminate, and the control would go to the next immediate next statement following the loop.</p>
<h3><p #while-loop></p></h3>
<h3><u><strong>Understanding and Using while loop</strong></u></h3>
<p><strong><em>Syntax:</em></strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">initialization</span>
<span class="k">while</span><span class="ss">(</span><span class="nv">test_condition</span><span class="ss">)</span>
{
<span class="w"> </span><span class="o">//</span><span class="nv">all</span><span class="w"> </span><span class="nv">code</span>
<span class="w"> </span><span class="nv">updation</span><span class="w"> </span>
}
</code></pre></div>
<p>In while loop, initialization is done even before control enters the loop, as shown. Updation is done as the last statement of the loop and test condition is given as shown.</p>
<p>Now let us print the numbers from 1 to 500 using while loop.</p>
<div class="highlight"><pre><span></span><code><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="k">while</span><span class="ss">(</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">500</span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">i</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">i</span><span class="o">++</span><span class="w"> </span><span class="c1">;</span>
}
</code></pre></div>
<p>The initialization is need is just once, whatever loop it is, so in while loop, we have done initialization, before entering the while loop.</p>
<p><strong>Understanding flow of while loop</strong></p>
<p>First of all the test condition is checked, which would be true, since 1 is less than or equal to 500, so control enters loop, and the print statement would be executed and updation statement also takes place. There is no other statement to be executed as end of the loop is reached. Before the control leaves the loop, the test condition would be checked, which would be again true as 2 is less than equal to 500.</p>
<p>This would continue until value is 500. Next, the value would be printed, and value is incremented by 1. So, condition is tested again, now, 501 would not be less than or equal to 500. Hence, the test condition becomes false and the control exits the loop executing the immediate next statement following the loop.</p>
<h3><p #do-while-loop></p></h3>
<h3><u><strong>Understanding and Using do - while loop</strong></u></h3>
<p><strong>Syntax:</strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">initialization</span>
<span class="k">do</span><span class="w"> </span>{
<span class="w"> </span><span class="o">//</span><span class="nv">executing</span><span class="w"> </span><span class="nv">statements</span>
<span class="w"> </span><span class="nv">updation</span>
}<span class="w"> </span><span class="k">while</span><span class="ss">(</span><span class="nv">test</span><span class="w"> </span><span class="nv">condition</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p><u>Notice that in do while loop, we have a semi-colon at the end of while statement.</u></p>
<p>Let's now print the numbers from 1 to 500 using do while loop.</p>
<div class="highlight"><pre><span></span><code><span class="nv">int</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="c1">;</span>
<span class="k">do</span><span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"%d n"</span>,<span class="nv">i</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">i</span><span class="o">++</span><span class="c1">;</span>
}<span class="w"> </span><span class="k">while</span><span class="ss">(</span><span class="nv">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">500</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p><strong>Understanding flow of do while loop</strong></p>
<p>The do while loop has an interesting feature, which is it would <strong>execute at least once</strong> whether the condition is true or not.</p>
<p>In the above case, the control enters the loop, and execute the statements. So, the value 1 would be printed. Next updation is done in the value of i, and the condition is checked against value 2, so 2 is less than or equal to 500 and hence the control once again start executing from the first statement. Next the test condition is checked against the value of 3 and so on.</p>
<p>This would continue till value of 500, next after printing value of 500, the updation takes place, and then the condition would become false. The control would get out of the loop and be ready to execute the immediate next statement.</p>
<p #Difference-between-entry-controlled-and-exit-controlled-loop></p>
<h2><u>Difference between Entry Controlled and Exit Controlled loop</u></h2>
<p><strong>Entry Controlled</strong> loops are those in which the test condition is checked first, and then if the condition is true, the control enters the loop and all the statements inside the loops are executed. For example, <u>for</u> and <u>while loop</u> are entry controlled loop.</p>
<p><strong>Exit Controlled</strong> loops are those in which the statements inside loops are executed first, and then the condition is tested. Even if the condition is false, the loop is executed at least once. For example, <u>do-while</u> loop is an exit controlled loop and it is executed at least once, whether the condition is true of false. If the condition is true, the loop is executed one more time, and if it's false, termination occurs, as we have discussed in detail above.</p>
<p>In the next tutorial, we'll learn about structures in the C language.</p>Running A Python Script Using Chef2014-03-18T15:39:00+05:302014-03-18T15:39:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-03-18:/running-python-script-using-chef/<p>Chef is an amazing configuration management tool, widely used in a lot of industries. If you are not familiar with how to run chef/chef-solo, please go through <a href="http://www.thegeekyway.com/learning-basics-chef-solo-example/">this</a> article first. One of the best feature of Chef is it’s collection of resources. Resources allows Chef to handle configuration …</p><p>Chef is an amazing configuration management tool, widely used in a lot of industries. If you are not familiar with how to run chef/chef-solo, please go through <a href="http://www.thegeekyway.com/learning-basics-chef-solo-example/">this</a> article first. One of the best feature of Chef is it’s collection of resources. Resources allows Chef to handle configuration of the designated server. Resource have their predefined task like to run a command, to copy a file, to use a template etc. In this article we are going to talk about script resource. This is going to run any script, it could be bash, python or perl. There are separate resources for these scripts.</p>
<p>You put the resources in your recipe. Here is a simple bash script example:</p>
<div class="highlight"><pre><span></span><code><span class="nv">bash</span><span class="w"> </span><span class="s2">"install_something"</span><span class="w"> </span><span class="k">do</span>
<span class="w"> </span><span class="nv">user</span><span class="w"> </span><span class="s2">"root"</span>
<span class="w"> </span><span class="nv">cwd</span><span class="w"> </span><span class="s2">"/home"</span>
<span class="w"> </span><span class="nv">code</span><span class="w"> </span><span class="o"><<-</span><span class="nv">EOH</span>
<span class="w"> </span><span class="nv">echo</span><span class="w"> </span>“<span class="nv">Hello</span><span class="w"> </span><span class="nv">World</span><span class="o">!</span>”
<span class="w"> </span><span class="nv">ls</span>
<span class="w"> </span><span class="nv">EOH</span>
<span class="k">end</span>
</code></pre></div>
<p>As you can see, the bash resource will run the script in the code section and will print “Hello World” to the terminal and the content of /home directory.</p>
<p>So, when you are going to run a python script, the first choice to come to mind is “python” script resource. But there is a limitation to script resources. They only run in-line scripts. Meaning you will have to write the whole script in the code section and it will be executed one line at a time. But you can’t run an external script. Here comes the “execute” resource to rescue!</p>
<p>To run an external script, the structure of the execute resource is:</p>
<div class="highlight"><pre><span></span><code><span class="nv">filename</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"test.py"</span>
<span class="nv">execute</span><span class="w"> </span><span class="s1">'execute_file'</span><span class="w"> </span><span class="k">do</span>
<span class="w"> </span><span class="nv">cwd</span><span class="w"> </span><span class="s1">'/home/peeyush'</span>
<span class="w"> </span><span class="nv">command</span><span class="w"> </span><span class="s2">"python #{static}"</span>
<span class="k">end</span>
</code></pre></div>
<p>Here the filename contains the name of the script. Then in the execute block, we have cwd which represents the current working directory. This is the directory where the script is present. Then the “command” tells the shell to execute the script. Just put the above lines in your recipe, edit test.py according to the ouput you want and let chef run the script for you! Here is the sample test.py:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-03-18 15:19:33.png" src="https://lh5.googleusercontent.com/JihU-gdBpBp0eeYxeqIlDMBW-HjlSbYLCchDA56_DQqEbdSDuj0CtoD5x_q-Rk9tSrB4VvpOcpuEzf9_ZmQqM4ioc9VO5SqTXviSc7S2a5c6tzKFBAph0PhF28Z4hg"></strong></p>
</div>
<p>As you can see, I am printing “Hello, chef!” and redirecting some out to a text file. Redirection is needed because Chef will just execute the script and won’t show you the output, until it’s an error. Here is what the recipe look like:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-03-18 15:15:20.png" src="https://lh6.googleusercontent.com/Xg83QDTJ7qeMTFUMgqrwtnGydgegGGFx_7NzGhADUch8b2Qzw7YncSMelwzEd7h7xMr7I4GKPpzGszQXdfvGMxxF4vVNOTSfojoeefNOVSBuZhmJ46_TXWWwntDO9A"></strong></p>
</div>
<p>Remember to change the current working directory (cwd) according to your setup.Everything else is same as previous article and you can test it out using chef-solo.</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-03-18 15:18:51.png" src="https://lh4.googleusercontent.com/FXzkjCooBE7AGaHapRX_7IwhwvrADtaJBSbYGiepY-0BXiPI0PVLKNhSDPhO6ZKhB9-6KZTaDE3ln0zmMDFB-WNJD9ELZ20XZeiuO1ryQuqB30yHK0BqmUAU0xvZTA"></strong></p>
</div>Storage Classes in C language2014-03-17T17:55:00+05:302014-03-17T17:55:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-17:/storage-classes-c-language-2/<p>In the previous tutorials, we have discussed about the conditional statements. In this tutorial we'll be learning about something known as Storage Classes in the C language. Every variable you define in the C language is generally associated with a Storage class which basically defines the Scope and lifetime of …</p><p>In the previous tutorials, we have discussed about the conditional statements. In this tutorial we'll be learning about something known as Storage Classes in the C language. Every variable you define in the C language is generally associated with a Storage class which basically defines the Scope and lifetime of that variable.</p>
<p>Before we get into detail about this, let's first understand<a href="http://www.TheGeekyWay.com/what-is-scope-and-lifetime-of-a-variable-in-c-language">what is exactly meant by scope and lifetime of variable.</a></p>
<p>Now, let us take a look at different types of storage classes available in C programming language:</p>
<ul>
<li>auto</li>
<li>register</li>
<li>static</li>
<li>extern</li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/storage_class1.png" title="Storage classes in C language and keywords used to define them. auto, extern, static and register."><img alt="Storage classes in C language and keywords used to define them. auto, extern, static and register." src="//thegeekyway.com/uploads/images/2014/03/storage_class1.png"></a></p>
</div>
<p>Now, we'll understand each one of this in detail :</p>
<p><u><strong>Auto (Automatic) :</strong></u></p>
<p>The default storage class of any variable is Automatic . It is created as soon as the declaration statement is encountered and is destroyed as soon as the program control leaves the block which contains your variable.</p>
<p style="font-family: comic sans ms,sans-serif;">*<u>Keyword used - auto</u>*</p>
<p>For example, here we define two variables, first, count, which is a local variable and second, counter inside a block (local variable using auto class).</p>
<div class="highlight"><pre><span></span><code>{
int count;
auto int counter;
}
</code></pre></div>
<p><u><strong>External Storage Class (Extern) :</strong></u></p>
<p>They are the Global Variables, i.e. declared outside the function block but are accessible inside the function block as well. They are accessible by all the function blocks present in the program and hence they have lifetime for entire program.</p>
<p>It is actually used to give a reference to the global variable which can be accessed in all your files. If we need to have a global variable, then in one file, we use that particular variable or function, and in the other file we define it by keyword extern so that when it is used in our other file, the reference is used from the first file.</p>
<p style="text-decoration: underline;"> <p style="font-family: comic sans ms,sans-serif;">Keyword used - extern</p></p>
<p>For example, let's have two files, first which would have our main function (main.c) and second one having a function named extern_storage (extern.c)</p>
<p>First file : main.c</p>
<div class="highlight"><pre><span></span><code>int counter = 1 ; // Global Variable
void main()
{
extern_storage();
counter++;
extern_storage();
counter--;
extern_storage();
}
</code></pre></div>
<p>Second file : extern.c</p>
<div class="highlight"><pre><span></span><code>void extern_storage(void);
extern int counter;
void extern_storage(void)
{
printf("Current value of extern counter is %d n", &counter);
}
</code></pre></div>
<p>When the above two files are executed, the function extern_storage picks up the current value from our main.c file. So, the following output will be produced.</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 1</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 2</p>
<p style="font-family: courier new,courier;">Current value of extern counter is 1</p>
<p><u><strong>Static Storage Class (static) :</strong></u></p>
<p>It has visibility of a local variable but lifetime of external variable. Thus, once defined, it would not be destroyed after the function finishes execution but would retain it's value in memory and can be used further in future function calls.</p>
<p style="text-decoration: underline;">* <p style="font-family: comic sans ms,sans-serif;">Keyword used - static</p>*</p>
<p>(Self-Explanatory through post on <a href="http://www.thegeekyway.com/what-is-scope-and-lifetime-of-a-variable-in-c-language/" title="What is scope and lifetime of a variable in C language">Scope and lifetime of variables</a>)</p>
<p><u><strong>Register Storage Class (register) :</strong></u></p>
<p>It is very similar in behavior to an automatic storage class variable. The difference between the two comes from the fact that auto storage class uses main memory for storage but register storage class variables might use CPU Registers.</p>
<p><u><em><strong>NOTE:</strong> register keyword doesn't guarantee that CPU Registers would be used every time, it depends on hardware and other restrictions that may be there.</em></u></p>
<p>The use of these registers facilitates faster accessing and helps in running programs faster. Thus variables that need quick access like variables for storing counter values should be declared by using register keyword.</p>
<p>For example :</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="w"> </span><span class="n">register</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p><em>We hope the concept of storage classes is now clear ! Have any doubt or something to say? Let us know through comments section below.</em></p>Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Ubuntu/Debian. Part-32014-03-11T08:10:00+05:302014-03-11T08:10:00+05:30Abhishek_Rawaltag:thegeekyway.com,2014-03-11:/manage-package-apt-get-apt-cache-ubuntu-commands/<p>This is last & final chapter of this tutorial. Before proceeding further, I would like to link first two parts here just incase you haven't checked it out.</p>
<p>(<a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu/" title="Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-I">PART-1</a> & <a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu-part-2/" title="Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-2">PART-2</a>)</p>
<p>Continuing from Part-2,</p>
<p><strong>19) Checking The Stats Of Cache Using APT-CACHE :</strong></p>
<p>This command will display all the statistics of the cache …</p><p>This is last & final chapter of this tutorial. Before proceeding further, I would like to link first two parts here just incase you haven't checked it out.</p>
<p>(<a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu/" title="Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-I">PART-1</a> & <a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu-part-2/" title="Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-2">PART-2</a>)</p>
<p>Continuing from Part-2,</p>
<p><strong>19) Checking The Stats Of Cache Using APT-CACHE :</strong></p>
<p>This command will display all the statistics of the cache, from total number of packages to total number of dependencies, total number of globbed strings, slack space ,etc.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache stats
</code></pre></div>
<p><strong>20) Listing All The Packages Available In The Repository Using APT-CACHE :</strong></p>
<p>Using this command you can list all the packages that are available in the repository.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache pkgnames
</code></pre></div>
<p><strong>21) Checking The Package Details Using APT-CACHE :</strong></p>
<p>This command will give you details of any package available in the repository, alongwith maintainer's name, description details, size, dependency, architecture,etc.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code><span class="nv">sudo</span><span class="w"> </span><span class="nv">apt</span><span class="o">-</span><span class="nv">cache</span><span class="w"> </span><span class="k">show</span><span class="w"> </span><span class="o"><</span><span class="nv">package_name</span><span class="o">></span>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code><span class="nv">sudo</span><span class="w"> </span><span class="nv">apt</span><span class="o">-</span><span class="nv">cache</span><span class="w"> </span><span class="k">show</span><span class="w"> </span><span class="nv">minitube</span>
</code></pre></div>
<p><strong>22) Searching The Package Using APT-CACHE :</strong></p>
<p>You can find the package name alongwith description using the following command syntax. Very handy tool when you're looking for similar name of packages.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache search <word_close_to_package_you're_searching_for>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache search mini
</code></pre></div>
<p>You'll see that first results of search will give me all packages name with prefix 'mini'. This can be helpful when you don't know correct package name.</p>
<p><strong>23) Getting Dependency Details Using APT-CACHE :</strong></p>
<p>You can easily obtain dependency details using following command. It also gives details whether those packages are already installed in your system or not.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache showpkg <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-cache showpkg vlc
</code></pre></div>
<p>There are chances that I might've missed few commands. You can suggest some of commands in the comments below, so I can add it.</p>
<p>However, you can get more details of apt-get & apt-cache by typing "man apt-get" & "man apt-cache" respectively from Debian/Ubuntu system.</p>Convert GIMP Into Photoshop Like UI2014-03-10T08:11:00+05:302014-03-10T08:11:00+05:30Abhishek_Rawaltag:thegeekyway.com,2014-03-10:/convert-gimp-photoshop-like-ui/<p>An artist in Deviantart with profile name <a href="http://doctormo.deviantart.com/" title="Doctormo Profile">'doctormo'</a> published an awesome tweak which let's you to change the user-interface of the GIMP something similar to world popular image editing software Photoshop. This tweak currently works on GIMP 2.8 .It does works for GIMP 2.9 but some features might …</p><p>An artist in Deviantart with profile name <a href="http://doctormo.deviantart.com/" title="Doctormo Profile">'doctormo'</a> published an awesome tweak which let's you to change the user-interface of the GIMP something similar to world popular image editing software Photoshop. This tweak currently works on GIMP 2.8 .It does works for GIMP 2.9 but some features might be missing</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/TGW-imported-3.0-RGB-color-1-layer-351x320-%e2%80%93-GIMP_0051.png"><img alt="[TGW] (imported)-3.0 (RGB color, 1 layer) 351x320 – GIMP_005" src="//thegeekyway.com/uploads/images/2014/03/TGW-imported-3.0-RGB-color-1-layer-351x320-%e2%80%93-GIMP_0051.png"></a></p>
</div>
<p><strong>Steps To Installation :</strong></p>
<p>1) Download Tweaks.zip from <a href="http://doctormo.deviantart.com/art/Gimp-2-8-Photoshop-Tweaks-432736644" title="Download Tweaks">Official Page</a>.</p>
<p>2) Make backup of your current GIMP configuration (Just incase if anything goes wrong, you can back it up). To do so, go to /home and press 'Ctrl+h' it will show hidden files. Now copy entire folder named <strong>'.gimp-2.8'</strong> to anywhere but /home</p>
<p>3) Extracted the downloaded .zip file</p>
<p>4) It will contain file named .gimp-2.8</p>
<p>5) Simply copy that file & paste it in /Home & overwrite it on current .gimp-2.8</p>
<p>And you're done!</p>
<p><strong>NOTE :</strong> You can perform this tweak in <strong>Windows</strong> & <strong>OS X</strong> operating systems too. I don't use Windows nor OS X so I don't know how to do it. But I guess from these steps you can figure it out on your own.</p>Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-22014-03-10T05:36:00+05:302014-03-10T05:36:00+05:30Abhishek_Rawaltag:thegeekyway.com,2014-03-10:/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu-part-2/<p>This is second part of the tutorial.You would like to read the first part before you proceed further. Here is the <a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu/" title="First Part"><strong>LINK</strong></a>.</p>
<p>Continuing from the first part,</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/slide_apt-11.png" title="APT Ubuntu"><img alt="APT Ubuntu" src="//thegeekyway.com/uploads/images/2014/03/slide_apt-11.png"></a></p>
</div>
<p><strong>11) Removing Packages From The System Using APT-GET :</strong></p>
<p>This command will help you to remove the installed packages from the system. However …</p><p>This is second part of the tutorial.You would like to read the first part before you proceed further. Here is the <a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu/" title="First Part"><strong>LINK</strong></a>.</p>
<p>Continuing from the first part,</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/slide_apt-11.png" title="APT Ubuntu"><img alt="APT Ubuntu" src="//thegeekyway.com/uploads/images/2014/03/slide_apt-11.png"></a></p>
</div>
<p><strong>11) Removing Packages From The System Using APT-GET :</strong></p>
<p>This command will help you to remove the installed packages from the system. However, you should note that it won't remove configuration files at all. Which means, if you remove the application using this command & then re-install it later, then the application settings you had previously will still be there.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get remove <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get remove vlc
</code></pre></div>
<p><strong>12) Removing Packages With Configuration Settings Using APT-GET :</strong></p>
<p>Using this command, you can completely remove packages alongwith configuration settings. So, if you will reinstall that application later, you'll have to reconfigure again. This command can be extremely useful when you're messing with configuration settings & something goes extremely wrong & you cannot back it up to original state. Removing with this command & re-installing the package can help you in such case.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get purge <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get purge synaptic
</code></pre></div>
<p><strong>13) Cleaning Up The Disk Space Using APT-GET :</strong></p>
<p>It will cleanup the disk space by cleaning debian packages (.deb) that are downloaded from local repository.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get clean
</code></pre></div>
<p><strong>14) Auto Cleaning The Cache Using APT-GET :</strong></p>
<p>The packages which users download is usually stored in /var/cache/apt/archives in form of cache, consuming hefty amount of space. You can simply remove all the .deb cache packages from this directory using this command.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get autoclean
</code></pre></div>
<p><strong>15) Observe The Changelog Of The Package :</strong></p>
<p>Using the following command you can view the changelog of the desired application. It is usually useful to see what new features are included in the latest build or upgrade.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get changelog <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get changelog wine
</code></pre></div>
<p><strong>16) Fixing Missing Dependencies & Re-installing The Package Using APT-GET :</strong></p>
<p>There are times when you're having poor internet connectivity, some packages are unable to retrieve but easily can. However, an application depends on some of it's dependencies, due to such problems it doesn't meet the require dependency & package won't get install. So, the following command usually helpful when the dependencies are missing & you want to install the missing dependencies to fix the problem.</p>
<p>Command:</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install -f
</code></pre></div>
<p><strong>17) Auto-Removing The Installed Packages Using APT-GET :</strong></p>
<p>Sometimes, when you remove any package, it's dependencies are not removed with it. They remain in system as 'Orphan'. To remove the Orphan packages auto-remove command is used.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get autoremove
</code></pre></div>
<p><strong>18) Checking The Dependencies Of The Application Using APT-GET :</strong></p>
<p>You can check the broken dependencies required for the package you wanna install using the following command. Quite handy, it is!</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get check
</code></pre></div>
<p><em>Rest of the commands that are related to APT-CACHE command will be covered in next part. i.e Part-3</em></p>What is scope and lifetime of a variable in C language2014-03-10T00:28:00+05:302014-03-10T00:28:00+05:30sanyam_khuranatag:thegeekyway.com,2014-03-10:/what-is-scope-and-lifetime-of-a-variable-in-c-language/<p>Welcome to your next tutorial, here we would be discussing about the scope and lifetime of a variable, and how exactly they are similar yet different from each other. We'll try to understand all this by using an example to understand the concepts in a better way.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png" title="Scope and life time of a variable in C programming language"><img alt="Scope and life time of a variable in C programming language" src="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png"></a></p>
</div>
<p>So, let's understand …</p><p>Welcome to your next tutorial, here we would be discussing about the scope and lifetime of a variable, and how exactly they are similar yet different from each other. We'll try to understand all this by using an example to understand the concepts in a better way.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png" title="Scope and life time of a variable in C programming language"><img alt="Scope and life time of a variable in C programming language" src="//thegeekyway.com/uploads/images/2014/03/scope-and-lifetime-variable-c1.png"></a></p>
</div>
<p>So, let's understand each of these :</p>
<p><strong>Life Time</strong> - Life time of any variable is the time for which the particular variable outlives in memory during running of the program.</p>
<p><strong>Scope</strong> - The scope of any variable is actually a subset of life time. A variable may be in the memory but may not be accessible though. So, the area of our program where we can actually access our entity (variable in this case) is the scope of that variable.</p>
<p>The scope of any variable can be broadly categorized into three categories :</p>
<ul>
<li><strong>Global scope</strong> : When variable is defined outside all functions. It is then available to all the functions of the program and all the blocks program contains.</li>
<li><strong>Local scope</strong> : When variable is defined inside a function or a block, then it is locally accessible within the block and hence it is a local variable.</li>
<li><strong>Function scope</strong> : When variable is passed as formal arguments, it is said to have function scope.</li>
</ul>
<p>Let's understand this with help of an example :</p>
<p>Consider this snippet of program :</p>
<div class="highlight"><pre><span></span><code><span class="c1">#include<stdio.h></span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">declared</span>
<span class="nb nb-Type">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">declared</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global variable is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Local variable is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">local</span><span class="p">);</span>
<span class="w"> </span><span class="n">func1</span><span class="p">();</span>
<span class="p">}</span>
<span class="nb nb-Type">void</span><span class="w"> </span><span class="n">func1</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Would</span><span class="w"> </span><span class="nb">print</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">successfully</span><span class="o">.</span>
<span class="p">}</span>
</code></pre></div>
<p>Here we have a variable named global which is defined outside all the functions (generally at the top of the program) , it is defined even outside main, and hence would be available to every function defined in the program , here we consider only one function func1() .</p>
<p>The output of the above program would be :</p>
<p style="font-family: arial,helvetica,sans-serif;">Global variable is 100</p>
<p style="font-family: arial,helvetica,sans-serif;">Local variable is 10</p>
<p style="font-family: arial,helvetica,sans-serif;">Global inside func1 is 100</p>
<p>Let us consider the same program again but this time, let us change the definition of our function func1() as:</p>
<div class="highlight"><pre><span></span><code><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">func1</span><span class="p">()</span><span class="w"> </span>
<span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Global inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="n">global</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Would</span><span class="w"> </span><span class="nb">print</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">successfully</span><span class="o">.</span><span class="w"> </span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s2">"Local inside func1 is </span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span><span class="w"> </span><span class="n">local</span><span class="p">);</span><span class="w"> </span><span class="o">/*</span><span class="w"> </span><span class="n">It</span><span class="w"> </span><span class="n">would</span><span class="w"> </span><span class="n">produce</span><span class="w"> </span><span class="n">an</span><span class="w"> </span><span class="n">error</span><span class="p">,</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="n">local</span>
<span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">accessed</span><span class="w"> </span>
<span class="w"> </span><span class="n">only</span><span class="w"> </span><span class="n">inside</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="n">function</span><span class="o">.</span><span class="w"> </span><span class="o">*/</span>
<span class="p">}</span>
</code></pre></div>
<p>Clearly, as mentioned in the comment, the printing of global variable would be done, but that of local variable would produce an error, as the variable named local is actually local to the function main() and hence can be accessed only through that function not from any other function, but for global variable, it can be accessed by all functions/ blocks a program is using.</p>
<p>Here our variable named global has global scope and lifetime, hence it outlives in memory till the program execution is completed and can be accessed by any function / block locally too. But in case of our local variable, it may outlives in memory till program execution is completed but it can be accessed from only within the function or block it is defined in.</p>
<p>The basic problem here can be, we can have variable with same name defined as a global as well as local variable, and hence, it would be ambiguos as to which variable we want to actually refer to. We can resolve this issue in C++ (Advancement of C language) by the use of a special operator known as scope resolution operator. We'll look into this, when we'll cover tutorials for C++.</p>
<p>I hope all the concepts of scope and lifetime are now clear, but still if you have any doubt/ suggestion, please let us know through the comments section below.</p>Learn To Manage Packages Using APT-GET & APT-CACHE Commands In Debian/Ubuntu. Part-I2014-03-09T05:43:00+05:302014-03-09T05:43:00+05:30Abhishek_Rawaltag:thegeekyway.com,2014-03-09:/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu/<p><strong>APT</strong> abbreviated as <strong>A</strong>dvanced <strong>P</strong>ackaging <strong>T</strong>ool, is the package management tool used in Debian based distributions to install new applications from repository, removing installed applications, upgrading the system, updating the repository and bunch full of other useful tasks, all that from terminal itself.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/abhishek@abhishek-desktop-_0021.png" title="APT"><img alt="APT" src="//thegeekyway.com/uploads/images/2014/03/abhishek@abhishek-desktop-_0021.png"></a></p>
</div>
<!--more-->
<p><strong>1) Installing Application In …</strong></p><p><strong>APT</strong> abbreviated as <strong>A</strong>dvanced <strong>P</strong>ackaging <strong>T</strong>ool, is the package management tool used in Debian based distributions to install new applications from repository, removing installed applications, upgrading the system, updating the repository and bunch full of other useful tasks, all that from terminal itself.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/03/abhishek@abhishek-desktop-_0021.png" title="APT"><img alt="APT" src="//thegeekyway.com/uploads/images/2014/03/abhishek@abhishek-desktop-_0021.png"></a></p>
</div>
<!--more-->
<p><strong>1) Installing Application In Ubuntu/Debian Using APT-GET</strong></p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install gimp
</code></pre></div>
<p>Where gimp is the package name which is to be installed.</p>
<p><strong>2) Updating The System Repository Using APT-GET</strong></p>
<p>This command will update the Debian/Ubuntu repository with the newer latest version that's available.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get update
</code></pre></div>
<p><strong>3) Upgrading The Software Packages Using APT-GET</strong></p>
<p>It is usually used to upgrade the installed packages in the system. For example, if new updated package are available in the repository, then this command will upgrade the installed package with the latest one that is available in repository.</p>
<p>Command :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get upgrade
</code></pre></div>
<p><strong>4) Upgrading Specific Desired Packages Using APT-GET</strong></p>
<p>Using this command you can upgrade stacks of desired packages you want. The command is very much similar to that of installing packages.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install <package_name> --only-upgrade
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install gimp --only-upgrade
</code></pre></div>
<p><strong> 5) Installing Packages Without Upgrading Using APT-GET</strong></p>
<p>The following command will install the desired package, but won't upgrade even if the upgrade is available.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install <package_name> --no-upgrade
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install wine --no-upgrade
</code></pre></div>
<p><strong>6) Installing Multiple Packages All At Once In One Command Using APT-GET</strong></p>
<p>This command is used to install as many packages/application you want using one single command. Quite a productive command it is!</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install <package_1> <package_2> <package_3>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install gimp wine vlc
</code></pre></div>
<p><strong> 7) Install Only Desired Version Package Using APT-GET</strong></p>
<p>Inserting '<strong>=</strong>' after the package name and appending desired version, you can install specific version of the package</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install <package_name>=<version_number>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install wine=1.7
</code></pre></div>
<p><strong>8) Download Only Source Code Of The Desired Package Using APT-GET</strong></p>
<p>You can download source code of the desired package from the repository using following syntax.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code><span class="n">sudo</span><span class="w"> </span><span class="n">apt</span><span class="o">-</span><span class="n">get</span><span class="w"> </span><span class="o">--</span><span class="n">download</span><span class="o">-</span><span class="n">only</span><span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="o"><</span><span class="n">package_name</span><span class="o">></span>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code><span class="n">sudo</span><span class="w"> </span><span class="n">apt</span><span class="o">-</span><span class="n">get</span><span class="w"> </span><span class="o">--</span><span class="n">download</span><span class="o">-</span><span class="n">only</span><span class="w"> </span><span class="n">source</span><span class="w"> </span><span class="n">synapse</span>
</code></pre></div>
<p><strong>9) Downloading The .deb Package Without Installing It Using APT-GET</strong></p>
<p>You can download the .deb package of the desired application using following command. The downloaded .deb package is usually stored in /var/cache/apt/archive directory.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code><span class="n">sudo</span><span class="w"> </span><span class="n">apt</span><span class="o">-</span><span class="n">get</span><span class="w"> </span><span class="n">download</span><span class="w"> </span><span class="o"><</span><span class="n">package_name</span><span class="o">></span>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code><span class="n">sudo</span><span class="w"> </span><span class="n">apt</span><span class="o">-</span><span class="n">get</span><span class="w"> </span><span class="n">download</span><span class="w"> </span><span class="n">synaptic</span>
</code></pre></div>
<p><strong>10) Download The Source Code, Decompress It & Then Compile It Using APT-GET</strong></p>
<p>You can download source code of the application you want to use, then decompress it & compile it using one single command.</p>
<p>Command Syntax :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get --compile source <package_name>
</code></pre></div>
<p>Example :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get --compile source gimp
</code></pre></div>
<p><em>Dozens of other commands will be covered in next part of the tutorial. Stay tuned.</em></p>
<p>Edit : You can read Part-2 Of The Tutorial From <a href="http://www.thegeekyway.com/learn-manage-packages-using-apt-get-apt-cache-commands-debianubuntu-part-2/" title="Part-2 Of Tutorial"><strong>HERE</strong></a></p>Benchmarking OpenStack With Rally2014-02-28T11:48:00+05:302014-02-28T11:48:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-02-28:/benchmarking-openstack-rally/<p>When you are trying to make changes in a code, there is a big chance of failure. And if the system is as huge as OpenStack, it becomes really hard to figure the error out. Here comes Rally. Rally makes benchmarking of OpenStack simple and automated. Rally works in three …</p><p>When you are trying to make changes in a code, there is a big chance of failure. And if the system is as huge as OpenStack, it becomes really hard to figure the error out. Here comes Rally. Rally makes benchmarking of OpenStack simple and automated. Rally works in three steps:</p>
<p><strong>1. Deployment:</strong> Rally is not a deployment tool by itself, instead it works as a plugin with devstack, RDO etc.</p>
<p><strong>2. Verification:</strong> Here Rally uses tempest to verify the OpenStack cloud deployment.</p>
<p><strong>3. Benchmarking:</strong> This step is used to calculate load and performance of the particular cloud deployment.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/openstack-logo1.jpg" title="Openstack with Rally"><img alt="Openstack with Rally" src="//thegeekyway.com/uploads/images/2014/02/openstack-logo1.jpg"></a></p>
</div>
<p>Rally generally works a command line tool (GUI is work in progress) and helps in detecting scaling and performance issues, figuring out how the code change affects the system and finding out suitable architecture of deployment etc.</p>
<p>The first step is to install rally in OpenStack environment. I am going to use rally as a plugin with devstack, though it is possible to install it manually in an already deployed environment. To start, clone the following repositories from github:</p>
<div class="highlight"><pre><span></span><code>git clone https://github.com/openstack-dev/devstack
git clone https://github.com/stackforge/rally
</code></pre></div>
<p>Now, execute the following commands from your terminal in order to set devstack up with rally:</p>
<div class="highlight"><pre><span></span><code>cp rally/contrib/devstack/lib/rally devstack/lib/
cp rally/contrib/devstack/extras.d/70-rally.sh devstack/extras.d/
cd devstack
echo "enable_service rally" >> localrc
</code></pre></div>
<p>Next, as usual, we run the installation script of devstack:</p>
<div class="highlight"><pre><span></span><code>./stack.sh
</code></pre></div>
<p>Go for a coffee or take a walk, until devstack completes its work!</p>
<p>After the installation is done, source the openrc file present in \~/.rally/ directory to start using OpenStack.</p>
<div class="highlight"><pre><span></span><code>source ~/.rally/openrc
</code></pre></div>
<p>To use rally, we first need to tell it about the OpenStack deployment which is going to be benchmarked. To do that, we have a deployment configuration file. That means, you need to provide configuration details of your OpenStack deployment in form of a json file. Go to the rally directory and create a my-deployment.json file. The content of the file should be like this:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-02-27 17:20:18.png" src="https://lh6.googleusercontent.com/kRZy9MBq6cBMDPrVgwm9nXRDrBasz2tbBqXJvTmGD0FrU1eUOTucylKtHzYVU5BHmz1OfDQRxzzoyO1s6qccUofo0ILWNx_WZX3QmGoK7AAcSfjemvwQmLs1BQ"></strong></p>
</div>
<p>You can find the relevant information for you system in openrc file. Next, we create a deployment named test using the following command:</p>
<div style="text-align:center">
<p><strong><em>*</em>*<img alt="Screenshot from 2014-02-27 17:19:54.png" src="https://lh3.googleusercontent.com/_lAcpSqYhUncDSomOX8fjW_vcMthYAAU5L_HhNuHK3V4ZpEYlhFToDctsPE9Z7HWOc6pAqBKAg_rlnVqxXa2IWLyEBm5rrG_S0rkJcZNJNsPaxzpt1EZp2zuRw"></strong>****</p>
</div>
<p>Run ‘rally deployment check’ to check if current deployment is ready to be benchmarked. You should get an output like this:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-02-27 17:21:26.png" src="https://lh6.googleusercontent.com/CujE4Dm-jfe4s5aSB3eCpNwxWQWqedxz6oSFuvHZoSmppfLzmz17nxQb-6W_lUsNBds9XpjEA1PHyy8M4dyUh6adwwA7w7Euz7PN_Oe-hQlH8MHIszqKrmbj-w"></strong></p>
</div>
<p>Next, we come to benchmarking. There are a lot of ways to test the deployment, we will use an example that comes with rally. This is a boot and delete test. First copy the test to you current directory:</p>
<div class="highlight"><pre><span></span><code>cp doc/samples/tasks/nova/boot-and-delete.json my-task.json
</code></pre></div>
<p>Now, edit the my-task.json file to look like:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-02-27 17:25:25.png" src="https://lh6.googleusercontent.com/Wi-J5GrWHYqoyY-buqPouTYbeuSrsqwTVDhmqCL0j7CKpJCDaasJpZhuz-TRdb7Ew5w1B-5ikyQpFBSGp_uHG_RYBGOvnrDr9JXhAVTwNfNbouHZxAwvvYxyOQ"></strong></p>
</div>
<p>Edit the falvor_id and image_id according to your system. Use “glance image-list” and “nova flavor-list” commands. And now we are ready to run our first task. Type this on your machine:</p>
<div class="highlight"><pre><span></span><code>rally --verbose task start --task=my-task.json
</code></pre></div>
<p>You should see the output:</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-02-27 17:29:29.png" src="https://lh6.googleusercontent.com/6RAxgAkO135b4iMW1sn8ghAHojAA1aJmLamDp_CqtYHSwAbqL6Dy9WjOoN5dM1FDEISZp8YRg-Ij5az03jfYXiExERGk21Tx5eqPeS9DQI2SOL17cdDpNiR5Pw"></strong></p>
</div>
<p>You can also find out the detailed report about all the task.</p>
<div style="text-align:center">
<p><strong><img alt="Screenshot from 2014-02-27 17:30:25.png" src="https://lh5.googleusercontent.com/nZ35zyKAr8V8rPF9HJ9revry8MVpdjZo7q5wwqAwImn1_ccLQt9lP-pj-MEsIG9E7OS4oWEMPXzWPpCTE_mX0IvkzXFrO2XPKGvZk5n_G6Cy8rfbpO-3jYF2yg"></strong></p>
</div>
<p>There are other examples available in the doc/samples/ directory. You can learn more about test cases from these files. Happy benchmarking!</p>Block ads to make your web experience beautiful and speed up your Internet2014-02-27T21:52:00+05:302014-02-27T21:52:00+05:30Anooptag:thegeekyway.com,2014-02-27:/block-ads-speed-internet-2/<p>Everyone of us annoyed by creepy ads everywhere on internet. From mailbox to keep waiting for video in Youtube.</p>
<p>But ads not only makes your internet looks ugly but most of them sneak cookie, steal other website data and track your internet activity. Also these ads eats up your precious …</p><p>Everyone of us annoyed by creepy ads everywhere on internet. From mailbox to keep waiting for video in Youtube.</p>
<p>But ads not only makes your internet looks ugly but most of them sneak cookie, steal other website data and track your internet activity. Also these ads eats up your precious bandwidth.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/home-splash-stop1.png" title="Block ads for smoother experience"><img alt="Block ads for smoother experience" src="//thegeekyway.com/uploads/images/2014/02/home-splash-stop1.png"></a></p>
</div>
<p>So here is one time little setup to get rid of ads and tracking.</p>
<p>1. <a href="#host">Block ads and tracking from Host file</a></p>
<p>2. <a href="#adblock">Install Adblock extension to Firefox/Chrome to block all ads, even in Youtube </a></p>
<p><strong> <p style="font-size: large;"> <p #host></p> Block ads and tracking from Host file </p></strong></p>
<p>Step1. Locate your host file</p>
<p>Linux: <p style="font-family: 'courier new', courier;">\$ sudo nano /etc/hosts </p></p>
<p>Windows: <p style="font-family: 'courier new', courier;"> %systemroot%System32driversetc (right click on host file and open with notepad)</p></p>
<p>Step2. To edit host file, you need roor/administrator privilege. If direct editing not allowed save the host file somewhere else and then replace original host including these line. Remember while saving host file do not add any extension.</p>
<p>(Geeky Tip: If you open notpad/text editor with run as administrator, you can directly edit host file and save. For Linux user you already opening file using sudo.)</p>
<p>I found these list <a href="http://someonewhocares.org/hosts/">here</a>, and also added few server to remove annoying Skype ads.</p>
<p>If following embed file talking long time to load find <a href="https://docs.google.com/file/d/0B5_j19lviiqyQURvM2o3Wkt0SFE/preview?pli=1">Google Drive notepad here.</a> Just click inside following copy all (Ctrl+a --> Ctrl+c) and paste at end of host file. </p>
<iframe width="100%" height="580" src="https://docs.google.com/file/d/0B5_j19lviiqyQURvM2o3Wkt0SFE/preview?pli=1" max-width="640"></iframe>
<p>To get rid of all ads from webpage use following.</p>
<p><strong> <p style="font-size: large;"> <p #adblock></p> Install Adblock extension to Firefox/Chrome to block all ads, even in Youtube </p></strong></p>
<p>Chrome extension : A<a href="https://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom">dblock</a></p>
<p>Firefox extension: <a href="https://addons.mozilla.org/en-US/firefox/addon/adblock-plus/developers?src=api">AdblockPlus</a></p>
<p>This setup definitely remove all ads, also from YouTube.</p>
<p>If you find any other server to add in host file, let us know.</p>XHTML and CSS -2- Understanding Basic Semantics of Webpage2014-02-26T20:52:00+05:302014-02-26T20:52:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-26:/xhtml-css-2-understanding-basic-semantics-webpage-2/<p>In the previous tutorial, we took a brief overview, of what we'll be going to discuss in this tutorial series and how to see the rendered XHTML coding of any webpage. You can see the <a href="http://www.thegeekyway.com/video-tut-xhtml-and-css-1-overview-downloading-text-editor/" title="XHTML and CSS – 1 – Overview and Downloading Text Editor">previous tutorial</a> , in this video tutorial, we'll first understand the difference between XHTML and a …</p><p>In the previous tutorial, we took a brief overview, of what we'll be going to discuss in this tutorial series and how to see the rendered XHTML coding of any webpage. You can see the <a href="http://www.thegeekyway.com/video-tut-xhtml-and-css-1-overview-downloading-text-editor/" title="XHTML and CSS – 1 – Overview and Downloading Text Editor">previous tutorial</a> , in this video tutorial, we'll first understand the difference between XHTML and a Programming Language and know why it is not a programming language but a Mark - Up language. We'll go through the Basic Semantics of any webpage, and understand about tags used in XHTML.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/VideoTut2XHTMLandCSS1.jpg" title="Free video tutorial - understanding basic semantics of the webpage"><img alt="Free video tutorial - understanding basic semantics of the webpage" src="//thegeekyway.com/uploads/images/2014/02/VideoTut2XHTMLandCSS1.jpg"></a></p>
</div>
<p>We'll know about the head section, body sections and the "sandwich" type structure of the webpage, and how the two sections are important and what they include in them.</p>
<p>Here is the video tutorial : <a href="http://www.youtube.com/watch?v=Jy3IqfdSVjI"><strong>Understanding Basic Semantics of a WebPage</strong></a></p>
<iframe width="425" height="350" src="http://www.youtube.com/embed/Jy3IqfdSVjI" frameborder="0"></iframe>
<p>If you have any questions or doubts in this tutorial, please feel free to ask in the comments section below, and don't forget to <a href="http://www.youtube.com/user/TheGeekyWayDotCom">SUBSCRIBE us on YOUTUBE</a>.</p>Conditional Statements in Python2014-02-26T17:44:00+05:302014-02-26T17:44:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-02-26:/conditional-statements/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg" title="Conditional Statements in python"><img alt="Conditional Statements in python" src="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg"></a></p>
</div>
<p>In this tutorial we will discuss about conditional statements in python. Conditional statements are used to perform different computation based on the <strong>Boolean expression</strong> which turns out to be true or false . <strong>Boolean expression</strong> is on which these conditional statement works. Various Conditional statement used are:</p>
<ul>
<li>If</li>
<li>Else</li>
<li>Else if …</li></ul><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg" title="Conditional Statements in python"><img alt="Conditional Statements in python" src="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg"></a></p>
</div>
<p>In this tutorial we will discuss about conditional statements in python. Conditional statements are used to perform different computation based on the <strong>Boolean expression</strong> which turns out to be true or false . <strong>Boolean expression</strong> is on which these conditional statement works. Various Conditional statement used are:</p>
<ul>
<li>If</li>
<li>Else</li>
<li>Else if</li>
</ul>
<p><strong>If</strong> condition</p>
<p>The general syntax for using if statement is</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="ss">(</span><span class="nv">Boolean</span><span class="w"> </span><span class="nv">expression</span><span class="ss">)</span><span class="w"> </span>:
<span class="w"> </span><span class="nv">code</span><span class="w"> </span>#<span class="nv">indentation</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="nv">required</span>
</code></pre></div>
<p><strong>if</strong> is a keyword used for if statements ,it is a followed by a boolean expression which is evaluated . Now question arises is why space is given in front when we have written code statement . This is because as no brackets are used to hold the computations after the if statement as this is a case in c++,java which uses curly brackets {}. The symbol : signifies that from here if block starts .</p>
<p>In Python indentation is given rather than using brackets . If we not give the indentation compiler will give an error in case when no line is there in the if block .</p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">2</span><span class="c1">;</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">==</span><span class="mi">2</span><span class="ss">)</span>:
<span class="nv">print</span><span class="w"> </span><span class="nv">a</span>
<span class="k">else</span>
<span class="nv">print</span><span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"><img alt="Screenshot-7" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"></a></p>
</div>
<p>If statement can be used with else statements and can be used alone . If the condition evaluates to true then code inside if block is executed otherwise it is skip to the next line.</p>
<p><strong>else</strong> condition</p>
<p>The syntax of the else statement is</p>
<div class="highlight"><pre><span></span><code><span class="k">else</span><span class="w"> </span><span class="o">:</span>
<span class="w"> </span><span class="n">code</span>
</code></pre></div>
<p>Here else is a keyword used and there are no brackets used as in if statement ,else is also followed by the code segment and indentation is required. Else statement are followed by if statement and cant be used alone . Compiler checks the if block first if Boolean expression doesn't come out to be true ,code inside else statement gets executed.</p>
<p><strong>Example</strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">2</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">==</span><span class="mi">2</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="k">else</span><span class="w"> </span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<p>In this example a has been assigned a value 2 , after that value of a is checked by using if statement . The statement (a==2) means a is compared to the value 2 if it comes to be true if statement will execute otherwise else statement will execute. The output for this case will be "YES".</p>
<p><strong>Elif</strong></p>
<p>To avoid multiple if else statements we use else if .It is just like a else followed by a if .elif is a keyword used for else-if statemnts . This example will tell the use of elif keyword.</p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">9</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">%</span><span class="mi">2</span><span class="o">==</span><span class="mi">0</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="nv">elif</span><span class="w"> </span><span class="ss">(</span><span class="nv">a</span><span class="o">%</span><span class="mi">3</span><span class="o">==</span><span class="mi">0</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="k">else</span>
<span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<p>Here first if condition is checked ,if it comes out to be false it goes to elseif block and if that also comes to be false ,it finally goes to else block.</p>
<p>\% is an operator used to find modulus of a number .It find the remainder when two numbers are divided.</p>
<p>Any doubts related to conditional statement can be asked in comment section .In next tutorial we will discuss about looping .</p>Writing your First Javascript code2014-02-24T18:11:00+05:302014-02-24T18:11:00+05:30Deep_Singhtag:thegeekyway.com,2014-02-24:/writing-first-javascript-code/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Introduction-to-javascript.png" title="Writing your first javascript code"><img alt="Writing your first javascript code" src="//thegeekyway.com/uploads/images/2014/02/Introduction-to-javascript.png"></a> </p>
</div>
<p>In the <a href="http://www.thegeekyway.com/introduction-javascript/">previous article</a> we discussed about introduction to javascript, in this article we will discuss about how to write a javascript code, where we can add javascript in our web pages, how to use tools that are built in our web browser, how to validate HTML5/javascript code.</p>
<p style="font-size: x-large;"><u>Validation …</u></p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Introduction-to-javascript.png" title="Writing your first javascript code"><img alt="Writing your first javascript code" src="//thegeekyway.com/uploads/images/2014/02/Introduction-to-javascript.png"></a> </p>
</div>
<p>In the <a href="http://www.thegeekyway.com/introduction-javascript/">previous article</a> we discussed about introduction to javascript, in this article we will discuss about how to write a javascript code, where we can add javascript in our web pages, how to use tools that are built in our web browser, how to validate HTML5/javascript code.</p>
<p style="font-size: x-large;"><u>Validation of HTML5 document</u></p>
<p>In order to validate HTML5 code, we write the following code.</p>
<div class="highlight"><pre><span></span><code><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>TheGeekyWay.com<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">h1</span><span class="p">></span>Welcome to TheGeekyWay.com<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</code></pre></div>
<p>There are various validators online to check the validation, the reason we are doing this so as to correct them now as it might create a problem in future when we are writing the code. The famous validator is <a href="http://html5.validator.nu">http://html5.validator.nu</a>/ validator. Copy and paste the code there, and select the text field and click on validate. After validation you must get this green box. </p>
<div style="text-align:center">
<p><img alt="validated" src="//thegeekyway.com/uploads/images/2014/02/validated.png"> </p>
</div>
<p>If you are getting a red/pink box, please recheck the code.</p>
<p style="font-size: x-large;"><u>Find your Javascript editor </u></p>
<p>A good editor is a must, some features that might help you to determine what makes a good editor.</p>
<p><strong>Code is more writable</strong></p>
<ul>
<li>Auto-typing</li>
<li>Block balancing</li>
</ul>
<p><strong>Code is more readable</strong></p>
<ul>
<li>Syntax Highlighting</li>
<li>Code Folding</li>
<li>Function navigation</li>
</ul>
<p><strong>Some of the good editors are</strong></p>
<ul>
<li><a href="http://www.sublimetext.com">Sublime Text</a>(Windows, Linux, Mac)</li>
<li><a href="http://www.adobe.com/in/products/dreamweaver.html">Adobe Dreamweaver</a>(Mac, Windows)</li>
<li><a href="http://aptana.com/">Aptana Studio</a>(Mac, Windows, Linux)</li>
<li><a href="http://www.barebones.com/products/bbedit/">BBEdit</a>(Mac)</li>
<li><a href="http://download.cnet.com/TextWrangler/3000-2351_4-10220012.html">Text Wrangler</a>(Mac)</li>
</ul>
<p style="font-size: x-large;"><u>Browsers</u></p>
<p>In this section we are going to discuss browsers and web consoles, various tools that are used for intersection between browsers and web pages. For now you just should pick one, any browser you pick will probably have some tools built in.<br>
Here are some of the options :-</p>
<p><strong>Google Chrome</strong></p>
<ul>
<li>Console : Developer Tools</li>
<li>Developer Version : Chrome Canary (if you are a serious developer then you should use chrome canary, it has great development tools).</li>
</ul>
<p><strong>Firefox</strong></p>
<ul>
<li>It has a web inspector but generally speaking if you want to do any serious work you need to have a firebug addon.</li>
</ul>
<p><strong>Safari</strong></p>
<ul>
<li>Console : Web inspector</li>
<li>Developer version : Webkit Nightly</li>
</ul>
<p><strong>Internet Explorer</strong></p>
<ul>
<li>F12 Developer Tools(built-in)</li>
</ul>
<p style="text-decoration: underline;"/p><p style="font-size: x-large;">Writing your first javascript code</p></p>
<p>In order to write our first javascript code, we need to include a script tag in the head section of html coding (later in the articles Ill show why we should never use script tag in javasctipt inside html) but for now, since the code is small, so we can use this method.</p>
<p>Analyze the following code</p>
<div class="highlight"><pre><span></span><code><span class="nt"><html></span>
<span class="nt"><head></span><span class="w"> </span>
<span class="w"> </span><span class="nt"><title></span><span class="w"> </span>TheGeekyWay.com<span class="w"> </span><span class="nt"></title></span>
<span class="w"> </span><span class="nt"><script</span><span class="w"> </span><span class="na">type=</span><span class="s">"text/javascript"</span><span class="nt">></span>
<span class="w"> </span>function<span class="w"> </span>replace()<span class="w"> </span>{
<span class="w"> </span>var<span class="w"> </span>myValue<span class="w"> </span>=<span class="w"> </span>document.getElementById('textBox1').value;
<span class="w"> </span>var<span class="w"> </span>getTitle<span class="w"> </span>=<span class="w"> </span>document.getElementById('title');
<span class="w"> </span>if<span class="w"> </span>(myValue.length<span class="w"> </span>==<span class="w"> </span>0)<span class="w"> </span>{
<span class="w"> </span>getTitle.innerHTML<span class="w"> </span>=<span class="w"> </span>"Nothing<span class="w"> </span>entered,<span class="w"> </span>enter<span class="w"> </span>a<span class="w"> </span>valid<span class="w"> </span>character";
<span class="w"> </span>return;
<span class="w"> </span>}
<span class="w"> </span>else<span class="w"> </span>{
<span class="w"> </span>getTitle.innerHTML<span class="w"> </span>=<span class="w"> </span>myValue;
<span class="w"> </span>}<span class="w"> </span>
<span class="w"> </span>}
<span class="w"> </span><span class="nt"></script></span>
<span class="w"> </span><span class="nt"></head></span>
<span class="nt"><body></span>
<span class="w"> </span><span class="nt"><form</span><span class="w"> </span><span class="na">id=</span><span class="s">"form1"</span><span class="w"> </span><span class="na">runat=</span><span class="s">"server"</span><span class="nt">></span>
<span class="w"> </span><span class="nt"><div></span>
<span class="w"> </span><span class="nt"><h1</span><span class="w"> </span><span class="na">id=</span><span class="s">"title"</span><span class="nt">></span>Welcome<span class="w"> </span>to<span class="w"> </span>TheGeekyWay.com<span class="nt"></h1></span>
<span class="w"> </span><span class="nt"><input</span><span class="w"> </span><span class="na">type=</span><span class="s">"text"</span><span class="w"> </span><span class="na">id=</span><span class="s">"textBox1"</span><span class="w"> </span><span class="nt">/></span>
<span class="w"> </span><span class="nt"><input</span><span class="w"> </span><span class="na">type=</span><span class="s">"button"</span><span class="w"> </span><span class="na">value=</span><span class="s">"Submit"</span><span class="w"> </span><span class="na">id=</span><span class="s">"myButton"</span><span class="w"> </span><span class="na">onclick=</span><span class="s">"replace()"</span><span class="w"> </span><span class="nt">/></span>
<span class="w"> </span><span class="nt"></div></span>
<span class="w"> </span><span class="nt"></form></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</code></pre></div>
<p>[codepen_embed height=220 theme_id=3 slug_hash='wLDGe' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<p>Remember that all your javascript code will be written under script tag with the type "text/javascript". Now lets analyze line by line, in the first line we are creating a variable myValue which gets the value from textbox via getElementById() method which takes the value from the text box id, similarly we take the id from title. In the if condition, if the length of the code entered is 0, then it will display "Nothing entered, enter a valid character", else it will take the value from textbox and put the value in title. (Remember that this is just the starting of the javascript series tutorial, its highly recommended not to put your javascript code under the head tag, the exact reason will be discussed in the following tutorials.</p>
<p>Hope this tutorial finds you interesting, hit the following social buttons below and spread this article.</p>Introduction to Javascript2014-02-23T18:55:00+05:302014-02-23T18:55:00+05:30Deep_Singhtag:thegeekyway.com,2014-02-23:/introduction-javascript/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/IntroductionToJavascript.png" title="Introduction to Javascript"><img alt="Introduction to Javascript" src="//thegeekyway.com/uploads/images/2014/02/IntroductionToJavascript.png"></a></p>
</div>
<p>Hi everyone, welcome to this brief article discussing about javascript, we'll do this by discussing various fundamentals of javascript, javascript is a programming language that as a software developer you can't ignore. Javascript was first demonstrated and announced at conference at SanFrancisco in 1995, over the time web browsers emerged …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/IntroductionToJavascript.png" title="Introduction to Javascript"><img alt="Introduction to Javascript" src="//thegeekyway.com/uploads/images/2014/02/IntroductionToJavascript.png"></a></p>
</div>
<p>Hi everyone, welcome to this brief article discussing about javascript, we'll do this by discussing various fundamentals of javascript, javascript is a programming language that as a software developer you can't ignore. Javascript was first demonstrated and announced at conference at SanFrancisco in 1995, over the time web browsers emerged and javascript became one of the premium programming language of the future.</p>
<p>Javascript is not only used for making web pages but also for making applications, infact in September 2011 Microsoft announced it that it would be making javascript as a first class language for making native windows applications alongside C++ and C#.</p>
<p>Javascript is moved mainly from inside a browser only to also being used on web server for application logic, file logic, accessing database server and so on.</p>
<p>When your web browser downloads a web page it starts to parse all the html of webpage it starts creating a series of object of the webpage, that series of objects contains like its content and attributes and so on. This is known as <strong>Document Object Model</strong> or the <strong>DOM</strong> Elements. So you can use javascript to access the DOM in order to change the response on the web page. For example : A user might hover its mouse over an element, or clicking an element.</p>
<p>There is no "RIGHT WAY" to include a javascript file in html, but there are some practices that are being followed to include javascript. There is at least 3 different ways to include javascript.</p>
<p>Various uses of Javascript are</p>
<ul>
<li>Javascript is used for show or hide text or image, based on some interaction on webpage.</li>
<li>Javascript is being used to apply styling on webpage.</li>
<li>Javascript is used for client side form field validation, as a first line of defense before sending data to web server.</li>
<li>Javascript has been used to animate element on screen (such as color/ opacity transition, moving graphics around the page, sliding elements around the page).</li>
<li>Entire games can be loaded via javascripts.</li>
<li>It can be used to communicate to web server without the need for http post or get.</li>
<li>And many more</li>
</ul>
<p>HTML 5 and CSS 3 has reduced the need for javascript in some of these scenarios, some form validations, some animations, these are now handled by HTML 5 and CSS 3.</p>
<p>What you should know before we move on to our next article</p>
<ul>
<li>First of all since you have interest in javascript then you are an active web designer, then you should be familiar with HTML and CSS and good understanding on how to work.</li>
<li>Recognize JavaScript in documents if you saw it, you dont have to use it before you should know how a javascript looks like.</li>
</ul>
<p style="font-size: x-large;">What is Javascript ?</p>
<p>The most common question before anyone starts working in Javascript is, "What is <strong>JavaSript</strong> ? " Remember that <strong>Javascript is NOT Java</strong>. They are totally different, let not the name confuse you.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/java_logo.png" title="Javascript is Not Java"><img alt="javascript" src="//thegeekyway.com/uploads/images/2014/02/java_logo.png"></a></p>
</div>
<ul>
<li>Javscript is the programming language of the web.</li>
<li>It creates interactively, it helps you to click, drag, tap, swipe, tap, etc.</li>
<li>It is most commonly used for front end development, i.e any kind of programming that happens in a web browser.</li>
<li>It is also used in back end development as well like Node.JS.</li>
<li>So javascript is used for any programming on the web.</li>
</ul>
<p style="font-size: x-large;">Where should we not use javascript ?</p>
<ul>
<li>We should use CSS3 transitions instead of Javascript, CSS3 transitions are more powerful, especially on mobile platform where they are more performant.</li>
<li>CSS can handle hover and focus events (like rollover menus. tooltips, etc) we dont need javascript at all.</li>
<li>HTML5 form controls can be used as custom widgets (like range controls, sliders, etc), this will help on mobile platforms.</li>
</ul>
<p>Hope this article might give you some information about javascript or make some interest in javascript. In our next article we will discuss about writing our first Javascript application.</p>Creating your first Java Program (J4A - 2)2014-02-22T23:58:00+05:302014-02-22T23:58:00+05:30gagan93tag:thegeekyway.com,2014-02-22:/creating-first-java-program/<p><u>Beginning Java Programming</u></p>
<p>Some institutes/tutorials start java directly using Netbeans or Eclipse IDE. But this is not the correct way. A better method is to use a basic text editor like Notepad2 or Notepad++ to begin Java. IDE’s provide shortcuts which experienced programmers use. If beginners opt for …</p><p><u>Beginning Java Programming</u></p>
<p>Some institutes/tutorials start java directly using Netbeans or Eclipse IDE. But this is not the correct way. A better method is to use a basic text editor like Notepad2 or Notepad++ to begin Java. IDE’s provide shortcuts which experienced programmers use. If beginners opt for these methods, they will leave half of the work for IDE and miss this ‘half’.</p>
<p><strong>IDE</strong> is an Integrated Development Environment or a software which accelerates software development and provides features like syntax completion, error detection,error correction, auto indentation, drag and drop for G.U.I. and much more. One should go for an IDE only after practising a programming language on text editors.</p>
<p>The best text editor for starting java is TextPad (<a href="http://www.textpad.com/download/index.html" title="link">download from here</a>). The best part of TextPad is that it provides Program compilation and execution shortcuts just like Turbo C++. ‘Ctrl + 1’ can be used to compile a Java program and ‘Ctrl + 2’ can be used to execute a compiled program.</p>
<p><u>First Simple Java Program</u></p>
<p><strong><u> </u></strong></p>
<p><strong>class Demo</strong></p>
<p><strong>{</strong></p>
<p><strong> public static void main(String[] args)</strong></p>
<p><strong> {</strong></p>
<p><strong> System.out.println(“My first java program”);</strong></p>
<p><strong> }</strong></p>
<p><strong>}</strong></p>
<p><u>Noteworthy points about this small program</u> :</p>
<ol>
<li>In Java, even the main method lies inside a class, and Java file is saved with the name of the class containing main method so, the above code should be saved as “Demo.java”.</li>
<li>In Java, ‘functions’ are known as ‘methods’ and variables are called as ‘attributes’</li>
<li>Indentation is used to increase readability. Programmers <strong>must</strong> use indentation to make their code look clean.</li>
<li>Main method is both public and static. Public methods are accessible outside the class also (public is similar to what it was in C++). As main method is accessed by Java Virtual Machine during execution, so it <strong>must</strong> be public. Static keyword is described later. For now, just keep in mind that ‘static’ method or variable has only copy for entire class irrespective of no. of objects.</li>
<li>The main method takes a string array as an argument. This argument can be passed when executing the program.(Consider String to be a data type till further discussion)</li>
<li>System.out.println() is similar to printf or cout\<\<. Here System is a class, out is a stream and println() is a method. ‘out’ is a <u>static stream </u>so it is accessed by name of class (System) rather than object of system class.</li>
</ol>
<p>Write the above program (don’t copy) in a simple text editor (or Textpad). Save the file as “Demo.java” at some specific path. If you are using Textpad, use shortcuts described above to compile and execute the code.</p>
<p>If you are using a simple text editor, follow these steps</p>
<ol>
<li>Open command prompt (goto start -> run -> cmd - > Enter).</li>
<li>Navigate to the directory containing the file which you have just saved.</li>
<li>To compile java program, type “javac Demo.java”. Here ‘javac’ is the java compiler application and Demo.java is the name of the java program which we are compiling. After this step, Demo.class file is created in the directory.</li>
<li>To execute java program, type “java Demo”. Here java is the name of application which will execute the .class file and show the output as shown here.</li>
</ol>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/61.jpg"><img alt="6" src="//thegeekyway.com/uploads/images/2014/02/61.jpg"></a></p>
</div>Moving from C++ to Java (J4A - 1)2014-02-22T23:51:00+05:302014-02-22T23:51:00+05:30gagan93tag:thegeekyway.com,2014-02-22:/moving-c-java/<p><u>C to C++ and C++ to Java:</u></p>
<p>C was a powerful programming language but used structural approach. So when the length of code exceeded a particular no. of lines, the code became unmanageable. So this led to Object Oriented Programming techniques and birth of C++. But C++ compilers at that …</p><p><u>C to C++ and C++ to Java:</u></p>
<p>C was a powerful programming language but used structural approach. So when the length of code exceeded a particular no. of lines, the code became unmanageable. So this led to Object Oriented Programming techniques and birth of C++. But C++ compilers at that time were not platform independent. You needed a different code for Windows, different for Mac, Unix and other operating systems. Today programs written in C++ are also categorized by 32 and 64 bit versions. So C++ followed the W.O.C.A. principle, i.e. <u>Write Once, Compile Anywhere</u>. The main focus was to create programs which could execute on any platform and any O.S. These forces led to the development of Java (originally called as ‘Oak’) which followed W.O.R.A. principle, i.e. <u>Write Once, Run Anywhere.</u></p>
<p><u> </u></p>
<p><u>Where the Magic lies?</u></p>
<p>So what magic is there in Java which was not in older languages? Basically all the older compilers compile the source code (program) to native executable file (like .exe in windows). Here ‘native executable file’ means a file which is created for some target OS and is ready to execute on that OS. So we need to create a different ‘native’ file for different OS and different processor architectures (32 and 64 bit).</p>
<p>But in Java, the code is not directly compiled to native executable. The output of java compiler is a .class file, irrespective of OS / processor architecture. This .class file is a set of optimized instruction for a virtual machine called JVM (Java Virtual Machine). The creators of Java implemented this virtual machine (JVM) for different operating systems. So now the programmers need not worry about the target operating system or processor. They just have to write the code and install JVM on the target system, JVM handles the rest.</p>
<p><u>Understanding Simple Java Terms:</u></p>
<p><u>JVM</u> is simply a virtual machine included in JDK as well as JRE.</p>
<p><u>JDK</u> is Java Development Kit for Java Developers. It includes Java Runtime Environment(JRE) and all other related tools needed by developers.</p>
<p><u>JRE</u> is Java Runtime Environment. It <strong>excludes</strong> the development tools and encapsulates only runtime environment. When you create a java application for a client, just install JRE on client side and your application will execute.</p>
<p>Remember, <u>Java code is platform independent but Java Runtime Environment is specific for each platform.</u></p>
<p>+-----------------------------------------------------------------------+
| <div> |
| |
| <u>The Android Counterpart</u>: |
| Android Devices have DVM (Dalvik Virtual Machine) rather than JVM |
| (Java Virtual Machine). You can think of Dalvik as mobile or android |
| specific implementation of JVM. You need not know about Dalvik for |
| now, for those who are interested can visit |
| <a href="http://stackoverflow.com/questions/3536786/dalvikvm-vs-javavm- |
| in-android">here</a>. |
| |
| </div> |
+-----------------------------------------------------------------------+</p>
<p><u>Downloading & Installing JDK</u></p>
<p>Download JDK from <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html">here</a>.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/112.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/02/112.jpg"></a></p>
</div>
<p>Scroll down to see screen as shown above. Accept the License Agreement to start download. As you can see, JDK is implemented for almost all major operating systems. After downloading, install JDK with default settings. After installation, go to</p>
<p>C:/program files/java/jdk\<version_name>/bin/</p>
<p>Make sure ‘javac’ and ‘java’ are present in this folder. Now copy the complete path from the address bar of explorer as shown in the pic below.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/25.jpg"><img alt="2" src="//thegeekyway.com/uploads/images/2014/02/25.jpg"></a></p>
</div>
<p>Now, follow these steps</p>
<p>Go to Start Menu -> Run. Type ‘sysdm.cpl’ and press OK. You get the following screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/32.jpg"><img alt="3" src="//thegeekyway.com/uploads/images/2014/02/32.jpg"></a> Click on ‘Advanced’ tab and then on Environment Variables button.</p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/41.jpg"><img alt="4" src="//thegeekyway.com/uploads/images/2014/02/41.jpg"></a> You will get following screen now</p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/51.jpg"><img alt="5" src="//thegeekyway.com/uploads/images/2014/02/51.jpg"></a> </p>
</div>
<p>Create a new user variable using button as shown above. Write the following values in text field provided.</p>
<p>Variable name : path</p>
<p>Variable value : \<text you had copied>.</p>
<p>Press OK to save and close all previous dialog boxes.</p>
<p>Now your Java Environment Variable is set and if you have followed the steps correctly, you will be able to compile and run java applications. To check if you have followed the above procedure correctly, goto start menu -> Run and type</p>
<p>java and press enter, if you get a flashing black screen, type</p>
<p>javac and press enter, if you still get a flashing black screen, it means the variables are correctly setup. If you get some error when you type java or javac, you might have done some mistake in setting up variable. It’s better to restart your computer and try again.</p>
<p>The next tutorial describes creating your first simple Java program.</p>
<p><a href="http://www.thegeekyway.com/creating-first-java-program" title="link">Link to next tutorial</a></p>Introduction and Syllabus (J4A - 0)2014-02-22T22:54:00+05:302014-02-22T22:54:00+05:30gagan93tag:thegeekyway.com,2014-02-22:/java-for-android-introducion/<p>Hello geeks, <strong>(J4A) Java for Android</strong> is a series of tutorials for those who have good knowledge of C++ and want to switch to android without going into details of Core Java. So here we'll discuss ONLY THOSE features of JAVA (keywords, classes, concepts) which are used in android also …</p><p>Hello geeks, <strong>(J4A) Java for Android</strong> is a series of tutorials for those who have good knowledge of C++ and want to switch to android without going into details of Core Java. So here we'll discuss ONLY THOSE features of JAVA (keywords, classes, concepts) which are used in android also. Some may think that these tutorials serve nothing and its better to learn these features in parallel with android, but if you know these concepts in advance, it will ultimately accelerate your efforts towards android application development and you will not need to LOOK BACK while developing for android. ECE students interested in android can also go for these tutorials. Please note that these tutorials don't make you Java Programmer !</p>
<p>Topics from JAVA included in this tutorial series are:</p>
<ol>
<li>Moving from C++ to JAVA & Installing and JDK.</li>
<li>A simple java program to explain various simple concepts.</li>
<li>Revising data types, if, for, switch, Typecasting, operators, arrays in Java.</li>
<li>Concept of class, method, references, access specifiers, keywords (static, final, extends, implements).</li>
<li>Packages and Interfaces (Overview only).</li>
<li>Exception Handling and Multithreading (Both Detailed).</li>
<li>String class, its functions and java.lang.CharSequence interface.</li>
<li>Overview of important Java API used in Android (java.util, java.io, java.net).</li>
<li>Event Based Programming and Anonymous Inner Classes.</li>
<li>Introduction to Java Beans Architecture to understand various classes/components.</li>
</ol>
<p>Please note that above list of topics are tentative and may be added / removed.</p>
<p><a href="http://www.thegeekyway.com/moving-c-java/" title="link">Link to First Tutorial</a></p>Second simple Android Application Part 3 (Android-Part 6)2014-02-14T23:20:00+05:302014-02-14T23:20:00+05:30gagan93tag:thegeekyway.com,2014-02-14:/secondapp3/<p>Welcome back geeks, so after pasting <a href="http://pastebin.com/zs00wWNd">this code</a>, you would be having the MainActivity.java file like this :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/131.jpg"><img alt="13" src="//thegeekyway.com/uploads/images/2014/02/131.jpg"></a></p>
</div>
<p>In this screenshot, notice the highlighted part on the left. The Eclipse IDE is showing some errors at some specific lines (line no. 9 10,17,18 and so on). Now we …</p><p>Welcome back geeks, so after pasting <a href="http://pastebin.com/zs00wWNd">this code</a>, you would be having the MainActivity.java file like this :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/131.jpg"><img alt="13" src="//thegeekyway.com/uploads/images/2014/02/131.jpg"></a></p>
</div>
<p>In this screenshot, notice the highlighted part on the left. The Eclipse IDE is showing some errors at some specific lines (line no. 9 10,17,18 and so on). Now we’ll use <u>Quick Fix</u> feature of Eclipse to include necessary classes.</p>
<ol>
<li>Click on left corner of line no. 9 and Eclipse with provide <strong>fixes</strong> for this error. Click on the first solution, i.e. import <strong>android.widget.Button.</strong></li>
<li>Similarly click on left corner of line no. 10 to import <strong>android.widget.EditText</strong>. So now two of required classes have been imported, you will notice that in import statements.</li>
<li>Click on left corner of line no. 21 to import View.OnClickListener (third solution to import <strong>android.view.View.OnClickListene</strong>r)</li>
<li>Click on left corner of line no. 27 to import <strong>android.widget.Toast</strong>.</li>
</ol>
<p>So now our necessary packages and classes have been imported and we can start understand the actual code.</p>
<p>First we create a button reference and two edit text references globally within the class. This part required no explanation. Inside the onCreate method we map all of them to the layout file which we had created. We use the method <strong>findViewById</strong> to find the components by their ID as defined in the XML file. So we map the Button and two EditTexts with their respective IDs.</p>
<p>Next we add an <strong>onClickListener</strong> to the button. This is similar to the <strong>ActionListener</strong> from Swing API. It defines the action to be performed when the button is clicked. We have used the <strong>anonymous inner class version</strong> of this method. Alternatively, we may even use <strong>implement</strong> keyword to implement <strong>OnClickListener</strong> interface and define the required method ( <strong>public void onClick()</strong> ).</p>
<p>The onClick() method takes a <strong>View</strong> as an argument. As discussed previously also, View is directly or indirectly superclass of all android components. So we can use the View parameter to obtain view Id, and other properties of the View which generates the event.</p>
<p>Inside the method we call makeText method of the Toast class. This method is used to create a simple dialog like view which displays the input. This is similar to JOptionPane class from Swing API where we have few ready-made dialogs. The arguments inside Toast method specify the <strong>application context</strong>, the <strong>string</strong> to be displayed and the <strong>time</strong> for which the dialog is displayed (Toast.LENGTH_SHORT). In the end, show method is called from the Toast context to show this dialog which we have just created.</p>
<p>So this is how this simple application works. This was an effort to introduce the onClickListener to our audience. Similarly different components have different type of listeners like onCheckedChangedListener for toggle based events.</p>
<p>Execute the application on Android Virtual Device, Bluestacks or directly on your phone. This application when executed on AVD 4.3 gives the following output :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/141.jpg"><img alt="14" src="//thegeekyway.com/uploads/images/2014/02/141.jpg"></a></p>
</div>Second simple Android Application Part 2 (Android-Part 5)2014-02-14T22:32:00+05:302014-02-14T22:32:00+05:30gagan93tag:thegeekyway.com,2014-02-14:/secondapp2/<p>Welcome back geeks, this tutorial is a continuation of <a href="http://www.thegeekyway.com/secondapp1/" title="previous">previous</a><a href="http://www.thegeekyway.com/secondapp1/" title="this"> </a>tutorial. So please read the previous one if you have not read it.</p>
<p>So we had created a simple layout and it looked something like this : </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/7.jpg"><img alt="7" src="//thegeekyway.com/uploads/images/2014/02/7.jpg"></a></p>
</div>
<p>Double click on the button to change the text of the button, now the …</p><p>Welcome back geeks, this tutorial is a continuation of <a href="http://www.thegeekyway.com/secondapp1/" title="previous">previous</a><a href="http://www.thegeekyway.com/secondapp1/" title="this"> </a>tutorial. So please read the previous one if you have not read it.</p>
<p>So we had created a simple layout and it looked something like this : </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/7.jpg"><img alt="7" src="//thegeekyway.com/uploads/images/2014/02/7.jpg"></a></p>
</div>
<p>Double click on the button to change the text of the button, now the window will switch to XML version of the layout. In the XML file, the part of button definition is highlighted.</p>
<p>Move to the top of XML file and we’ll understand each thing written in the file. The first two lines are just references to website.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/8.jpg"><img alt="8" src="//thegeekyway.com/uploads/images/2014/02/8.jpg"></a><strong>1. </strong> The default layout being used by eclipse here is <strong>Relative Layout.</strong> As obvious from the name, in Relative Layout each component is aligned with respect to its previous component(s) which was added to layout.</p>
</div>
<p><strong>2. </strong> android:layout_width=”match_parent”. This code sets the width of Relative Layout to cover the complete width of layout. Similarly the next line sets the width to complete width of the layout.</p>
<p><strong>3. </strong> The next four lines of code just provide some padding to all the sides using constant value defined in dimension ( \@dimen ).</p>
<p>The next code fragment adds an EditText (or textfield) to the layout whose attributes are :</p>
<p>a) android:id=”\@id/editText1”: This id is used to refer this textfield in java program, as well as to align other components using this EditText.</p>
<p>b) android:layout_width=”wrap_content” : The width of component just wraps the content, or simply just sufficient to store a simple line of text. Similar is for height of EditText.</p>
<p>c) android:layout_alignParentLeft="true" : The component is aligned from left of its parent. This value is set to true to apply these changes. Similar is for next line. So by these two lines, the EditText is aligned to <u>Top-Left</u> of Parent (or the layout/screen).</p>
<p>d) android:layout_marginLeft="39dp" : A slight margin is given towards left side. <strong>dp</strong> is <strong>density pixels</strong>. It is better to specify spacing using <u>dp</u> rather than <u>px</u> (pixels) because px is not same for different android phones, whereas dp is same for different android phones. Similar is done by next line to set top margin.</p>
<p>e) android:ems=”10” : Simply means that the EditText (text field) can contain 10 characters. To Experiment with ems, try changing its value and see corresponding change in the Graphical Layout.</p>
<p>We can specify many more attributes for a text field. Use <strong>Ctrl+Space</strong> in Eclipse to view more related properties. For eg. write this in eclipse and press <strong>Ctrl+Space</strong> to view more attributes.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/9.jpg"><img alt="9" src="//thegeekyway.com/uploads/images/2014/02/9.jpg"></a></p>
</div>
<p>When you write <u>android:</u> on a blank line and press Ctrl+Space to view all these attributes. Similarly we can also use this shortcut to view suggestions in Java program also.</p>
<p>In the second EditText, everything is same except <u>inputType </u>attribute which is set to “<u>textPassword</u>”. This sets the input mode to password, so the text input by user is set to bullets (to hide text).</p>
<p>To experiment, you can use Ctrl+Space shortcut to see more available <u>inputTypes</u> (shown below).</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/10.jpg"><img alt="10" src="//thegeekyway.com/uploads/images/2014/02/10.jpg"></a></p>
</div>
<p>For button, all the attributes are self-explanatory and similar to those explained previously except the <u>android:text </u>attribute. This attribute sets the Text which is displayed on the button and is visible to the user. If you notice carefully, you’ll see an exclamatory mark on the left corner of line where text for button is specified.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/111.jpg"><img alt="11" src="//thegeekyway.com/uploads/images/2014/02/111.jpg"></a></p>
</div>
<p>When you bring mouse pointer close to the mark, you see the notice the warning “Hardcoded string ”Button”, should use \@string resource”. For now just ignore this warning, it would be discussed later where you will specify the String using strings.xml file.Now our XML work is complete and we’ll move to the <strong>MainActivity.java</strong> file to start the coding part.</p>
<p>Out Java file contains this much code for now : </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/121.jpg"><img alt="12" src="//thegeekyway.com/uploads/images/2014/02/121.jpg"></a></p>
</div>
<p>People familiar with java will understand the first two statements (package and import). Next is our class which extends the Activity class. In android, each new screen is an <strong>Activity,</strong> so it extends the Activity class which is present in <strong>android.app</strong> package. Click on ‘+’ sign on the left import to see other imported packages.</p>
<p>The first method inside our class is onCreate method. This is a part of Android life cycle. Java programmers must be knowing <strong>Applet Life Cycle methods.</strong> Similarly, an android application also has a <a href="http://i.stack.imgur.com/rVnSi.png">life cycle</a> (which would be discussed in further tutorials). For now, just consider that onCreate is just an initialization method for our activity. In the first line of code, it simply calls the super class method. The second line sets the layout of our activity to <u>R.layout.activity_main</u>. This is nothing but a reference to the XML file just created by us and stored in layout folder. It is accessed by its defined in R.java file (auto-generated java file). The second method creates a menu which is rather not required here, so either let it be there or remove it. We’ll discuss about that method later. Paste <a href="http://pastebin.com/zs00wWNd">this code</a>to your application and in the next tutorial we’ll understand the java code and finish this second simple android application.</p>
<p><a href="http://www.thegeekyway.com/secondapp3" title="link"> <p style="font-size: xx-large;">Link to next tutorial</p></a></p>Second simple Android Application Part 1 (Android-Part 4)2014-02-14T21:26:00+05:302014-02-14T21:26:00+05:30gagan93tag:thegeekyway.com,2014-02-14:/secondapp1/<p>Hey Geeks ! Hope you all have gone through previous tutorials and are clear with the basics of android. Here we’ll create our second simple android application which will consist of creating our own layout and a bit of java coding. As discussed previously, the <strong>layout of our android application …</strong></p><p>Hey Geeks ! Hope you all have gone through previous tutorials and are clear with the basics of android. Here we’ll create our second simple android application which will consist of creating our own layout and a bit of java coding. As discussed previously, the <strong>layout of our android application is written in XML file</strong>, so we’ll create the layout with a button and two text fields. When the button is pressed, the contents of text fields are shown. For demonstration, the two text fields input the ‘user Id’ and ‘password’ from the user and they are displayed to the user when the button is pressed..</p>
<p>So open Eclipse and create a new Android Project from File-> New -> Android project (or use keyboard shortcut Alt+Shift+N).</p>
<p>Fill in the name of application as “UserId” or something similar. Fill in package name and project name. Let everything else remain <strong>default</strong> and finish to get a similar screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/19.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/02/19.jpg"></a></p>
</div>
<p>Eclipse opens two files by default for us (highlighted in above pic):</p>
<ol>
<li>MainActivity.java: Here, java code for activity is written. <strong>Activity</strong> is something similar to java frame. Or in simpler words, for now you can consider each new screen as a new Activity.</li>
<li>Activity_main.xml: XML file contains the components added by us and their positioning or simply the layout of the activity. Usually, each <strong>new activity</strong> will have its own layout defined in its respective XML file.</li>
</ol>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/24.jpg"><img alt="2" src="//thegeekyway.com/uploads/images/2014/02/24.jpg"></a></p>
</div>
<p>Just to refresh, MainActivity.java file is stored in <strong>src</strong> folder and layout files like activit_main.xml are stored in <strong>res/layout</strong> folder. Click on the activity_main.xml and the following screen is opened by Eclipse.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/31.jpg"><img alt="3" src="//thegeekyway.com/uploads/images/2014/02/31.jpg"></a></p>
</div>
<p>One important part is highlighted in this screenshot, <u>Graphical Layout</u> tab and <u>activity_main</u> tab. <strong>Graphical Layout</strong> tab is used to view the graphical state of layout or to drag and drop components onto your activity. To view what is happening on the coding side, click on <strong>activity_main.xml</strong> tab.</p>
<p>For most of the part, you can use drag and drop to add components to your activity, but being a programmer you must know what is actually happening in the XML file. Also, when you are working with layouts and for better positioning of components, you will need to have a good understanding of what is happening on the XML side, so that you can customize the components from the code also. So we’ll use drag and drop for adding components and at the same time we’ll also understand what changes are taking place in XML file.</p>
<p>By clicking all the component types (Form widgets, textfields, layouts etc.), you can have an overview of what all components you have (you must close the upper folder to view lower folder due to limited space in eclipse window).</p>
<p>Now we’ll start creating layout for our application. Click on “Hello World” on the screen so that it is selected as shown below.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/4.jpg"><img alt="4" src="//thegeekyway.com/uploads/images/2014/02/4.jpg"></a></p>
</div>
<p>Now right click and Delete this TextView (or use Delete key from keyboard). Now what’s a TextView? In Android, all components are subclasses of a class called <strong>View.</strong> Or put in simpler words, in Android every component is a View. Each component whether a <u>button</u>,<u> text field</u> etc. are directly or indirectly subclasses of View.</p>
<p>Coming back to our TextView, it is a passive component like a JLabel in Java. It simply displays some text and doesn’t interact with the user other than this. After removing the ‘Hello World’ TextView, we are left with only the title of the application. So now we’ll start dragging components from the left panel to the layout.</p>
<p>Expand the <u>Text Fields</u> section and use your mouse to drag a <u>Plain Text</u> and a <u>Password</u> field on to your layout.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/5.jpg"><img alt="5" src="//thegeekyway.com/uploads/images/2014/02/5.jpg"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/6.jpg"><img alt="6" src="//thegeekyway.com/uploads/images/2014/02/6.jpg"></a></p>
</div>
<p>Next, use the <u>Form Widgets</u> to insert a button under these two fields, preferably in the middle of the screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/7.jpg"><img alt="7" src="//thegeekyway.com/uploads/images/2014/02/7.jpg"></a></p>
</div>
<p>So now our layout is ready with two textfields and a button. We don't want to make this tutorial lengthy for you guys, so we'll just stop here and do check next tutorial to proceed further.</p>
<p><a href="http://www.thegeekyway.com/secondapp2/" title="link"> <p style="font-size: xx-large">Link to next tutorial</p></a></p>KVM vs QEMU vs Libvirt2014-02-14T16:22:00+05:302014-02-14T16:22:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-02-14:/kvm-vs-qemu-vs-libvirt/<p>Since the day I started to read about the virtualization concept, I have been troubled by QEMU and KVM. They both look like complementary, but what exactly they do? And then there is libvirt, what role does it play? Let’s try to understand that.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/download.jpg"><img alt="KVM" src="//thegeekyway.com/uploads/images/2014/02/download-300x94.jpg"></a></p>
</div>
<p>When you dive deep into …</p><p>Since the day I started to read about the virtualization concept, I have been troubled by QEMU and KVM. They both look like complementary, but what exactly they do? And then there is libvirt, what role does it play? Let’s try to understand that.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/download.jpg"><img alt="KVM" src="//thegeekyway.com/uploads/images/2014/02/download-300x94.jpg"></a></p>
</div>
<p>When you dive deep into the virtualization, you find hypervisor. Hypervisor is the agent that helps you create virtual machines. He is the guy who creates and runs the guest machine and provide the host’s resource to the guest. QEMU and KVM both are able to act as hypervisor. Then why are they used together? Well, because QEMU is dumb! Well, not exactly, but it is a little slower when it comes to system which dont have hardware virtualization. So, to achieve that, we have our own very dear KVM. KVM helps QEMU to access hardware virtualization features on different architectures. It also adds the acceleration feature to the QEMU process. So, in short, when they are together, QEMU is the hypervisor/emulator and KVM is the accelerating agent.</p>
<p>Then we come to libvirt. Libvirt is quite innocent in the sense that is doesn’t confuse you! It is simply a virtualization management library. And what does it do? It manages both KVM and QEMU. It consists of three utilities namely - an API library, a daemon (libvirtd) and a command line tool -virsh. Libvirt is quite effective and it can manage a lot of hypervisors altogether.</p>
<p>So, whenever you are using these all together, remember there is a hypervisor, an accelerating agent and a management library.</p>Java: Autoboxing and -XX:AutoBoxCacheMax2014-02-13T22:52:00+05:302014-02-13T22:52:00+05:30Anooptag:thegeekyway.com,2014-02-13:/java-autoboxing-xxautoboxcachemax/<p>Autoboxing is the mechanism in Java to convert primitive type values to respective wrapper class object. Java programmer mostly use autoboxing while using collections, since collection only holds object and to put primitive type data into collection, it's need to use wrapper classes to convert into objects.</p>
<p>So , here is …</p><p>Autoboxing is the mechanism in Java to convert primitive type values to respective wrapper class object. Java programmer mostly use autoboxing while using collections, since collection only holds object and to put primitive type data into collection, it's need to use wrapper classes to convert into objects.</p>
<p>So , here is autoboxing to assign primitive type to wrapper type directly.</p>
<ul>
<li><code>boolean</code> to type <code>Boolean</code></li>
<li><code>byte</code> to type <code>Byte</code></li>
<li><code>short</code> to type <code>Short</code></li>
<li><code>char</code> to type <code>Character</code></li>
<li><code>int</code> to type <code>Integer</code></li>
<li><code>long</code> to type <code>Long</code></li>
<li><code>float</code> to type <code>Float</code></li>
<li><code>double</code> to type <code>Double</code></li>
</ul>
<p>You can directly compare wrapper classes using <p style="background-color: #ffff99;"> <em>==</em> , <p style="background-color: #ffffff;"> just like primitive types but </p></p>for certain range which is by default (-128 to 127) .Which is default value of <p style="background-color: #ffff00;">-XX:AutoBoxCacheMax .</p></p>
<p>Here is the demo program when your object which is a autoboxed primitive type can be compared directly.</p>
<div class="highlight"><pre><span></span><code> int int1 = 100;
int int2 = 100;
System.out.println(int1 == int2); // true as they are primitive
Integer integerObj1 = new Integer(100);
Integer integerObj2 = new Integer(100);
System.out.println(integerObj1 == integerObj2); // always false as these are objects
Integer integer1 = 100;
Integer integer2 = 100;
System.out.println(integer1 == integer2); // true
Character c1 = 110;
Character c2 = 110;
System.out.println(c1 == c2); //true
Short s = 56;
Short s1 = 56;
System.out.println(s == s1); //true
</code></pre></div>
<p><strong> Why caching this range :</strong></p>
<p>This short range are generally used and performance of <strong> <p style="font-family: courier new,courier; background-color: #ffff00;"><em>public static valueOf( i)</em></p></strong> as this method is likely to yield significantly better space and time performance by caching frequently requested values.</p>
<p><strong> Changing -XX:AutoBoxCacheMax :</strong></p>
<p>You can set the -XX:AutoBoxCacheMax=1000, and it will catch integer upto 1000 which means following code will results true</p>
<div class="highlight"><pre><span></span><code>Integer integer1 = 1000;
Integer integer2 = 1000;
System.out.println(integer1 == integer2); // true
</code></pre></div>
<p>Note: this changing size only works for positive integer and it can't be set less than 127. See inner class <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Integer.java?av=f#596"> <p style="font-family: courier new,courier;">private static class IntegerCache</p></a> in Integer.java</p>
<p><a href="http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Flaunchers%2Farguments.htm">Setting Runtime VM option in Eclipse</a>,</p>
<p><strong>Max size of -XX:AutoBoxCacheMax ;</strong></p>
<p>Max cache size can't be more than -Xmx (which is JVM heap size) . Heap size is defined by vm argument -Xmxm .<br>
But, you as soon as JVM initialized, it allocate the memory for caching purpose. But you can't allocate whole (-Xmx in byte)/4 (4byte is size of int) for AutoBoxCache because of other object needed to be loaded and you might end up with <p style="font-family: courier new,courier;">java.lang.OutOfMemoryError: Java heap space</p>.</p>
<p>Also, note that for other wrapper classes except Integer , have fixed caching size upto 127 only.</p>XHTML and CSS - 1 - Overview and Downloading Text Editor2014-02-09T13:55:00+05:302014-02-09T13:55:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-09:/video-tut-xhtml-and-css-1-overview-downloading-text-editor/<p>Welcome to first tutorial in the Video Tutorial Series of XHTML and CSS, you can find list of all tutorials on XHTML and CSS , and you can watch and<a href="http://www.youtube.com/user/TheGeekyWayDotCom/videos">subscribe the channel on YouTube.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/VideoTut1XHTMLandCSS.png" title="Video Tutorial on XHTML and CSS 1. Overview and Downloading Text Editor"><img alt="Video Tutorial on XHTML and CSS 1. Overview and Downloading Text Editor" src="//thegeekyway.com/uploads/images/2014/02/VideoTut1XHTMLandCSS.png"></a></p>
</div>
<p>In this tutorial, we'll get started with Web Development by understanding how to develop webpages. The …</p><p>Welcome to first tutorial in the Video Tutorial Series of XHTML and CSS, you can find list of all tutorials on XHTML and CSS , and you can watch and<a href="http://www.youtube.com/user/TheGeekyWayDotCom/videos">subscribe the channel on YouTube.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/VideoTut1XHTMLandCSS.png" title="Video Tutorial on XHTML and CSS 1. Overview and Downloading Text Editor"><img alt="Video Tutorial on XHTML and CSS 1. Overview and Downloading Text Editor" src="//thegeekyway.com/uploads/images/2014/02/VideoTut1XHTMLandCSS.png"></a></p>
</div>
<p>In this tutorial, we'll get started with Web Development by understanding how to develop webpages. The basic semantics of the webpage is done through XHTML and styling is done through CSS.</p>
<p>In this video tutorial, we'll know how we can see the rendered HTML coding of any webpage once it's retrieved from the server and then analyse different sections.</p>
<p><strong>Pre-Requisites for this and the coming tutorials :-</strong></p>
<p>You need just a good text editor,</p>
<ul>
<li>For Windows, go with Notepad ++, download it from <a href="www.notepad-plus-plus.org">www.notepad-plus-plus.org</a></li>
<li>For linux there are many good editors like Vim , GEdit, Nano etc.</li>
</ul>
<iframe width="425" height="350" src="http://www.youtube.com/embed/dYKyHnfpmtc" frameborder="0"></iframe>
<p>In this and the coming tutorial series, we'll be using a Text Editor known as <strong>Notepad ++</strong> for coding HTML on <strong>Windows</strong> platform, if you're on a different platform search for some appropriate text editor, feel free to try out any one from the above mentioned editors.</p>
<p>Stay tuned for more, if you have any queries, feel free to ask through comments section below.</p>Free Video Tutorials2014-02-09T13:46:00+05:302014-02-09T13:46:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-09:/free-video-tutorials/<p>So, finally we decided to make the site more spicy by giving out free video tutorials for you so that you can understand stuff in a better way.</p>
<p>This page would be frequently updated with links to different sections and video tutorials</p>
<p>You can subscribe the <a href="http://www.youtube.com/user/TheGeekyWayDotCom/videos">Official Channel of TheGeekyWay …</a></p><p>So, finally we decided to make the site more spicy by giving out free video tutorials for you so that you can understand stuff in a better way.</p>
<p>This page would be frequently updated with links to different sections and video tutorials</p>
<p>You can subscribe the <a href="http://www.youtube.com/user/TheGeekyWayDotCom/videos">Official Channel of TheGeekyWay.com on Youtube.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/FreeVideoTutorials.png" title="Free Video Tutorials from TheGeekyWayDotCom"><img alt="Free Video Tutorials from TheGeekyWayDotCom" src="//thegeekyway.com/uploads/images/2014/02/FreeVideoTutorials.png"></a></p>
</div>
<p style="font-size: large;">***List of Tutorials ***</p>
<p><strong>Tutorials on XHTML and CSS</strong></p>
<ol>
<li><a href="http://www.thegeekyway.com/video-tut-xhtml-and-css-1-overview-downloading-text-editor/">Overview and Downloading Text Editor</a></li>
<li>Basic Semantics of a Web page and Doctype Decleration</li>
</ol>Java Multithreading : Thread States and Thread Priority2014-02-08T23:16:00+05:302014-02-08T23:16:00+05:30Anooptag:thegeekyway.com,2014-02-08:/java-multithreading-thread-states-explained/<p>In a thread lifecycle it can have one of the following states mentioned below. I have already explained <a href="http://www.thegeekyway.com/java-multithreading/#lifecycle">Thread Life Cycle</a> and basics of thread including <a href="http://www.thegeekyway.com/java-multithreading/#daemonthread">Daemon Threads</a>. <p style="line-height: 1.5em;">Now let see inside the thread states and how to achieve them .</p></p>
<ul>
<li><a href="#newRT">NEW </a>: <em>A thread has not started yet.</em></li>
<li><a href="#newRT">RUNNABLE </a>: <em>Thread is …</em></li></ul><p>In a thread lifecycle it can have one of the following states mentioned below. I have already explained <a href="http://www.thegeekyway.com/java-multithreading/#lifecycle">Thread Life Cycle</a> and basics of thread including <a href="http://www.thegeekyway.com/java-multithreading/#daemonthread">Daemon Threads</a>. <p style="line-height: 1.5em;">Now let see inside the thread states and how to achieve them .</p></p>
<ul>
<li><a href="#newRT">NEW </a>: <em>A thread has not started yet.</em></li>
<li><a href="#newRT">RUNNABLE </a>: <em>Thread is running state but it can be in state of waiting.</em></li>
<li><a href="#blocked">BLOCKED </a>: <em>Thread is waiting to acquire monitor lock to enter into a synchronized block/method after calling <p style="font-family: 'courier new', courier;">Object.wait()</p></em></li>
<li><a href="#timedWait">WAITING </a>: <em>A thread is in waiting state due to calling one of the following methods</em><ul>
<li>Object.wait() : <em>It causes current thread to wait until it been notified by method <p style="font-family: 'courier new', courier;"> </p> <p style="font-family: terminal, monaco;"> </p>or <p style="font-family: 'courier new', courier;">notifyAll().</p></em></li>
<li>Object.join() : <em>Waits for current thread to die</em>.</li>
<li><a href="#locksupport">LockSupport.park </a>:<em> Disables the current thread for thread scheduling purposes unless the permit is available.</em></li>
</ul>
</li>
<li><a href="timedWait">TIMED_WAITING </a>: <em>Current thread is waits for another thread for specified time to perform the aciton.</em><ul>
<li>Thread.sleep (long timeInMilliSecond) : <em>Makes current thread to cease the execution for specified time.</em></li>
<li>Object.wait (long timeInMilliSecond) : * Causes current thread to wait for specified time until time elapsed or get notified by <p style="font-family: 'courier new', courier;">notify()</p> or <p style="font-family: 'courier new', courier;">notifyAll()</p>.*</li>
<li>Thread.join (long millis) : <em>Current thread waits for specified time to die the thread.</em></li>
<li>LockSupport.parkNanos (long nanoSeconds) : <em>Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available.</em></li>
<li>LockSupport.parkUntil ()</li>
</ul>
</li>
<li><a href="#newRT">TERMINATED </a>: <em>When thread completed its execution.</em></li>
</ul>
<p>Also, how to set <a href="#priority">Thread Priority</a></p>
<p><strong> <p #newRT></p>NEW, RUNNING and TERMINATED</strong></p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">ThreadStates</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="k">Exception</span><span class="err">{</span>
<span class="w"> </span><span class="n">MyThreads</span><span class="w"> </span><span class="n">firstObj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyThreads</span><span class="p">(</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="n">MyThreads</span><span class="w"> </span><span class="n">secondObj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyThreads</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">firstObj</span><span class="p">,</span><span class="w"> </span><span class="ss">"firstThread"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">secondObj</span><span class="p">,</span><span class="w"> </span><span class="ss">"secondThread"</span><span class="p">);</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t1</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="o">+</span><span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t2</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="o">+</span><span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t1</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="o">+</span><span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t2</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="o">+</span><span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">MyThreads</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">long</span><span class="w"> </span><span class="n">sleeptime</span><span class="p">;</span>
<span class="w"> </span><span class="n">MyThreads</span><span class="p">(</span><span class="n">long</span><span class="w"> </span><span class="n">sleeptime</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">sleeptime</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sleeptime</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><=</span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">currentThread</span><span class="p">().</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="ss">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">sleeptime</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InterruptedException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="o">---</span><span class="w"> </span><span class="k">Output</span><span class="o">--</span>
<span class="n">firstThread</span><span class="w"> </span><span class="k">NEW</span>
<span class="n">secondThread</span><span class="w"> </span><span class="k">NEW</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">0</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">0</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">1</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">2</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">3</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">4</span>
<span class="n">firstThread</span><span class="w"> </span><span class="mi">5</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">1</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">2</span>
<span class="n">firstThread</span><span class="w"> </span><span class="n">TERMINATED</span>
<span class="n">secondThread</span><span class="w"> </span><span class="n">TIMED_WAITING</span><span class="w"> </span><span class="p">(</span><span class="n">due</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="nc">time</span><span class="p">)</span><span class="w"> </span><span class="k">call</span><span class="p">)</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">3</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">4</span>
<span class="n">secondThread</span><span class="w"> </span><span class="mi">5</span>
</code></pre></div>
<p><strong>WAITING and <p #timedWait></p>TIMED_WAITING due to<em> <p style="font-family: 'courier new', courier;"> wait()</p></em> and <p style="font-family: 'courier new', courier;"><em>wait(timeGiven)</em> :</p></strong></p>
<p>When <em>wait()</em> is called on a Thread, it will keep waiting until notified.</p>
<p>While <em>wait(timeGiven)</em>, wait until the <em>timeGiven</em> expired or notified.</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">RunWait</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="n">InterruptedException</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">WaitDemo</span><span class="w"> </span><span class="n">waitDemo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">WaitDemo</span><span class="p">();</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">waitDemo</span><span class="p">,</span><span class="w"> </span><span class="ss">"WaitThread"</span><span class="p">);</span>
<span class="w"> </span><span class="n">t</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="o">+</span><span class="ss">" checking in main() "</span><span class="o">+</span><span class="n">t</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="n">waitDemo</span><span class="p">.</span><span class="n">setValue</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">WaitDemo</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="err">{</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="k">boolean</span><span class="w"> </span><span class="n">isValueSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">false</span><span class="p">;</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">synchronized</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">while</span><span class="p">(</span><span class="err">!</span><span class="n">isValueSet</span><span class="w"> </span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">wait</span><span class="p">();</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="k">Check</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="n">wait</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">TIMED_WAITING</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InterruptedException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="ss">"Got the Value: "</span><span class="o">+</span><span class="n">isValueSet</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">setValue</span><span class="p">()</span><span class="err">{</span>
<span class="w"> </span><span class="n">synchronized</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">isValueSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">true</span><span class="p">;</span>
<span class="w"> </span><span class="n">notifyAll</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="o">---</span>
<span class="k">Output</span>
<span class="n">WaitThread</span><span class="w"> </span><span class="n">checking</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="n">WAITING</span>
<span class="n">Got</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">Value</span><span class="err">:</span><span class="w"> </span><span class="k">true</span>
</code></pre></div>
<p><strong> <p #blocked></p>BLOCKED</strong> : When thread is waiting to enter acquire monitor lock or to enter into synchronized method/block or to enter into Critical Section</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">BlockedDemo</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">BlockedThread</span><span class="w"> </span><span class="n">blockedThread</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">BlockedThread</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">blockedThread</span><span class="p">,</span><span class="w"> </span><span class="ss">"t1"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">blockedThread</span><span class="p">,</span><span class="w"> </span><span class="ss">"t2"</span><span class="p">);</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t1</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="w"> </span><span class="o">+</span><span class="n">t1</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">t2</span><span class="p">.</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="w"> </span><span class="o">+</span><span class="n">t2</span><span class="p">.</span><span class="n">getState</span><span class="p">());</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">BlockedThread</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">givenVar</span><span class="p">;</span>
<span class="w"> </span><span class="n">BlockedThread</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">givenVar</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">givenVar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">givenVar</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">synchronized</span><span class="w"> </span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w"> </span><span class="n">givenVar</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">givenVar</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InterruptedException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">currentThread</span><span class="p">().</span><span class="n">getName</span><span class="p">()</span><span class="o">+</span><span class="ss">" changed value "</span><span class="o">+</span><span class="w"> </span><span class="n">givenVar</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="o">--------------</span>
<span class="k">Output</span>
<span class="n">t1</span><span class="w"> </span><span class="n">TIMED_WAITING</span>
<span class="n">t2</span><span class="w"> </span><span class="n">BLOCKED</span>
<span class="n">t1</span><span class="w"> </span><span class="n">changed</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="mi">10</span>
<span class="n">t2</span><span class="w"> </span><span class="n">changed</span><span class="w"> </span><span class="k">value</span><span class="w"> </span><span class="mi">20</span>
</code></pre></div>
<p>Observe the output, Thread t2 is in blocked status while t1 is executing. Without synchronized block both thread will read original value of givenVar (try without synchronized block).</p>
<p>Note: Here two threads are working on single object and synchronized block ensuring that only one thread will enter inside the block. This block also known as Critical section.</p>
<p><strong>WAITING and TIMED_WAITING becuase of <p style="font-family: 'courier new', courier;">Thread.join()</p> and <p style="font-family: 'courier new', courier;">Thread.join(givenTime)</p></strong></p>
<p>threadX.join() -- It makes calling thread to wait for threadXto finish.</p>
<p>threadX.join(1000) -- Calling thread will be glued to thread threadX for 1000 miliSeconds and resume after this.</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">ThreadJoinDemo</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="n">InterruptedException</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">ThreadJoin</span><span class="w"> </span><span class="n">threadJoin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ThreadJoin</span><span class="p">();</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">threadJoin</span><span class="p">);</span>
<span class="w"> </span><span class="n">t</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t</span><span class="p">.</span><span class="k">join</span><span class="p">();</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="ss">"in main() method"</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">ThreadJoin</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="err">{</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="ss">"Entered run()"</span><span class="p">);</span>
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="p">(</span><span class="n">i</span><span class="o"><</span><span class="mi">5</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InterruptedException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="ss">"Finished run()"</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p><strong> WAITING due to[ <p style="font-family: 'courier new', courier;">LockSupport.park()</p> : <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/LockSupport.html">Javadoc</a>]{style="font-family: 'courier new', courier;"}</strong></p>
<p>Methods park and unpark provide efficient means of blocking and unblocking threads and avoids the the Race condition between one thread is parking another is unparking.</p>
<p>Following is simplest program I code to demonstrate this situation to <em>unpark</em> by other thread.</p>
<div class="highlight"><pre><span></span><code><span class="nt">public</span><span class="w"> </span><span class="nt">class</span><span class="w"> </span><span class="nt">NewPark</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">public</span><span class="w"> </span><span class="err">static</span><span class="w"> </span><span class="err">void</span><span class="w"> </span><span class="err">main(String</span><span class="w"> </span><span class="cp">[]</span><span class="err">args)</span><span class="w"> </span><span class="err">throws</span><span class="w"> </span><span class="err">InterruptedException</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="err">FIFOMutex</span><span class="w"> </span><span class="err">fifoMutex</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">new</span><span class="w"> </span><span class="err">FIFOMutex()</span><span class="p">;</span>
<span class="w"> </span><span class="err">Thread</span><span class="w"> </span><span class="err">t1</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">new</span><span class="w"> </span><span class="err">Thread(fifoMutex,"t1")</span><span class="p">;</span>
<span class="w"> </span><span class="err">t1.start()</span><span class="p">;</span>
<span class="w"> </span><span class="err">Thread.sleep(1000)</span><span class="p">;</span>
<span class="w"> </span><span class="err">System.out.println(t1.getName()</span><span class="w"> </span><span class="err">+"</span><span class="w"> </span><span class="err">"+</span><span class="w"> </span><span class="err">t1.getState())</span><span class="p">;</span>
<span class="w"> </span><span class="err">fifoMutex.unlock(t1)</span><span class="p">;</span><span class="w"> </span><span class="err">//unPaking</span><span class="w"> </span><span class="err">thread</span><span class="w"> </span><span class="err">t1</span>
<span class="w"> </span><span class="p">}</span>
<span class="err">}</span>
<span class="nt">class</span><span class="w"> </span><span class="nt">FIFOMutex</span><span class="w"> </span><span class="nt">implements</span><span class="w"> </span><span class="nt">Runnable</span><span class="p">{</span>
<span class="w"> </span><span class="err">public</span><span class="w"> </span><span class="err">void</span><span class="w"> </span><span class="err">lock(){</span>
<span class="w"> </span><span class="err">System.out.println("Parking</span><span class="w"> </span><span class="err">Thread</span><span class="w"> </span><span class="err">"+Thread.currentThread().getName())</span><span class="p">;</span>
<span class="w"> </span><span class="err">LockSupport.park()</span><span class="p">;</span>
<span class="w"> </span><span class="err">System.out.println("After</span><span class="w"> </span><span class="err">unpark</span><span class="w"> </span><span class="err">"+Thread.currentThread().getName())</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nt">public</span><span class="w"> </span><span class="nt">void</span><span class="w"> </span><span class="nt">unlock</span><span class="o">(</span><span class="nt">Thread</span><span class="w"> </span><span class="nt">thread</span><span class="o">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">System.out.println("</span><span class="n">Unparking</span><span class="p">:</span><span class="w"> </span><span class="err">"</span><span class="o">+</span><span class="n">thread</span><span class="o">.</span><span class="nf">getName</span><span class="p">());</span>
<span class="w"> </span><span class="err">LockSupport.unpark(thread)</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nt">public</span><span class="w"> </span><span class="nt">void</span><span class="w"> </span><span class="nt">run</span><span class="o">()</span><span class="p">{</span>
<span class="w"> </span><span class="err">lock()</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="err">}</span>
<span class="nt">---</span>
<span class="nt">Output</span><span class="o">:</span>
<span class="nt">Parking</span><span class="w"> </span><span class="nt">Thread</span><span class="w"> </span><span class="nt">t1</span>
<span class="nt">t1</span><span class="w"> </span><span class="nt">WAITING</span>
<span class="nt">Unparking</span><span class="o">:</span><span class="w"> </span><span class="nt">t1</span>
<span class="nt">After</span><span class="w"> </span><span class="nt">unpark</span><span class="w"> </span><span class="nt">t1</span>
</code></pre></div>
<p><strong> <p #priority></p>Setting Thread Priority</strong></p>
<p>You can set priority from 1 (lowest) to 10 (highest) by using thread.setPriority(number) or can use Thread.MAX_PRIORITY, Thread.MIN_PRIORITY. By default thread priority set to 5 that is Thread.NORM_PRIORITY.</p>
<p>Setting priority doesn't guarantee to execute higher priority thread to run first. It just hint the scheduler that this thread has high priority but it's depends on scheduler how to queue the threads.</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">ThreadPriority</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="n">InterruptedException</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">PriorityThread</span><span class="w"> </span><span class="n">priorityThread</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">PriorityThread</span><span class="p">();</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">priorityThread</span><span class="p">,</span><span class="w"> </span><span class="ss">"Thread1"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">priorityThread</span><span class="p">,</span><span class="w"> </span><span class="ss">"Thread2"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">priorityThread</span><span class="p">,</span><span class="w"> </span><span class="ss">"Thread3"</span><span class="p">);</span>
<span class="w"> </span><span class="n">t3</span><span class="p">.</span><span class="n">setPriority</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">MAX_PRIORITY</span><span class="p">);</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="n">setPriority</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">MIN_PRIORITY</span><span class="p">);</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="n">setPriority</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">NORM_PRIORITY</span><span class="p">);</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t3</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">PriorityThread</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">currentThread</span><span class="p">().</span><span class="n">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="ss">" "</span><span class="o">+</span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>I hope threads states are clear now, Feel free to comment and share.</p>Understanding Permissions With umask2014-02-07T22:42:00+05:302014-02-07T22:42:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-02-07:/understanding-permissions-umask/<p>Linux was developed as a multi-user system. One big problem with multi-user systems is securing information of a particular user from other users. To do that, a concept of permissions was introduced. When you create a file, you have the right to read and write it. In this article we …</p><p>Linux was developed as a multi-user system. One big problem with multi-user systems is securing information of a particular user from other users. To do that, a concept of permissions was introduced. When you create a file, you have the right to read and write it. In this article we are going to understand how these permissions are handled.</p>
<p>A user can read from a file, write into it or execute it (if it’s executable). These actions are represented by r for read, w for write and x for executable. Now, there are three type of actors in play in the system, first is user, second is the group the user belongs to and the third is everyone else. Hence, the permissions are in the form of three digits. The default permissions for a file are 666 (i.e. -rw-rw-rw-) and for directory they are 777 ( i.e. drwxrwxrwx). Here the x for directory means search permissions and d in the start indicated it’s a directory.</p>
<p>Now, this is place where umask comes in picture. When you create a file or directory, the default permissions will be given, but may be you don’t want your file to be readable/writable by everyone, for that we use umask. Fire up your console and type umask. You will find 0002 on most of the systems.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span><span class="nb">umask</span>
<span class="m">0002</span>
</code></pre></div>
<p>Now, leave the leading zero and you are left with three digits i.e. 002. This value is subtracted from default values (666 for files and 777 for directories) and the resulting values are set as permissions.</p>
<div style="text-align:center">
<p><strong><img alt="" src="https://lh5.googleusercontent.com/3qlpYN3OkS4o1Lctj8oD-ClQTmQp7LYK3Z79DaioR7uH11ftV0h9kAKxhDlSkS0nk3sCumPx-L5aybUd-vIpv2GFmBAI4QSJ6HHOhpkLqqR-g1xHnXpriIiuvw"></strong></p>
</div>
<p>As you can see the permission for file is 664 (666-002) and for the directory it is 775 (777-002). You can also change the umask and set permissions according to your requirements.</p>
<div style="text-align:center">
<p><strong><img alt="" src="https://lh6.googleusercontent.com/b-vgaM38MX_gDCaLlNqjwFVW2ZKIucJXjwbYh2RH9CcHGmsjwdJpeFk1C30Dip6UUfbZrNPqwyAMODa4F3w4RQqzRExTLhHl4LT4Dg6QWblEpL34rwTF4agRSA"></strong></p>
</div>
<p>You can revert back to your original umask by typing</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span><span class="nb">umask</span><span class="w"> </span><span class="m">002</span>
</code></pre></div>Disclaimer2014-02-07T20:50:00+05:302014-02-07T20:50:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-07:/disclaimer/<p>This blog is a collaborative blog written by a group of individuals. For questions about this blog, please contact admin\@TheGeekyWay.com.</p>
<h2>Disclaimers for www.TheGeekyWay.com:</h2>
<p>All the information on this website is published in good faith and for general information purpose only. www.TheGeekyWay.com does not make …</p><p>This blog is a collaborative blog written by a group of individuals. For questions about this blog, please contact admin\@TheGeekyWay.com.</p>
<h2>Disclaimers for www.TheGeekyWay.com:</h2>
<p>All the information on this website is published in good faith and for general information purpose only. www.TheGeekyWay.com does not make any warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on this website (www.TheGeekyWay.com), is strictly at your own risk. www.TheGeekyWay.com will not be liable for any losses and/or damages in connection with the use of our website.</p>
<p>All advertising is in the form of advertisements generated by a third party ad network. Those advertisements will be identified as paid advertisements.</p>
<p>The owner(s) of this blog is compensated to provide opinion on products, services, websites and various other topics. Even though the owner(s) of this blog receives compensation for our posts or advertisements, we always give our honest opinions, findings, beliefs, or experiences on those topics or products. The views and opinions expressed on this blog are purely the bloggers' own. Any product claim, statistic, quote or other representation about a product or service should be verified with the manufacturer, provider or party in question.</p>
<p>This blog does not contain any content which might present a conflict of interest.</p>
<p>From our website, you can visit other websites by following hyperlinks to such external sites. While we strive to provide only quality links to useful and ethical websites, we have no control over the content and nature of these sites. These links to other websites do not imply a recommendation for all the content found on these sites. Site owners and content may change without notice and may occur before we have the opportunity to remove a link which may have gone 'bad'.</p>
<p>Please be also aware that when you leave our website, other sites may have different privacy policies and terms which are beyond our control. Please be sure to check the Privacy Policies of these sites as well as their "Terms of Service" before engaging in any business or uploading any information.</p>
<h3>Consent</h3>
<p>By using our website, you hereby consent to our disclaimer and agree to its terms.</p>
<h3>Update</h3>
<p>This site disclaimer was last updated on: Sunday, February 2nd, 2014<br>
<em>· Should we update, amend or make any changes to this document, those changes will be prominently posted here.</em></p>
<hr>Executing your Android Application (AVD's) (Android-Part 3)2014-02-05T23:31:00+05:302014-02-05T23:31:00+05:30gagan93tag:thegeekyway.com,2014-02-05:/executeandroid/<p>[Hello Geeks, hope you have gone through <a href="http://www.thegeekyway.com/firstandroidapp/" title="prev post">this post</a>and have a good understanding of Android projects before proceeding further. To test / debug our Android Application, we can use any of available methods :]{style="font-size: 14px;line-height: 1.5em"}<br>
1. We can use the Android Virtual Device (we'll create one …</p><p>[Hello Geeks, hope you have gone through <a href="http://www.thegeekyway.com/firstandroidapp/" title="prev post">this post</a>and have a good understanding of Android projects before proceeding further. To test / debug our Android Application, we can use any of available methods :]{style="font-size: 14px;line-height: 1.5em"}<br>
1. We can use the Android Virtual Device (we'll create one shortly) to execute this application.<br>
2. We can directly copy the APK to the phone to test the application.<br>
3. We can also use an Android Emulator like Bluestacks to test our application. Benefit of using Bluestacks is that it starts much faster than AVDs.</p>
<p>1. We'll discuss first option in detail. The second option require a simple setup. On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development. On Android 4.0 and newer, it's in Settings > Developer options. (<a href="http://developer.android.com/tools/device.html" title="Developers Google">Detailed Explanation here</a>).The third option required downloading and installing <strong><a href="http://www.bluestacks.com/" title="Bluestacks">Bluestacks emulator</a>.</strong> Bluestacks is a free android <a href="http://en.wikipedia.org/wiki/Emulator" title="meaning of emulator">emulator</a> available for both Windows and Mac platform. It can be used to run most of the android applications including Whatsapp, games etc.</p>
<p>To create new <strong>AVD</strong> <strong>(Android Virtual Device)</strong> follow these steps <strong><br>
</strong></p>
<ul>
<li>Goto Windows Menu -> Android Virtual Device Manager.</li>
<li>Click 'New' to create a new Device.</li>
<li>Fill in mandatory fields like AVD Name, Device (preferrably Nexus 4, 4.7"as it is a portrait device), Target API 18.</li>
<li>Fill in Internal Storage and SD Card size as 200 MB (Enough for applications). Press Ok to create that device. Now close the AVD window.</li>
</ul>
<p>Now we will be able to use this device for executing the Android Applications we create. But we may also opt for Mobile Phone / Bluestacks method as they are very fast. The AVDs take a long time to start, approx 2 minutes on core i7 4th generation processor to over 5-7 minutes on old systems. If you still prefer executing application using AVDs then you must start AVD when you start the Eclipse IDE, so that the AVD loads while you code your application.</p>
<p>To execute the first Android Project Goto "Run" from Menu Bar and select first option, or you could simply use Ctrl+ F11. Choose your AVD, which would be the one just created, or your phone(if attached) / bluestacks (if installed).Preview of this application (Executed on Bluestacks) is shows here </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/15.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/02/15-1024x604.jpg"></a></p>
</div>
<div style="text-align:center">
<p>A similar application with two labels and buttons executed on Android 4.0 Virtual Device is as shown below : <a href="//thegeekyway.com/uploads/images/2014/02/18.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/02/18.jpg"></a></p>
</div>
<p>This was a very short tutorial to setup AVD and execute your first android application.In the next Article, we will create simple application by creating its GUI in layout.xml file and obtaining IDs in the JAVA code. Till then stay tuned & comment if you have any suggestions/doubts.</p>
<p><a href="http://www.thegeekyway.com/secondapp1/" title="link"> <p style="font-size: xx-large">Link to next tutorial</p></a></p>Java Multithreading2014-02-05T23:31:00+05:302014-02-05T23:31:00+05:30Anooptag:thegeekyway.com,2014-02-05:/java-multithreading/<p>*Threads. * Thread is also known as lightweight process. Java multithreading allows to process multiple task simultaneously with less overhead than process. Mostly use to monitor session, background synchronization etc.</p>
<h5>Content</h5>
<ul>
<li><a href="#processes">Process vs Thread</a></li>
<li><a href="#firstthread">Creating first thread program</a><ul>
<li><a href="#runnableVsThread">Implementing Runnable vs extending Thread</a></li>
</ul>
</li>
<li><a href="#lifecycle">Thread State and Life cycle</a></li>
<li><a href="#daemonthread">Daemon Thread</a></li>
</ul>
<p #processes></p>
<p><strong>Processes …</strong></p><p>*Threads. * Thread is also known as lightweight process. Java multithreading allows to process multiple task simultaneously with less overhead than process. Mostly use to monitor session, background synchronization etc.</p>
<h5>Content</h5>
<ul>
<li><a href="#processes">Process vs Thread</a></li>
<li><a href="#firstthread">Creating first thread program</a><ul>
<li><a href="#runnableVsThread">Implementing Runnable vs extending Thread</a></li>
</ul>
</li>
<li><a href="#lifecycle">Thread State and Life cycle</a></li>
<li><a href="#daemonthread">Daemon Thread</a></li>
</ul>
<p #processes></p>
<p><strong>Processes : </strong>These are often application or programs running in their own memory space and runtime resources. In simple term when you run new program with main() method . It is basically a new process and have their own private memory, and resources.</p>
<p><strong>Thread:</strong> The path of execution of a process is known as <em>Thread. </em>A process can have more than one thread. In a simple program with <em>main()</em> method.This program contains a user defined thread with<em> main()</em> method. While with user threads there are <em>system</em> threads like memory management and signal handling.</p>
<ul>
<li>Threads are also known as <em>lightweight processes.</em></li>
<li>A process must contain at least one Thread.</li>
<li>All threads inside a process can communicate each other via shared memory space (Shared variable)</li>
</ul>
<p><strong> <p #firstthread></p>Creating first Java Thread</strong> There are two way to create java threads.</p>
<ul>
- In both cases we need to override run() method and this method is part of a thread. A thread start from start from starting of run() and ends when run() ends.
- To start thread, Thread object needs to be start using *threadObj.start()* which invoke the thread and execute the run method.
</ul>
<ol>
<li>Implementing <em>Runnable interface </em>:</li>
</ol>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">ThreadExtendingRunnable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">MyThread1</span><span class="w"> </span><span class="n">obj1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyThread1</span><span class="p">(</span><span class="ss">"thread1"</span><span class="p">);</span>
<span class="w"> </span><span class="n">MyThread1</span><span class="w"> </span><span class="n">obj2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">MyThread1</span><span class="p">(</span><span class="ss">"thread2"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">obj1</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">obj2</span><span class="p">);</span><span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">pass</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">same</span><span class="w"> </span><span class="k">object</span><span class="p">.</span><span class="w"> </span><span class="n">It</span><span class="w"> </span><span class="n">allows</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">associate</span><span class="w"> </span><span class="n">single</span><span class="w"> </span><span class="k">object</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">multiple</span><span class="w"> </span><span class="n">threads</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">MyThread1</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">str</span><span class="p">;</span>
<span class="w"> </span><span class="n">MyThread1</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="nf">str</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="nf">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">str</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><=</span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="nf">str</span><span class="o">+</span><span class="ss">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
</code></pre></div>
<p>2. <p style="line-height: 1.5em;">Extending </p>*Thread class *</p>
<div class="highlight"><pre><span></span><code><span class="n">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">MyThreadExtendingThread</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="p">(</span><span class="nb nb-Type">String</span><span class="w"> </span><span class="p">[]</span><span class="n">args</span><span class="p">){</span>
<span class="w"> </span><span class="n">MyThread2</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="n">MyThread2</span><span class="p">(</span><span class="s2">"thread1"</span><span class="p">)</span><span class="w"> </span><span class="p">;</span>
<span class="w"> </span><span class="n">MyThread2</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="n">MyThread2</span><span class="p">(</span><span class="s2">"thread2"</span><span class="p">)</span><span class="w"> </span><span class="p">;</span>
<span class="w"> </span><span class="n">t1</span><span class="o">.</span><span class="n">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="o">.</span><span class="n">start</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">MyThread2</span><span class="w"> </span><span class="k">extends</span><span class="w"> </span><span class="n">Thread</span><span class="p">{</span>
<span class="w"> </span><span class="nb nb-Type">String</span><span class="w"> </span><span class="nb">str</span><span class="p">;</span>
<span class="w"> </span><span class="n">MyThread2</span><span class="p">(</span><span class="nb nb-Type">String</span><span class="w"> </span><span class="nb">str</span><span class="p">){</span>
<span class="w"> </span><span class="n">this</span><span class="o">.</span><span class="n">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">str</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">public</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">run</span><span class="p">(){</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><=</span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="nb">str</span><span class="o">+</span><span class="s2">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p style="text-decoration: underline;"> <p #runnableVsThread></p> Implementing *Runnable* vs Extending *Thread* </p>
<p>:</p>
<ul>
<li><p style="line-height: 24px;">Implementing Runnable allows you to extends another class while, if you extend Thread class you will not able to extends any other class.</p></li>
<li>Extending thread, each thread needs to associate with unique object.</li>
<li>Implementing Runnable allows you to associate single object to as many threads you want.</li>
<li>Sharing same object to multiple thread also avoids unnecessary object creation and saves memory.</li>
</ul>
<p><strong> <p #lifecycle></p>Thread State and Life Cycle</strong></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/state-machine-example-java-6-thread-states.png"><img alt="state-machine-example-java-6-thread-states" src="//thegeekyway.com/uploads/images/2014/02/state-machine-example-java-6-thread-states.png"></a> Source: <a href="http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html">uml-diagram.org</a></p>
</div>
<p>A thread can be one of the following state (java 1.5+)</p>
<ul>
<li><strong> <p style="font-family: 'courier new', courier;">NEW</p></strong> : A thread has not started yet.</li>
<li><strong> <p style="font-family: 'courier new', courier;">RUNNABLE</p></strong> : Thread is running state but it can be in state of waiting.</li>
<li><strong> <p style="font-family: 'courier new', courier;">BLOCKED</p></strong> : Thread is waiting to acquire monitor lock to enter into a synchronized block/method after calling Object.wait()</li>
<li><strong> <p style="font-family: 'courier new', courier;">WAITING</p></strong> : A thread is in waiting state due to calling one of the following methods<ul>
<li><strong> <p style="font-family: 'courier new', courier;">Object.wait()</p></strong> : It causes current thread to wait until it been notified by method <em>notify()</em> or <em>notifyAll().</em></li>
<li><strong> <p style="font-family: 'courier new', courier;">Object.join()</p></strong> : Waits for current thread to die.</li>
<li><strong> <p style="font-family: 'courier new', courier;">LockSupport.park</p></strong> : Disables the current thread for thread scheduling purposes unless the permit is available.</li>
</ul>
</li>
<li><strong> <p style="font-family: 'courier new', courier;">TIMED_WAITING</p></strong> : Current thread is waits for another thread for specified time to perform the aciton.<ul>
<li><strong> <p style="font-family: 'courier new', courier;">Thread.sleep (long timeInMilliSecond)</p></strong> : Makes current thread to cease the execution for specified time.</li>
<li><strong> <p style="font-family: 'courier new', courier;">Object.wait (long timeInMilliSecond) </p></strong>: Causes current thread to wait for specified time until time elapsed or get notified by notify() or notifyAll().</li>
<li><strong> <p style="font-family: 'courier new', courier;">Thread.join (long millis)</p></strong> : Current thread waits for specified time to die the thread.</li>
<li><strong> <p style="font-family: 'courier new', courier;">LockSupport.parkNanos (long nanoSeconds)</p></strong> : Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available.</li>
<li><p style="font-family: 'courier new', courier;"><strong>LockSupport.parkUntil ()</strong></p></li>
</ul>
</li>
<li><p style="font-family: 'courier new', courier;">TERMINATED</p> : When thread completed its execution.</li>
</ul>
<p>\<Post in progress to explain thread states></p>
<p><strong> <p #daemonthread></p>Daemon Thread</strong> Daemon threads are threads to which JVM don't watch to completed or not. So how JVM works, As soon as all user threads finished processing , JVM terminated itself. JVM doesn't wait to finish daemon threads. Garbage collector and other housekeeping threads are inbuild daemon threads.</p>
<ul>
<li>User created threads are non-daemon by default, since it's created by main() thread which is by default a non-daemon and newly created thread inherit daemon property from the thread it was created.</li>
<li>To set a user created thread as daemon, you need to set by <p style="font-family: 'courier new', courier;"><em>Thread.setDaemon(true)</em></p> (Make sure you set a daemon before calling<em> <p style="font-family: 'courier new', courier;"> start()</p></em>).</li>
</ul>
<p style="font-size: x-small;">Example: Daemon Thread</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">DaemonTest</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="err">[]</span><span class="n">args</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">DaemonObject</span><span class="w"> </span><span class="n">deamonObj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">DaemonObject</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span><span class="w"> </span><span class="ss">"daemonThread"</span><span class="p">);</span>
<span class="w"> </span><span class="n">DaemonObject</span><span class="w"> </span><span class="n">notDaemonObj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">DaemonObject</span><span class="p">(</span><span class="mi">500</span><span class="p">,</span><span class="w"> </span><span class="ss">"notDaemonThread"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">deamonObj</span><span class="p">);</span>
<span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="n">notDaemonObj</span><span class="p">);</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="n">setDaemon</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
<span class="w"> </span><span class="n">t1</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="n">t2</span><span class="p">.</span><span class="k">start</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">DaemonObject</span><span class="w"> </span><span class="n">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="err">{</span>
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">sleepTime</span><span class="p">;</span>
<span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">threadname</span><span class="p">;</span>
<span class="w"> </span><span class="n">DaemonObject</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">sleepTime</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">threadname</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">sleepTime</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sleepTime</span><span class="p">;</span>
<span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">threadname</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">threadname</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="nv">@Override</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">run</span><span class="p">()</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">threadname</span><span class="o">+</span><span class="w"> </span><span class="ss">" "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">sleepTime</span><span class="p">);</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InterruptedException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="err">}</span>
<span class="o">----------</span>
<span class="k">Output</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="ow">like</span><span class="err">:</span><span class="w"> </span>
<span class="o">----------</span>
<span class="n">daemonThread</span><span class="w"> </span><span class="mi">0</span>
<span class="n">notDaemonThread</span><span class="w"> </span><span class="mi">0</span>
<span class="n">notDaemonThread</span><span class="w"> </span><span class="mi">1</span>
<span class="n">daemonThread</span><span class="w"> </span><span class="mi">1</span>
<span class="n">notDaemonThread</span><span class="w"> </span><span class="mi">2</span>
<span class="n">notDaemonThread</span><span class="w"> </span><span class="mi">3</span>
<span class="n">daemonThread</span><span class="w"> </span><span class="mi">2</span>
<span class="n">notDaemonThread</span><span class="w"> </span><span class="mi">4</span>
<span class="n">Process</span><span class="w"> </span><span class="n">finished</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="k">exit</span><span class="w"> </span><span class="n">code</span><span class="w"> </span><span class="mi">0</span>
</code></pre></div>
<p>Notice, thread marked as daemon thread didn't completed its execution, because JVM only waited for non-daemon thread and as soon as it completed, JVM also terminated.</p>Anatomy of First Android Application (Android-Part 2)2014-02-03T22:22:00+05:302014-02-03T22:22:00+05:30gagan93tag:thegeekyway.com,2014-02-03:/firstandroidapp/<p>Hello Geeks, hope you all have read the <a href="http://www.thegeekyway.com/setting-up-eclipse-ide-for-android-application-development/" title="Setup Android IDE">previous post</a> for setting up IDE, here we explain what each folder of a newly created Android Application means. As soon as you follow the steps described in previous post, a simple android application with the application name and a predefined string …</p><p>Hello Geeks, hope you all have read the <a href="http://www.thegeekyway.com/setting-up-eclipse-ide-for-android-application-development/" title="Setup Android IDE">previous post</a> for setting up IDE, here we explain what each folder of a newly created Android Application means. As soon as you follow the steps described in previous post, a simple android application with the application name and a predefined string is created.The whole picture looks something like this (click on res/layout/activity_main.xml):</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/17.jpg"><img alt="New Proj" src="//thegeekyway.com/uploads/images/2014/02/17.jpg"></a></p>
</div>
<p>The basic structure of an android application looks like this :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/16.jpg"><img alt="package overview" src="//thegeekyway.com/uploads/images/2014/02/16.jpg"></a></p>
</div>
<p>The highlighted part in the above image shows expanded form of a Android Project. On the top left of tree, we have the name of the Application itself. Under that we have the following folders :</p>
<ul>
<li><strong>src :</strong> SRC means SOURCE. Source folder stores the source of your application. This folder stores name of the package and then the source file(s). Here we have MainActivity.java as the main source file, which opens on Right side.</li>
<li><strong>gen</strong> : Gen means Generated Java Files. The folder stores generated Java files like R.java . These are AUTO GENERATED JAVA FILES which should not be edited.If there is any error in project (xml/source), the file is not generated. So we must remove The files like R.java are used to map layout files from <strong>Layout file (XML File) to MainActivity file (JAVA File).</strong></li>
<li><strong>Android 4.3 and Android Private Libraries :</strong> These are source files given by google, normally we do not require to do anything from these files. These are just imported, as required.</li>
<li><strong>assets :</strong> This folder stores the text files/HTMLs or databases related to your application.</li>
<li><strong>bin :</strong> As clear from the name, bin folder stores the binary output of your application i.e. the <strong>APK file</strong> which is sufficient to execute independently on your android device.</li>
<li><strong>lib :</strong> This folder contains one file by default (android support file). We can add any JAR files in this folder which may be used by our application. People who have worked with JAVA must be knowing how to include external jar files in application (by pasting in specific directories in C:/program files/java). This is just similar.</li>
<li><strong>res :</strong> res folder contains all the resources in your application. There are accessible in your application using its ID which is maintained in <strong>R.java (auto-generated file).</strong> Most of the people are often confused between res and assets, but here's the difference : <strong>Files inside assets folder are accessible in your java file as if it exists in the same folder, No IDs are created for asset files inside R.java. Whereas files in res folder are accessible using their IDs defined in R.java.</strong> Here is the explanation of each subfolder of <strong>res.</strong></li>
</ul>
<p>The first five subfolders in res (<strong>drawable</strong> types) consists of images of different sizes which may be used in your android application. Here <strong>hdpi</strong> means <strong>High Density Picture, mdpi</strong> means <strong>Medium Density Picture</strong> and so on<strong>.</strong></p>
<p>The next folder<strong>(layout)</strong> is the most important which consists of the layout of our application. By default, it consists of one file (activity_main.xml). As we create more activities, more files are added here as per our specification.</p>
<p>The <strong>menu</strong> folder can be used to create a custom menu inside an Android Application. It is not discussed here in detail.</p>
<p>The <strong>values</strong> folder contains many XML files which define constant objects for your Android Application. Our main concentration remains on <strong>strings.XML</strong> file which stores constant strings. These strings are accessed in Java application using their ID (eg. R.string.app_name) where <strong>string</strong> is the <strong>strings.xml</strong> file and <strong>app_name</strong> is the constant string <strong>defined in that xml file</strong> (app_name defines the name of android app).</p>
<p>The other versions of values folder are used for different APIs versions. Other than this we have the AndroidManifest.xml file which defined basic information about the Android Application.</p>
<p>Eclipse allows two views of an XML file which can be toggled by the TAB given below the XML file as shown here : </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/14.jpg"><img alt="AndroidManifest.XML" src="//thegeekyway.com/uploads/images/2014/02/14.jpg"></a></p>
</div>
<p>The last tab <strong>(AndroidManifest.xml)</strong> represents the XML view of the file whereas the first four tabs <strong>(Manifest, Application,Permissions and Instrumentation)</strong> represent the GUI view of the file. This is the advantage of using <strong>Eclipse IDE,</strong> i.e. for adding some new information to manifest, when we don't know the syntax, we can use the first four tabs so that we enter the information using GUI and Eclipse automatically adds the same information to the XML file.</p>
<p>The rest of the files describe basic properties of the file and do not require immediate discussion. So we defer this to future posts, they would be discussed as or when required. The next article describes how to create an Android Virtual Machine on your system to execute this created Application.</p>
<p><a href="http://www.thegeekyway.com/executeandroid/" title="link"> <p style="font-size: xx-large">Link to next tutorial</p></a></p>SR Flip-Flop: Description2014-02-03T21:20:00+05:302014-02-03T21:20:00+05:30Jeffrey_Samueltag:thegeekyway.com,2014-02-03:/sr-flip-flop-2/<p>In the <a href="http://www.thegeekyway.com/race-around-effect/" title="Race around Effect">previous post</a> we concentrated on the major advantage of Flip-Flop over Latches. In this Blog we will be concentrating more on the most basic type of Flip-Flops which is SR Flip-Flop. Do note that there are counterparts for every Flip-Flop in latch as well.</p>
<p>As we all know …</p><p>In the <a href="http://www.thegeekyway.com/race-around-effect/" title="Race around Effect">previous post</a> we concentrated on the major advantage of Flip-Flop over Latches. In this Blog we will be concentrating more on the most basic type of Flip-Flops which is SR Flip-Flop. Do note that there are counterparts for every Flip-Flop in latch as well.</p>
<p>As we all know the major types of Flip-FLop include</p>
<h2>Types of Flip-Flop:</h2>
<p>The major types of Flip-Flop are</p>
<ul>
<li>SR Flip-Flop</li>
<li>D- Flip-Flop</li>
<li>JK Flip-Flop</li>
<li>T Flip-Flop</li>
</ul>
<h2><strong>SR Flip-Flop:</strong></h2>
<p>This is the most basic of all the types of Flip-Flop. It is also known as Set - Reset Flip Flop. It has two excitation states. One state is the set state and the other is Reset state. In the set state the output of the Flip-Flop is high. In reset state the output is Low.</p>
<h3>State Diagram:</h3>
<p>The state diagram of an SR Flip-Flop is just a toggle between two states.The common states can be explained in the following points</p>
<ul>
<li>When the Set is high the output is High irrespective of the previous input</li>
<li>When Reset is high the output is Low irrespective of the previous input</li>
<li>If the SR Flip-Flop is off (i.e.,) S=0 and R=0 We have no change in the output of the SR Flip-flop.</li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/SR-Flip-Flop-1.jpg" title="State diagram of SR Flip-Flop"><img alt="State diagram of SR Flip-Flop" src="//thegeekyway.com/uploads/images/2014/02/SR-Flip-Flop-1.jpg"></a></p>
</div>
<h3></h3>
<h3>Truth table:</h3>
<p>Knowing the states we can draw the truth table as shown in the table below. The truth table gives us</p>
<hr>
<p><strong>S</strong> <strong>R</strong> <strong>Next State</strong>
0 0 0
0 1 0
1 0 1
1 1 X</p>
<hr>
<h3>Excitation table:</h3>
<p>The excitation states can be clearly seen from the table below. The excitation state gives us the various output possible for an SR Flip-Flop When the past output and the current values of SR is known. When we know the excitation table of an SR Flip-Flop, we can use it to implement other flip flops like D, T and JK.</p>
<p>+:-----------:+:-----------:+:-----------:+:-----------:+-------------+
| ### Present | ### S {#s s | ### R | ### Next St | ### Comment |
| State | tyle="text- | | ate | |
| | align: cent | | | |
| | er;"} | | | |
+-------------+-------------+-------------+-------------+-------------+
| 0 | 0 | 0 | 0 | Flip Flop |
| | | | | is OFF |
+-------------+-------------+-------------+-------------+-------------+
| 0 | 0 | 1 | 0 | Reset mode |
+-------------+-------------+-------------+-------------+-------------+
| 0 | 1 | 0 | 1 | Set mode |
+-------------+-------------+-------------+-------------+-------------+
| 0 | 1 | 1 | X | Undefined |
+-------------+-------------+-------------+-------------+-------------+
| 1 | 0 | 0 | 1 | Flip Flop |
| | | | | is OFF |
+-------------+-------------+-------------+-------------+-------------+
| 1 | 0 | 1 | 0 | Reset Mode |
+-------------+-------------+-------------+-------------+-------------+
| 1 | 1 | 0 | 1 | Set Mode |
+-------------+-------------+-------------+-------------+-------------+
| 1 | 1 | 1 | X | Undefined |
+-------------+-------------+-------------+-------------+-------------+</p>
<h3><strong>LIMITATIONS OF SR Flip-Flop:</strong></h3>
<p>We find that SR flip flop has one undefined cycle in its operation. This is one of the disadvantages of SR flip flop. Mostly while designing itself this condition is avoided. This defect is rectified in the JK flip-flop.</p>Setting Up Eclipse IDE for Android Application Development (Android-Part 1)2014-02-02T22:33:00+05:302014-02-02T22:33:00+05:30gagan93tag:thegeekyway.com,2014-02-02:/setting-up-eclipse-ide-for-android-application-development/<p>Here is a complete tutorial to setup Eclipse IDE. Hope you have seen the <a href="http://thegeekyway.com/beginandroid" title="Creating your first Android Application">first tutorial</a> and have installed the required tools (ADT + Eclipse & JDK).After installing JDK, extract the Android SDK to a folder and you will get something like this.</p>
<p>Few screenshots related to Eclipse may vary due …</p><p>Here is a complete tutorial to setup Eclipse IDE. Hope you have seen the <a href="http://thegeekyway.com/beginandroid" title="Creating your first Android Application">first tutorial</a> and have installed the required tools (ADT + Eclipse & JDK).After installing JDK, extract the Android SDK to a folder and you will get something like this.</p>
<p>Few screenshots related to Eclipse may vary due to different version, but the overall meaning remains same.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/1.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/1.jpg"></a></p>
</div>
<p><strong>Please note that Eclipse is just an Integrated Development Environment</strong> which supports a wide variety of development ranging from C/C++/Java/Android to Electronics. Our Eclispe is a one, tailored for Android. The Eclipse and Android SDK should be kept in the same folder as shown above so that eclipse IDE is able to detect the SDK and use it for further development. If you attempt to change the location of Eclipse or SDK, or rename the SDK, you get the following error.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/11.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/11.jpg"></a></p>
</div>
<p>So, now open eclipse folder and there you will find a file called <strong>eclipse.exe</strong>. Open that file, a window appears which shows option to select the <strong>Workspace.</strong> Workspace is a path where your entire project including source, APK and all other files are stored. After selecting preferences, something like this will appear.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/2.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/2.jpg"></a></p>
</div>
<p>To start creating a new project, either use the menubar (Flile -> New) or use keyboard shortcut (Alt+Shift+N). From the drop down menu, select <strong>Android Application Project.</strong> A new Dialog box appears. Fill in the basic application details to continue.</p>
<ul>
<li><strong>Application name : </strong>It is the name of the application as visible in your android phone</li>
<li><strong>Project name :</strong> It is the name of the project which will appear in the <a href="http://www.ibm.com/developerworks/library/wa-mobappdev1/Figure17.jpg" title="Example of tree structure">tree structure</a> in left panel of Eclipse</li>
<li><strong>Package name :</strong> People familiar with Java must be knowing the concept of a package (or a namespace in C++/C#). It is a unique identifier for your application.</li>
</ul>
<p>The best thing about IDEs is the way they explain each and every information. For eg, when we point on <strong>Package name,</strong> it shows the following text information as a help text explaining everything about the field.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/21.jpg"><img alt="Eclipse new Project" src="//thegeekyway.com/uploads/images/2014/02/21.jpg"></a></p>
</div>
<p>The other four fields are self explanatory, so are not explained here. Press <strong>Next </strong> to continue. You get the following dialog box. </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/22.jpg"><img alt="second screen" src="//thegeekyway.com/uploads/images/2014/02/22.jpg"></a></p>
</div>
<p>The options are explained here :-</p>
<ul>
<li><strong>Create custom launcher icon : </strong> This allows to choose a custom Application Icon for your application.</li>
<li><strong>Create Activity : </strong>Activity in an android application is somewhat similar to a JFrame/Frame in Java. An activity simply represents a screen in Android. <strong>Activity</strong> is also a class in Android. You must check this option so that eclipse creates a dummy activity (with required methods)</li>
<li><strong> <p style="font-size: small">Mark this project as library : </p></strong>Check this option if you want this project to act as library (or source) for another project (not to be given much stress now).</li>
<li><strong>Create Project in Workspace :</strong> This option allows to create all the files of the project inside the workspace which was selected when Eclipse started. This should be <strong>checked</strong> to store the files at that place and <strong>unchecked</strong> if you want to store the files anywhere else.</li>
<li><strong>Add project to working sets : </strong>This option is also related to linking multiple projects so should not be given much stress now.Click <strong>Next</strong> to continue. Now we get the following screen : </li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/23.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/23.jpg"></a></p>
</div>
<p>You can configure the icon for you application in this window. Try changing different colors, text,clipart etc and it would be clear to you. Most of the things are self explanatory and are not discussed here. Click next to continue. You will get the following screen. </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/12.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/12.jpg"></a></p>
</div>
<p>Now here we select the type of activity.</p>
<ul>
<li><strong>Blank Activity :</strong> It is an activity where you get empty implementation of required methods and status bar of android phone is visible. So it is not a fullscreen activity.</li>
<li><strong>Full Screen Activity :</strong> Usually games utilize complete screen and come under this category. Select this option so that this occupies the complete screen (including status bar).</li>
<li><strong>Master / Detailed Flow : </strong>This allows users to create activity in such a way so that it is able to display an object as well as information related to each object. Moreover, it created two activities, i.e. <strong>master</strong> and <strong>detailed</strong> fragment respectively.</li>
</ul>
<p>Click Next to continue. We get the following screen </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/1.png"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/1.png"></a></p>
</div>
<ul>
<li><strong>Activity Name :</strong> This is the name of new blank activity which starts the application.</li>
<li><strong>Layout Name :</strong> This specifies the layout for this activity. <strong>Please note that layout of an android activity is defined in XML inside layout folder.</strong></li>
</ul>
<p style="font-size: small">**Navigation Type : ** Specifies the type of navigation the user will use to navigate while using the app. See options for more information which are self explanatory. Let it be none for now.</p>
<p>Click Finish to start with the actual process.The following window opens in Eclipse. </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/13.jpg"><img alt="" src="//thegeekyway.com/uploads/images/2014/02/13-1024x608.jpg"></a></p>
</div>
<p>So now we have started with a simple Android Application. On the left hand side we have a list of folders which contain libraries and files related to our application. On the right hand side, we have the currently opened file. By default it shoes the main JAVA file of your application. Before starting coding, setting layouts etc. we require a complete understanding of all the folder on the left side. So in our next post tomorrow, we'll completely describe about each folder about what they mean and how to use them.</p>
<p><a href="http://www.thegeekyway.com/firstandroidapp/" title="link"> <p style="font-size: xx-large">Link to next Tutorial</p></a></p>Conditional Statements : Switch Case2014-02-01T21:02:00+05:302014-02-01T21:02:00+05:30sanyam_khuranatag:thegeekyway.com,2014-02-01:/conditional-statements-switch-case/<p>In the previous tutorial, we've discussed about <a href="http://www.thegeekyway.com/conditional-statements-if-else/" title="Understanding Conditional Statements: if else">if - else and nested if - else statements</a> and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we'll be covering another Conditional Statement used in …</p><p>In the previous tutorial, we've discussed about <a href="http://www.thegeekyway.com/conditional-statements-if-else/" title="Understanding Conditional Statements: if else">if - else and nested if - else statements</a> and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we'll be covering another Conditional Statement used in C known as Switch Case. The major difference between choosing to use if-else and switch is, when we have to compare a single variable to different set of values, then Switch is preferred, and if-else if preferred otherwise.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/SwitchCase1.png" title="Conditional Statements : Switch Case C language"><img alt="Conditional Statements : Switch Case C language" src="//thegeekyway.com/uploads/images/2014/02/SwitchCase1.png"></a></p>
</div>
<p>The basic semantics of a switch block statement is as follows :</p>
<div class="highlight"><pre><span></span><code><span class="n">switch</span><span class="p">(</span><span class="n">variable</span><span class="o">-</span><span class="n">name</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">1</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">2</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="mi">3</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="n">default</span><span class="p">:</span>
<span class="w"> </span><span class="o">.</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here variable-name corresponds to the variable you want to compare with a set of values and inside the curly braces we have defined cases such that if value of variable is 1 then the statement 1 and statement 2 would execute, for variable having value 2, statement 3 and 4 would execute and so on. If none of the cases are true, then statement n corresponding to default would execute.</p>
<p>Let us take an example and understand how it's done. Let us consider we need to just print what number it is if the number is less than or equal to 5 else print number is not 1, 2, 3, 4 or 5. We'll consider that user has input a number which is stored in variable named num. This can be done through if - else statements as we studied earlier like this :</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="k">else</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
</code></pre></div>
<p>Now in the above case, since we need to compare just one variable with a set of values, this can be done easily through switch case as follows:</p>
<div class="highlight"><pre><span></span><code><span class="nv">switch</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">num</span><span class="w"> </span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">default</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="k">break</span><span class="c1">;</span>
}
</code></pre></div>
<p>Notice here that I have mentioned break statement after each case , this is to terminate the the case above it if it's executed. Let's see if we didn't include break what would happen, so consider this code snippet.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Enter a number "</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">cin</span><span class="o">>></span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">switch</span><span class="w"> </span><span class="ss">(</span><span class="w"> </span><span class="nv">num</span><span class="w"> </span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 1 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 2 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 3 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 4 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">case</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is 5 n"</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">default</span><span class="w"> </span>:<span class="w"> </span><span class="nv">cout</span><span class="o"><<</span><span class="s2">"Number is not 1, 2, 3, 4 or 5 n"</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>Now, if we run this and input 2 is given then output would be as shown below</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Switch_Case.png" title="Switch case in C language Output Screen"><img alt="Switch case in C language Output Screen" src="//thegeekyway.com/uploads/images/2014/02/Switch_Case.png"></a></p>
</div>
<p>See, we not only got Number is 2 statement printed, but got all statements printed after that case, in a sequential manner, this is because in a switch condition we need to have conditional flow of statement using break statement.</p>
<p>Now, if you include break after each case but doesn't do it with default case, it would run fine too, this is because if the default case is true, then after executing set of statements with that case, the control would automatically be out of the switch block, so technically we do not need break after default case.</p>
<p>Now let's see how to match a character type variable. Consider a character (char) type variable named character_variable , and if we need to use switch, it can be done in this way:</p>
<div class="highlight"><pre><span></span><code><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">character_variable</span><span class="w"> </span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'a'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case a executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'b'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case b executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="s1">'c'</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"Case c executed n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="n">default</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">cout</span><span class="o"><<</span><span class="s2">"This is default case n"</span><span class="p">;</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here, notice that we have enclosed the character value between single quotes ( ' ' ).</p>
<p>Like wise, you can design your switch case for various purpose as/when you need to compare a single variable value with a set of values.</p>
<p>In next tutorials we'll know about the Storage Classes in C. If you have any problem with anything till now, feel free to let us know through comments section below, we'll be glad to help you.</p>
<p>Keep Coding !</p>Variables in PYTHON2014-01-31T21:07:00+05:302014-01-31T21:07:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-01-31:/variables-in-python/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/variables.jpg" title="Variables in python"><img alt="Variables in Python" src="//thegeekyway.com/uploads/images/2014/01/variables.jpg"></a></p>
</div>
<h2>Variables</h2>
<p>Variables are used for storing data in the form of integer,char ,boolean etc.They act as a storage medium for us . Based on the datatype of variable interpreter allocates the memory required by that datatype. There are 4 types of variables.</p>
<ul>
<li>Integer</li>
<li>String</li>
<li>float</li>
<li>complex</li>
</ul>
<p>Values are assigned …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/variables.jpg" title="Variables in python"><img alt="Variables in Python" src="//thegeekyway.com/uploads/images/2014/01/variables.jpg"></a></p>
</div>
<h2>Variables</h2>
<p>Variables are used for storing data in the form of integer,char ,boolean etc.They act as a storage medium for us . Based on the datatype of variable interpreter allocates the memory required by that datatype. There are 4 types of variables.</p>
<ul>
<li>Integer</li>
<li>String</li>
<li>float</li>
<li>complex</li>
</ul>
<p>Values are assigned to variables by using (=) sign..There is no need to write keyword when initializing a variable as interpreter itself asisign it according to the value given .</p>
<h2>Integer</h2>
<p>Integer is used for storing integer data . The keyword for integer variables is int .</p>
<div class="highlight"><pre><span></span><code>a=10
</code></pre></div>
<p>The range of integer is (2\^32) and get promoted to long when a number greater than (2\^32)</p>
<h2>String</h2>
<p>Strings are used for storing character literals .When we want to save some names of person or for any character related data we use strings.</p>
<div class="highlight"><pre><span></span><code>a="simer"
</code></pre></div>
<p>We can apply various operations on strings . To print letters of string we can give the index of the letter and print it .Index of a string always start from 0.</p>
<div class="highlight"><pre><span></span><code>a="simer"
print a[0]
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-55.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-55.png"></a></p>
</div>
<p>0 is the index of the 1 letter of the string ,similarly 1 idex will be of 2 letter and so on.</p>
<p>We can also give the range of letters which we want to print of the given string .These can be given as :</p>
<div class="highlight"><pre><span></span><code>a="simer"
print a[0:] # to print all the letter from index 0
print a[1:3] # to print letter starting from index 1 to index 3
print a[:4] # to print first 4 letter of string
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-6.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-6.png"></a></p>
</div>
<h2>Float</h2>
<p>float variables are used to store floating point values . They consist of two parts mantissa and exponent.</p>
<p>eg 2*10^-1 ^</p>
<p>Here 2 is the mantissa , -1 is the exponent and 10 is the base of the exponent.</p>
<div class="highlight"><pre><span></span><code>a=2.5
</code></pre></div>
<h2>Complex</h2>
<p>Complex numbers are represented by a+bj where a is the real part of number and b is the imaginary part of the number .They are used to repreent real floating point numbers.</p>
<div class="highlight"><pre><span></span><code>a=5+10i
print a
</code></pre></div>
<h2>Multiple Assignnmet</h2>
<p>We can assign multiple variables at the same time to same value. This will save time by not initializing the variable every time.</p>
<div class="highlight"><pre><span></span><code>a=b=c=20
</code></pre></div>
<h2>Conversions</h2>
<p>We can convert datatype of a variable to another datatype .</p>
<p>Integer to float or vice versa</p>
<p>We can convert a floating point number to an integer or a integer to floating point by using int and float keywords with that variable.</p>
<div class="highlight"><pre><span></span><code>a=123.5
print a
a=int(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-8.png"><img alt="Screenshot-8" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-8.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=123
print a
a=float(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"><img alt="Screenshot-7" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"></a></p>
</div>
<p>integer to string or vice versa</p>
<p>We can convert integer to sting and strings to integers by using int and str keywords .Numeric strings can be converted to integers only,alphabetic strings will not be converted.</p>
<div class="highlight"><pre><span></span><code>a="1234"
print a
a=int(a)
print a+2
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-57.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-57.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=1234
print a
a=str(a)
print a,a+2 #a+2 to check that is been converted to string
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-62.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-62.png"></a></p>
</div>
<p>Complex to string or vice versa</p>
<p>Complex can only be converted to string datatype and only numeric string can be converted to complex datat type ,keyword used for complex is complex.</p>
<div class="highlight"><pre><span></span><code>a="123"
a=complex(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-61.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-61.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=5+8j
a=str(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-56.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-56.png"></a></p>
</div>
<p>Any queries related to this tutorial can be asked and in next tutorial we will discuss about conditional statements in python.</p>Create your own Simple File Transfer App over LAN (with Source code in JAVA)2014-01-30T17:43:00+05:302014-01-30T17:43:00+05:30gagan93tag:thegeekyway.com,2014-01-30:/filetransfer/<p>Many Core Java packages can be used to create useful applications. This application uses java.net package to establish connection between two systems residing on the <strong>same network. </strong> This point is important, because it will not work for arbitrary networks (having no connection between them). For those who don't have …</p><p>Many Core Java packages can be used to create useful applications. This application uses java.net package to establish connection between two systems residing on the <strong>same network. </strong> This point is important, because it will not work for arbitrary networks (having no connection between them). For those who don't have basic networking knowledge, in the simplest language same network means computer systems connected through the same router, or directly connected to each other via <a href="http://www.pc.kg/Images/47/441657/441657.jpg" title="Lan Write">LAN Wire</a>.</p>
<p>Networking Basics : When we connect to a network (a router), we get an IP from the router, which is usually of the form xxx.xxx.x.x, where x is a no. between 0 to 255 like 192.168.1.5 or 192.168.0.5 . Usually the first address in the series is of the router itself, i.e. 192.168.1.1.<br>
In networking, there is a simple concept of a socket which is the combination of IP Address and Port number. Port no. is a is a no. between 0 to 65535. Out of this, 0 to 1023 are reserved for specific services and 1024 to 65536 can be used for our codes. In java, there are two classes which help in <strong>communication via these ports.</strong> These are namely, <strong>java.net.Socket</strong> and <strong>java.net.ServerSocket. </strong> Socket acts as a <strong>client</strong> and ServerSocket acts as a <strong>server.</strong></p>
<p>This simple software uses these two classes to send and receive files. ServerSocket opens a port on a system and waits for a client to connect to that port. On the other end, a system on the same network uses Socket class to connect to this device to establish connection and recieve data. The serveris currently able to send one file at a time to client.</p>
<p>The image shown below shows both the Client and Server side applications :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/server.jpg"><img alt="server" src="//thegeekyway.com/uploads/images/2014/01/server.jpg"></a></p>
</div>
<p>The Server Side Application has options to select the file to be sent and then press the button "Send File". For the client side, it has a text field where we enter the address specified by the server (which is 192.168.1.6 here). The default location where the client saves the file is <strong>Desktop</strong> of the user but the user can change the path where these files are saved after downloading. But, protected paths like <strong>C:</strong> and <strong>C:Program Files</strong> should be avoided as the destination paths, else an error is shown and Client shutdowns. The default location is then reset again to <strong>Desktop.<br>
</strong></p>
<p>A simple setup using <strong>Batch script, VB Script,Registry Script and Winrar</strong> was created to deploy the JAR file onto Windows Systems both 32 and 64 bit. <u><strong>JRE/ JDK 1.4 or above should be installed to use this application</strong></u>. You can try this application on your same system (local server) by using 127.0.0.1 as server address on client side.After installation on windows system, you will get these options on Right Click of each file/folder (called as context menu), which allows instant sharing as shown below.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/2.jpg" title="Screenshot of Installed Program"><img alt="Screenshot of Installed Program" src="//thegeekyway.com/uploads/images/2014/01/2.jpg"></a></p>
</div>
<p><a href="https://www.mediafire.com/download/6nscpopqnc4n57n/Smart%20File%20Transfer.exe" title="Smart file transfer download">Download the application from here</a></p>
<p style="text-decoration: underline;">**SOURCE **</p>
<p>Client side source code (Client.java) :</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">java.util.StringTokenizer</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.io.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.net.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">javax.swing.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.awt.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.awt.event.</span><span class="o">*</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Client</span> <span class="n">extends</span> <span class="n">JFrame</span> <span class="n">implements</span> <span class="n">ActionListener</span>
<span class="p">{</span>
<span class="n">JTextField</span> <span class="n">address</span><span class="p">;</span>
<span class="n">JButton</span> <span class="n">recieve</span><span class="p">;</span>
<span class="n">JLabel</span> <span class="n">status</span><span class="p">,</span><span class="n">title</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">screenX</span><span class="p">,</span><span class="n">screenY</span><span class="p">,</span><span class="n">myX</span><span class="p">,</span><span class="n">myY</span><span class="p">;</span>
<span class="n">JPanel</span> <span class="n">lowerBorder</span><span class="p">;</span>
<span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">;</span>
<span class="n">String</span> <span class="n">location</span><span class="p">;</span> <span class="o">//</span> <span class="n">represents</span> <span class="n">location</span> <span class="n">where</span> <span class="n">downloaded</span> <span class="n">file</span> <span class="ow">is</span> <span class="n">to</span> <span class="n">be</span> <span class="n">saved</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">setFont</span><span class="p">(</span><span class="n">Font</span> <span class="n">f</span><span class="p">,</span><span class="n">JComponent</span><span class="o">...</span><span class="n">c</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">for</span><span class="p">(</span><span class="n">JComponent</span> <span class="n">C</span><span class="p">:</span><span class="n">c</span><span class="p">)</span>
<span class="n">C</span><span class="o">.</span><span class="n">setFont</span><span class="p">(</span><span class="n">f</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">add</span><span class="p">(</span><span class="n">JComponent</span> <span class="n">parent</span><span class="p">,</span><span class="n">JComponent</span> <span class="n">component</span><span class="p">,</span><span class="nb">int</span> <span class="n">gridx</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridy</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridwidth</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridheight</span><span class="p">,</span><span class="nb">int</span> <span class="n">posInCell</span><span class="p">,</span><span class="n">Insets</span> <span class="n">insets</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">component</span><span class="p">,</span> <span class="n">new</span> <span class="n">GridBagConstraints</span><span class="p">(</span><span class="n">gridx</span><span class="p">,</span> <span class="n">gridy</span><span class="p">,</span><span class="n">gridwidth</span><span class="p">,</span> <span class="n">gridheight</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">posInCell</span><span class="p">,</span> <span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">NONE</span><span class="p">,</span> <span class="n">insets</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="p">}</span>
<span class="n">Client</span><span class="p">()</span>
<span class="p">{</span>
<span class="o">//</span> <span class="nb">set</span> <span class="n">Windows</span> <span class="n">Look</span> <span class="ow">and</span> <span class="n">Feel</span> <span class="k">for</span> <span class="n">Windows</span> <span class="n">Systems</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">UIManager</span><span class="o">.</span><span class="n">setLookAndFeel</span><span class="p">(</span><span class="s2">"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Using Swing Default Look and Feel, as you a using a NON-WINDOWS System"</span><span class="p">,</span><span class="s2">"Changing look and feel"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">JButton</span> <span class="n">cross</span><span class="p">,</span><span class="n">minimize</span><span class="p">;</span>
<span class="n">JPanel</span> <span class="n">upperBorder</span><span class="p">,</span><span class="n">panel</span><span class="p">,</span><span class="n">flowPanel</span><span class="p">;</span>
<span class="n">JFileChooser</span> <span class="n">jfc</span><span class="p">;</span>
<span class="n">final</span> <span class="n">JLabel</span> <span class="n">defaultLocation</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"Default location is : "</span><span class="o">+</span><span class="p">(</span><span class="n">location</span><span class="o">=</span><span class="n">defLoc</span><span class="p">()));</span>
<span class="n">final</span> <span class="n">JLabel</span> <span class="n">changeLocation</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"Change Location where downloaded file is saved "</span><span class="p">);</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JTextField</span><span class="p">(</span><span class="s2">"Enter Address as specified by Server"</span><span class="p">);</span>
<span class="n">recieve</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"Recieve File"</span><span class="p">);</span>
<span class="p">(</span><span class="n">cross</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"X"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="p">(</span><span class="n">minimize</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"-"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="p">(</span><span class="n">status</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"Enter Server Adress and press 'Recieve File' to Continue"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="p">(</span><span class="n">title</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">" Smart File Transfer (Client)"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">jfc</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JFileChooser</span><span class="p">();</span>
<span class="n">jpb</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JProgressBar</span><span class="p">();</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setStringPainted</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"Times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">BOLD</span><span class="p">,</span><span class="mi">20</span><span class="p">));</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="n">panel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">protected</span> <span class="n">void</span> <span class="n">paintComponent</span><span class="p">(</span><span class="n">Graphics</span> <span class="n">g</span><span class="p">)</span>
<span class="p">{</span>
<span class="nb">super</span><span class="o">.</span><span class="n">paintComponent</span><span class="p">(</span><span class="n">g</span><span class="p">);</span>
<span class="n">Graphics2D</span> <span class="n">g2d</span> <span class="o">=</span> <span class="p">(</span><span class="n">Graphics2D</span><span class="p">)</span> <span class="n">g</span><span class="p">;</span>
<span class="n">g2d</span><span class="o">.</span><span class="n">setRenderingHint</span><span class="p">(</span><span class="n">RenderingHints</span><span class="o">.</span><span class="n">KEY_RENDERING</span><span class="p">,</span> <span class="n">RenderingHints</span><span class="o">.</span><span class="n">VALUE_RENDER_QUALITY</span><span class="p">);</span>
<span class="n">g2d</span><span class="o">.</span><span class="n">setPaint</span><span class="p">(</span><span class="n">new</span> <span class="n">GradientPaint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="n">getHeight</span><span class="p">(),</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">)));</span>
<span class="n">g2d</span><span class="o">.</span><span class="n">fillRect</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">getWidth</span><span class="p">(),</span> <span class="n">getHeight</span><span class="p">());</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="p">(</span><span class="n">upperBorder</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">lowerBorder</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">flowPanel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"Times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">PLAIN</span><span class="p">,</span><span class="mi">20</span><span class="p">),</span><span class="n">status</span><span class="p">,</span><span class="n">recieve</span><span class="p">,</span><span class="n">title</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"tahoma"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">BOLD</span><span class="p">,</span><span class="mi">20</span><span class="p">),</span><span class="n">cross</span><span class="p">,</span><span class="n">minimize</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"Times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">PLAIN</span><span class="p">,</span><span class="mi">26</span><span class="p">),</span><span class="n">address</span><span class="p">);</span>
<span class="n">panel</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">GridBagLayout</span><span class="p">());</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">BorderLayout</span><span class="p">());</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">FlowLayout</span><span class="p">(</span><span class="n">FlowLayout</span><span class="o">.</span><span class="n">CENTER</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">address</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">CENTER</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">recieve</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">CENTER</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">defaultLocation</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">changeLocation</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">flowPanel</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">minimize</span><span class="p">);</span>
<span class="n">flowPanel</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">cross</span><span class="p">);</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">flowPanel</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">EAST</span><span class="p">);</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">title</span><span class="p">);</span> <span class="o">//</span> <span class="ow">not</span> <span class="n">centered</span><span class="p">,</span> <span class="n">towards</span> <span class="n">left</span><span class="o">.</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">upperBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">NORTH</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">final</span> <span class="n">JTextField</span> <span class="n">temp</span><span class="o">=</span><span class="n">address</span><span class="p">;</span>
<span class="n">addMouseListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mousePressed</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">screenX</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getXOnScreen</span><span class="p">();</span>
<span class="n">screenY</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getYOnScreen</span><span class="p">();</span>
<span class="n">myX</span> <span class="o">=</span> <span class="n">getX</span><span class="p">();</span>
<span class="n">myY</span> <span class="o">=</span> <span class="n">getY</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">addMouseMotionListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseMotionAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseDragged</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="nb">int</span> <span class="n">deltaX</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getXOnScreen</span><span class="p">()</span> <span class="o">-</span> <span class="n">screenX</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">deltaY</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getYOnScreen</span><span class="p">()</span> <span class="o">-</span> <span class="n">screenY</span><span class="p">;</span>
<span class="n">setLocation</span><span class="p">(</span><span class="n">myX</span> <span class="o">+</span> <span class="n">deltaX</span> <span class="p">,</span> <span class="n">myY</span> <span class="o">+</span> <span class="n">deltaY</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">address</span><span class="o">.</span><span class="n">addMouseListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseClicked</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">temp</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">temp</span><span class="o">.</span><span class="n">removeMouseListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">changeLocation</span><span class="o">.</span><span class="n">addMouseListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseEntered</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">changeLocation</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseExited</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">changeLocation</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseClicked</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JFileChooser</span> <span class="n">jfc</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JFileChooser</span><span class="p">();</span>
<span class="n">jfc</span><span class="o">.</span><span class="n">setFileSelectionMode</span><span class="p">(</span><span class="n">JFileChooser</span><span class="o">.</span><span class="n">DIRECTORIES_ONLY</span><span class="p">);</span>
<span class="n">jfc</span><span class="o">.</span><span class="n">setCurrentDirectory</span><span class="p">(</span><span class="n">new</span> <span class="n">File</span><span class="p">(</span><span class="n">location</span><span class="p">));</span>
<span class="nb">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">jfc</span><span class="o">.</span><span class="n">showSaveDialog</span><span class="p">(</span><span class="n">null</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="n">ret</span> <span class="o">==</span> <span class="n">JFileChooser</span><span class="o">.</span><span class="n">APPROVE_OPTION</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">FileWriter</span> <span class="n">fw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FileWriter</span><span class="p">(</span><span class="s2">"c:\sft\log.bin"</span><span class="p">);</span>
<span class="n">fw</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">jfc</span><span class="o">.</span><span class="n">getSelectedFile</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">());</span>
<span class="n">fw</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">defaultLocation</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Default Location is : "</span><span class="o">+</span><span class="p">(</span><span class="n">location</span><span class="o">=</span><span class="n">jfc</span><span class="o">.</span><span class="n">getSelectedFile</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">()));</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Error in changing path, The application will now exit"</span><span class="p">,</span><span class="s2">"Fatal Error"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">cross</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">minimize</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">setResizable</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="n">setUndecorated</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">setSize</span><span class="p">(</span><span class="mi">600</span><span class="p">,</span><span class="mi">300</span><span class="p">);</span>
<span class="n">getRootPane</span><span class="p">()</span><span class="o">.</span><span class="n">setBorder</span><span class="p">(</span><span class="n">BorderFactory</span><span class="o">.</span><span class="n">createLineBorder</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">),</span><span class="mi">5</span><span class="p">));</span>
<span class="n">getRootPane</span><span class="p">()</span><span class="o">.</span><span class="n">setDefaultButton</span><span class="p">(</span><span class="n">recieve</span><span class="p">);</span>
<span class="n">setLocationRelativeTo</span><span class="p">(</span><span class="n">null</span><span class="p">);</span>
<span class="n">setDefaultCloseOperation</span><span class="p">(</span><span class="n">DISPOSE_ON_CLOSE</span><span class="p">);</span>
<span class="n">setVisible</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">requestFocus</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">String</span> <span class="n">defLoc</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">String</span> <span class="n">ret</span><span class="o">=</span><span class="s2">""</span><span class="p">;</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">BufferedReader</span> <span class="n">br</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedReader</span><span class="p">(</span><span class="n">new</span> <span class="n">FileReader</span><span class="p">(</span><span class="s2">"c:\sft\log.bin"</span><span class="p">));</span>
<span class="n">String</span> <span class="n">s</span> <span class="o">=</span> <span class="n">br</span><span class="o">.</span><span class="n">readLine</span><span class="p">();</span>
<span class="n">ret</span><span class="o">=</span><span class="n">s</span><span class="p">;</span>
<span class="n">File</span> <span class="n">f</span> <span class="o">=</span> <span class="n">new</span> <span class="n">File</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="err">!</span><span class="n">f</span><span class="o">.</span><span class="n">isDirectory</span><span class="p">())</span>
<span class="n">new</span> <span class="n">FileReader</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="n">e</span> <span class="n">instanceof</span> <span class="n">FileNotFoundException</span> <span class="o">||</span> <span class="n">e</span> <span class="n">instanceof</span> <span class="n">NullPointerException</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">FileWriter</span> <span class="n">fw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FileWriter</span> <span class="p">(</span><span class="s2">"c:\sft\log.bin"</span><span class="p">);</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">System</span><span class="o">.</span><span class="n">getProperty</span><span class="p">(</span><span class="s2">"user.home"</span><span class="p">)</span><span class="o">+</span><span class="s2">"\Desktop</span><span class="se">\"</span><span class="s2">;</span>
<span class="n">fw</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">ret</span><span class="p">);</span>
<span class="n">fw</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">ee</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Critical Error, The application will now exit"</span><span class="p">,</span><span class="s2">"Critical Error"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">actionPerformed</span><span class="p">(</span><span class="n">ActionEvent</span> <span class="n">ae</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">String</span> <span class="n">com</span><span class="o">=</span><span class="n">ae</span><span class="o">.</span><span class="n">getActionCommand</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="n">com</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"-"</span><span class="p">))</span>
<span class="n">setState</span><span class="p">(</span><span class="n">JFrame</span><span class="o">.</span><span class="n">ICONIFIED</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">com</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"X"</span><span class="p">))</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">com</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"Recieve File"</span><span class="p">))</span>
<span class="p">{</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="nb">int</span> <span class="n">add</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="nb">int</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>
<span class="nb">int</span> <span class="n">counter</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="n">StringTokenizer</span> <span class="n">st</span> <span class="o">=</span> <span class="n">new</span> <span class="n">StringTokenizer</span><span class="p">(</span><span class="n">address</span><span class="o">.</span><span class="n">getText</span><span class="p">(),</span><span class="s2">"."</span><span class="p">);</span>
<span class="k">while</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">hasMoreTokens</span><span class="p">())</span>
<span class="p">{</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="n">counter</span><span class="o">>=</span><span class="mi">4</span><span class="p">)</span>
<span class="n">new</span> <span class="n">Integer</span><span class="p">(</span><span class="s2">"g"</span><span class="p">);</span>
<span class="n">add</span><span class="p">[</span><span class="n">counter</span><span class="o">++</span><span class="p">]</span><span class="o">=</span><span class="n">new</span> <span class="n">Integer</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">nextToken</span><span class="p">());</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="n">NumberFormatException</span> <span class="n">nfe</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">address</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"IP address has only no.s seperated by dots (.)nIt is of the form "</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="s2">" nwhere xxx is a no. less than 256"</span><span class="p">,</span><span class="s2">"Integer Data Expected"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span> <span class="err">!</span><span class="p">(</span><span class="n">counter</span> <span class="o">==</span> <span class="mi">4</span><span class="p">)</span> <span class="o">||</span> <span class="n">add</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">></span> <span class="mi">255</span> <span class="o">||</span> <span class="n">add</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">></span> <span class="mi">255</span> <span class="o">||</span> <span class="n">add</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">></span> <span class="mi">255</span> <span class="o">||</span> <span class="n">add</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">></span> <span class="mi">255</span> <span class="p">)</span>
<span class="p">{</span>
<span class="n">address</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Incorrect IP Address nIt is of the form "</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="o">.</span><span class="n">xxx</span><span class="s2">" nwhere xxx is a no. less than 256"</span><span class="p">,</span><span class="s2">"Invalid IP"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">new</span> <span class="n">Thread</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">recieveFile</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">})</span><span class="o">.</span><span class="n">start</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">recieveFile</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Searching for server...Please wait"</span><span class="p">);</span>
<span class="n">address</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">byte</span> <span class="n">b</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="n">byte</span><span class="p">[</span><span class="mi">100000</span><span class="p">];</span>
<span class="o">//</span> <span class="n">array</span> <span class="n">to</span> <span class="n">retrieve</span> <span class="n">data</span> <span class="kn">from</span> <span class="nn">server</span> <span class="ow">and</span> <span class="n">send</span> <span class="n">to</span> <span class="n">client</span>
<span class="n">String</span> <span class="n">sizeName</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="n">String</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
<span class="o">//</span> <span class="n">stores</span> <span class="n">size</span> <span class="ow">and</span> <span class="n">name</span> <span class="n">of</span> <span class="n">file</span> <span class="k">as</span> <span class="n">recieved</span> <span class="kn">from</span> <span class="nn">character</span> <span class="n">streams</span>
<span class="n">double</span> <span class="n">done</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">length</span><span class="p">;</span>
<span class="o">//</span> <span class="n">done</span> <span class="ow">is</span> <span class="n">used</span> <span class="n">to</span> <span class="n">count</span> <span class="n">the</span> <span class="n">percentage</span>
<span class="nb">int</span> <span class="n">read</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="o">//</span> <span class="n">read</span> <span class="n">counts</span> <span class="n">the</span> <span class="nb">bytes</span> <span class="n">read</span> <span class="p">(</span><span class="n">within</span> <span class="mi">4</span> <span class="nb">bytes</span> <span class="n">integer</span> <span class="nb">range</span><span class="p">)</span> <span class="ow">in</span> <span class="n">WHILE</span> <span class="n">loop</span>
<span class="o">//</span> <span class="n">constructing</span> <span class="n">streams</span>
<span class="n">BufferedReader</span> <span class="n">br</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">read</span> <span class="n">String</span> <span class="ow">and</span> <span class="n">long</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">PrintWriter</span> <span class="n">pw</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">write</span> <span class="n">String</span> <span class="ow">and</span> <span class="n">long</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">BufferedInputStream</span> <span class="n">bis</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span><span class="n">to</span> <span class="n">write</span> <span class="n">file</span> <span class="n">contents</span> <span class="p">(</span><span class="n">byte</span> <span class="n">stream</span><span class="p">)</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">BufferedOutputStream</span> <span class="n">bos</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span><span class="n">to</span> <span class="n">read</span> <span class="n">byte</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">FileOutputStream</span> <span class="n">fos</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">read</span> <span class="n">actual</span> <span class="n">file</span> <span class="n">using</span> <span class="n">byte</span> <span class="n">stream</span>
<span class="n">Socket</span> <span class="n">s</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">this</span> <span class="n">will</span> <span class="n">serve</span> <span class="n">a</span> <span class="n">local</span> <span class="n">port</span> <span class="k">for</span> <span class="n">a</span> <span class="n">client</span>
<span class="o">//</span> <span class="n">now</span> <span class="n">allocating</span> <span class="n">memory</span> <span class="n">to</span> <span class="n">objects</span> <span class="ow">and</span> <span class="n">starting</span> <span class="n">main</span> <span class="n">logic</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Socket</span><span class="p">(</span><span class="n">address</span><span class="o">.</span><span class="n">getText</span><span class="p">(),</span><span class="mi">4000</span><span class="p">);</span>
<span class="n">br</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedReader</span><span class="p">(</span><span class="n">new</span> <span class="n">InputStreamReader</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getInputStream</span><span class="p">()));</span>
<span class="n">pw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">PrintWriter</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getOutputStream</span><span class="p">());</span>
<span class="n">StringTokenizer</span> <span class="n">st</span> <span class="o">=</span> <span class="n">new</span> <span class="n">StringTokenizer</span><span class="p">(</span><span class="n">br</span><span class="o">.</span><span class="n">readLine</span><span class="p">(),</span><span class="s2">"/"</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Connection Established, about to begin download"</span><span class="p">);</span>
<span class="k">while</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">hasMoreTokens</span><span class="p">())</span>
<span class="n">sizeName</span><span class="p">[</span><span class="n">i</span><span class="o">++</span><span class="p">]</span><span class="o">=</span><span class="n">st</span><span class="o">.</span><span class="n">nextToken</span><span class="p">();</span>
<span class="n">pw</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="s2">"Recieved"</span><span class="p">);</span>
<span class="n">pw</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="n">length</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">sizeName</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<span class="n">bis</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedInputStream</span> <span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getInputStream</span><span class="p">());</span>
<span class="n">bos</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedOutputStream</span> <span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getOutputStream</span><span class="p">());</span>
<span class="n">fos</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FileOutputStream</span> <span class="p">(</span><span class="n">location</span><span class="o">+</span><span class="s2">"</span><span class="se">\"</span><span class="s2">+sizeName[0]);</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">remove</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">jpb</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">repaint</span><span class="p">();</span>
<span class="n">revalidate</span><span class="p">();</span>
<span class="k">while</span><span class="p">(</span><span class="n">true</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">done</span><span class="o">+=</span><span class="n">read</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="n">done</span><span class="o">>=</span><span class="n">length</span><span class="p">)</span>
<span class="k">break</span><span class="p">;</span>
<span class="n">read</span><span class="o">=</span><span class="n">bis</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">b</span><span class="p">);</span>
<span class="n">ClientSwingWorker</span> <span class="n">csw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ClientSwingWorker</span> <span class="p">(</span><span class="n">done</span><span class="p">,</span><span class="n">length</span><span class="p">,</span><span class="n">read</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">fos</span><span class="p">,</span><span class="n">jpb</span><span class="p">);</span>
<span class="n">csw</span><span class="o">.</span><span class="n">execute</span><span class="p">();</span>
<span class="k">while</span><span class="p">(</span><span class="err">!</span><span class="p">(</span><span class="n">csw</span><span class="o">.</span><span class="n">isDone</span><span class="p">()))</span> <span class="p">{}</span>
<span class="p">}</span>
<span class="n">fos</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="n">address</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">remove</span><span class="p">(</span><span class="n">jpb</span><span class="p">);</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Recieved 100%"</span><span class="p">);</span>
<span class="n">repaint</span><span class="p">();</span>
<span class="n">revalidate</span><span class="p">();</span>
<span class="n">double</span> <span class="n">time</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">br</span><span class="o">.</span><span class="n">readLine</span><span class="p">());</span>
<span class="n">String</span> <span class="n">speedString</span> <span class="o">=</span> <span class="n">br</span><span class="o">.</span><span class="n">readLine</span><span class="p">();</span>
<span class="n">bis</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">bos</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">fos</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">pw</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">br</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">s</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Time taken is "</span><span class="o">+</span><span class="n">time</span><span class="o">+</span><span class="s2">"nSpeed is "</span><span class="o">+</span><span class="n">speedString</span><span class="o">+</span><span class="s2">" MBPS"</span><span class="p">,</span><span class="s2">"File Sent (Client)"</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Enter Server Adress and press 'Recieve File' to Continue"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span> <span class="n">e</span> <span class="n">instanceof</span> <span class="n">ConnectException</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">address</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Enter Address as specified by Server"</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span> <span class="p">(</span><span class="s2">"Enter Server Adress and press 'Connect and Start' button to Continue"</span><span class="p">);</span>
<span class="n">address</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">recieve</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"No Running Server found on specified address [ "</span><span class="o">+</span><span class="n">address</span><span class="o">.</span><span class="n">getText</span><span class="p">()</span><span class="o">+</span><span class="s2">" ]"</span><span class="p">,</span><span class="s2">"Server Not Found"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="n">final</span> <span class="n">JTextField</span> <span class="n">temp</span><span class="o">=</span><span class="n">address</span><span class="p">;</span>
<span class="n">address</span><span class="o">.</span><span class="n">addMouseListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseClicked</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">temp</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">temp</span><span class="o">.</span><span class="n">removeMouseListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">e</span> <span class="n">instanceof</span> <span class="n">FileNotFoundException</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Failed in saving file,nLocation : "</span><span class="o">+</span><span class="n">location</span><span class="o">+</span><span class="s2">" required administrative rights to save or it was an invalid pathnSelect some other location for downloaded filesnThe Program will now Exit and default location would be reset"</span><span class="p">,</span><span class="s2">"Error ["</span><span class="o">+</span><span class="n">location</span><span class="o">+</span><span class="s2">"]"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">FileWriter</span> <span class="n">fw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FileWriter</span><span class="p">(</span><span class="s2">"c:\sft\log.bin"</span><span class="p">);</span>
<span class="n">fw</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="n">IOException</span> <span class="n">ee</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">}</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">e</span><span class="o">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">...</span><span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">new</span> <span class="n">Client</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">ClientSwingWorker</span> <span class="n">extends</span> <span class="n">SwingWorker</span><span class="o"><</span><span class="n">void</span><span class="p">,</span><span class="n">void</span><span class="o">></span>
<span class="p">{</span>
<span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">;</span>
<span class="n">final</span> <span class="n">double</span> <span class="n">done</span><span class="p">,</span><span class="n">size</span><span class="p">;</span>
<span class="n">byte</span> <span class="n">b</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="n">byte</span><span class="p">[</span><span class="mi">100000</span><span class="p">];</span>
<span class="n">final</span> <span class="nb">int</span> <span class="n">read</span><span class="p">;</span>
<span class="n">FileOutputStream</span> <span class="n">fos</span><span class="p">;</span>
<span class="n">ClientSwingWorker</span><span class="p">(</span><span class="n">double</span> <span class="n">done</span><span class="p">,</span><span class="n">double</span> <span class="n">size</span><span class="p">,</span><span class="nb">int</span> <span class="n">read</span><span class="p">,</span><span class="n">byte</span> <span class="n">b</span><span class="p">[],</span><span class="n">FileOutputStream</span> <span class="n">fos</span><span class="p">,</span><span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">this</span><span class="o">.</span><span class="n">done</span> <span class="o">=</span> <span class="n">done</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="n">size</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">read</span> <span class="o">=</span> <span class="n">read</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">jpb</span> <span class="o">=</span> <span class="n">jpb</span> <span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">fos</span> <span class="o">=</span> <span class="n">fos</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">protected</span> <span class="n">Void</span> <span class="n">doInBackground</span><span class="p">()</span> <span class="n">throws</span> <span class="ne">Exception</span>
<span class="p">{</span>
<span class="n">fos</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">read</span><span class="p">);</span>
<span class="k">return</span> <span class="n">null</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">protected</span> <span class="n">void</span> <span class="n">done</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">final</span> <span class="n">double</span> <span class="n">temp</span><span class="o">=</span><span class="p">(</span><span class="n">done</span><span class="o">/</span><span class="n">size</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">;</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">String</span> <span class="n">tString</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span><span class="o">.</span><span class="n">toString</span><span class="p">();</span>
<span class="nb">int</span> <span class="n">index</span><span class="o">=</span><span class="n">tString</span><span class="o">.</span><span class="n">indexOf</span><span class="p">(</span><span class="s2">"."</span><span class="p">);</span>
<span class="nb">int</span> <span class="n">breakPoint</span><span class="o">=</span> <span class="p">(</span><span class="n">index</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span><span class="o">></span><span class="n">tString</span><span class="o">.</span><span class="n">length</span><span class="p">()</span><span class="err">?</span><span class="n">tString</span><span class="o">.</span><span class="n">length</span><span class="p">():(</span><span class="n">index</span><span class="o">+</span><span class="mi">3</span><span class="p">);</span>
<span class="n">tString</span><span class="o">=</span><span class="n">tString</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">breakPoint</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setString</span><span class="p">(</span><span class="s2">"Recieving : "</span><span class="o">+</span><span class="n">tString</span><span class="o">+</span><span class="s2">" %"</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setValue</span><span class="p">((</span><span class="nb">int</span><span class="p">)</span><span class="n">temp</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Server Side code (Server.java):</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">java.io.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.net.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">javax.swing.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.awt.</span><span class="o">*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.awt.event.</span><span class="o">*</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Server</span> <span class="n">extends</span> <span class="n">JFrame</span> <span class="n">implements</span> <span class="n">ActionListener</span>
<span class="p">{</span>
<span class="n">JTextField</span> <span class="n">nameField</span><span class="p">,</span><span class="n">sizeField</span><span class="p">,</span><span class="n">typeField</span><span class="p">;</span>
<span class="o">//</span> <span class="n">textfields</span> <span class="ow">in</span> <span class="n">front</span> <span class="n">Dialog</span>
<span class="n">JButton</span> <span class="n">browse</span><span class="p">,</span><span class="n">start</span><span class="p">;</span>
<span class="o">//</span> <span class="n">browse</span> <span class="ow">is</span> <span class="n">to</span> <span class="n">select</span> <span class="n">the</span> <span class="n">file</span> <span class="ow">and</span> <span class="n">start</span> <span class="ow">is</span> <span class="n">to</span> <span class="n">start</span> <span class="n">the</span> <span class="n">server</span>
<span class="n">JFileChooser</span> <span class="n">jfc</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">select</span> <span class="n">the</span> <span class="n">file</span> <span class="kn">from</span> <span class="nn">local</span> <span class="n">computer</span>
<span class="n">JLabel</span> <span class="n">status</span><span class="p">,</span><span class="n">address</span><span class="p">;</span>
<span class="o">//</span> <span class="n">shows</span> <span class="n">current</span> <span class="n">status</span> <span class="n">of</span> <span class="n">the</span> <span class="n">app</span>
<span class="n">JPanel</span> <span class="n">lowerBorder</span><span class="p">;</span>
<span class="o">//</span> <span class="ow">is</span> <span class="n">removed</span> <span class="n">afterwards</span><span class="p">,</span> <span class="n">so</span> <span class="n">it</span> <span class="ow">is</span> <span class="n">declared</span> <span class="n">class</span><span class="o">-</span><span class="n">wide</span>
<span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">;</span>
<span class="o">//</span> <span class="n">progress</span> <span class="n">bar</span> <span class="n">to</span> <span class="n">show</span> <span class="n">file</span> <span class="n">completion</span>
<span class="n">final</span> <span class="n">String</span> <span class="n">sizeArray</span><span class="p">[]</span><span class="o">=</span><span class="p">{</span><span class="s2">"B"</span><span class="p">,</span><span class="s2">"KB"</span><span class="p">,</span><span class="s2">"MB"</span><span class="p">,</span><span class="s2">"GB"</span><span class="p">,</span><span class="s2">"TB"</span><span class="p">};</span>
<span class="o">//</span> <span class="n">final</span> <span class="n">string</span> <span class="n">array</span> <span class="n">to</span> <span class="nb">set</span> <span class="n">the</span> <span class="n">size</span> <span class="n">of</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">desired</span> <span class="nb">range</span>
<span class="nb">int</span> <span class="n">screenX</span><span class="p">,</span><span class="n">screenY</span><span class="p">,</span><span class="n">myX</span><span class="p">,</span><span class="n">myY</span><span class="p">;</span>
<span class="o">//</span> <span class="n">dialog</span> <span class="ow">is</span> <span class="n">undecorated</span><span class="p">,</span> <span class="n">so</span> <span class="n">these</span> <span class="n">coordinates</span> <span class="n">help</span> <span class="n">to</span> <span class="n">move</span> <span class="n">the</span> <span class="n">frame</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">screen</span>
<span class="n">boolean</span> <span class="n">startFlag</span><span class="o">=</span><span class="n">false</span><span class="p">;</span>
<span class="o">//</span> <span class="n">this</span> <span class="n">flag</span> <span class="n">sets</span> <span class="n">the</span> <span class="n">StartServer</span> <span class="n">button</span> <span class="s2">"working"</span>
<span class="n">double</span> <span class="n">fileSize</span><span class="p">;</span>
<span class="o">//</span> <span class="n">stores</span> <span class="n">the</span> <span class="n">size</span> <span class="n">of</span> <span class="n">file</span> <span class="ow">in</span> <span class="nb">bytes</span>
<span class="n">String</span> <span class="n">serverAddress</span><span class="o">=</span><span class="s2">""</span><span class="p">,</span><span class="n">tempAddress</span><span class="o">=</span><span class="s2">""</span><span class="p">;</span>
<span class="o">//</span> <span class="n">string</span> <span class="n">stores</span> <span class="n">the</span> <span class="n">address</span> <span class="n">of</span> <span class="n">the</span> <span class="n">server</span><span class="p">,</span> <span class="n">determined</span> <span class="n">inside</span> <span class="n">constructor</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">add</span><span class="p">(</span><span class="n">JComponent</span> <span class="n">parent</span><span class="p">,</span><span class="n">JComponent</span> <span class="n">component</span><span class="p">,</span><span class="nb">int</span> <span class="n">gridx</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridy</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridwidth</span><span class="p">,</span> <span class="nb">int</span> <span class="n">gridheight</span><span class="p">,</span><span class="nb">int</span> <span class="n">posInCell</span><span class="p">,</span><span class="n">Insets</span> <span class="n">insets</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">component</span><span class="p">,</span> <span class="n">new</span> <span class="n">GridBagConstraints</span><span class="p">(</span><span class="n">gridx</span><span class="p">,</span> <span class="n">gridy</span><span class="p">,</span><span class="n">gridwidth</span><span class="p">,</span> <span class="n">gridheight</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">posInCell</span><span class="p">,</span> <span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">NONE</span><span class="p">,</span> <span class="n">insets</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="p">}</span>
<span class="o">//</span> <span class="n">GridBagLayout</span> <span class="n">alignment</span> <span class="ow">and</span> <span class="n">add</span> <span class="n">function</span>
<span class="n">void</span> <span class="n">retrieveServerIP</span><span class="p">(</span><span class="n">boolean</span> <span class="n">flag</span><span class="p">)</span> <span class="o">//</span> <span class="n">flag</span> <span class="ow">is</span> <span class="n">false</span> <span class="n">when</span> <span class="n">it</span> <span class="ow">is</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">first</span> <span class="n">time</span><span class="p">,</span> <span class="k">else</span> <span class="n">label</span> <span class="ow">is</span> <span class="n">updated</span>
<span class="p">{</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">tempAddress</span> <span class="o">=</span> <span class="n">InetAddress</span><span class="o">.</span><span class="n">getLocalHost</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="n">InetAddress</span><span class="o">.</span><span class="n">getLocalHost</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span><span class="o">.</span><span class="n">lastIndexOf</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">this</span><span class="p">,</span><span class="s2">"Unable to Retrieve System IP, Restart the Application and Retry"</span><span class="p">,</span><span class="s2">"Runtime Error !"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span> <span class="o">//</span> <span class="n">EIWQ</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="err">!</span><span class="p">(</span><span class="n">tempAddress</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">serverAddress</span><span class="p">)))</span>
<span class="p">{</span>
<span class="n">serverAddress</span><span class="o">=</span><span class="n">tempAddress</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="n">flag</span><span class="p">)</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">address</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Type this in client app : "</span><span class="o">+</span><span class="n">serverAddress</span><span class="o">+</span><span class="s2">" "</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="o">//</span> <span class="n">starting</span> <span class="n">constructor</span> <span class="k">for</span> <span class="n">basic</span> <span class="n">initializations</span>
<span class="n">Server</span><span class="p">(</span><span class="n">String</span> <span class="n">path</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">this</span><span class="p">();</span>
<span class="n">openFile</span><span class="p">(</span><span class="n">new</span> <span class="n">File</span><span class="p">(</span><span class="n">path</span><span class="p">));</span>
<span class="p">}</span>
<span class="n">Server</span><span class="p">()</span>
<span class="p">{</span>
<span class="o">//</span> <span class="n">Retrieve</span> <span class="n">Server</span><span class="s1">'s IP</span>
<span class="n">retrieveServerIP</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="o">//</span> <span class="nb">set</span> <span class="n">Windows</span> <span class="n">Look</span> <span class="ow">and</span> <span class="n">Feel</span> <span class="k">for</span> <span class="n">Windows</span> <span class="n">Systems</span> <span class="p">(</span><span class="n">only</span> <span class="n">on</span> <span class="n">file</span> <span class="n">chooser</span><span class="p">)</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">UIManager</span><span class="o">.</span><span class="n">setLookAndFeel</span><span class="p">(</span><span class="s2">"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"</span><span class="p">);</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">updateComponentTreeUI</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="ne">Exception</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Using Swing Default Look and Feel, as you a using a NON-WINDOWS System"</span><span class="p">,</span><span class="s2">"Changing look and feel"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="o">//</span> <span class="n">Constructos</span> <span class="n">Declarations</span>
<span class="n">JButton</span> <span class="n">cross</span><span class="p">,</span><span class="n">minimize</span><span class="p">;</span>
<span class="n">JLabel</span> <span class="n">nameLabel</span><span class="p">,</span><span class="n">sizeLabel</span><span class="p">,</span><span class="n">typeLabel</span><span class="p">,</span><span class="n">title</span><span class="p">;</span>
<span class="n">JPanel</span> <span class="n">panel</span><span class="p">,</span><span class="n">upperBorder</span><span class="p">,</span><span class="n">flowPanel</span><span class="p">;</span>
<span class="o">//</span> <span class="n">Memory</span> <span class="n">allocation</span>
<span class="p">(</span><span class="n">browse</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"Browse"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">start</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"Send File"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">cross</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"X"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="p">(</span><span class="n">minimize</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JButton</span><span class="p">(</span><span class="s2">"-"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="n">cross</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">minimize</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">jfc</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JFileChooser</span><span class="p">();</span>
<span class="n">jpb</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JProgressBar</span><span class="p">();</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setStringPainted</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"Times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">BOLD</span><span class="p">,</span><span class="mi">20</span><span class="p">));</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="n">nameLabel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"File Name"</span><span class="p">);</span>
<span class="n">sizeLabel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"File Size"</span><span class="p">);</span>
<span class="n">typeLabel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"File Type"</span><span class="p">);</span>
<span class="p">(</span><span class="n">title</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">" Smart File Transfer (Server)"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"Type this in client app : "</span><span class="o">+</span><span class="n">serverAddress</span><span class="o">+</span><span class="s2">" "</span><span class="p">);</span>
<span class="p">(</span><span class="n">status</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s2">"Select File and Start Server"</span><span class="p">))</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">panel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">();</span>
<span class="n">panel</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">));</span>
<span class="p">(</span><span class="n">upperBorder</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">lowerBorder</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);</span>
<span class="p">(</span><span class="n">flowPanel</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JPanel</span><span class="p">())</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">BLACK</span><span class="p">);;</span>
<span class="p">(</span><span class="n">nameField</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JTextField</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span><span class="o">.</span><span class="n">setEditable</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="p">(</span><span class="n">sizeField</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JTextField</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span><span class="o">.</span><span class="n">setEditable</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="p">(</span><span class="n">typeField</span> <span class="o">=</span> <span class="n">new</span> <span class="n">JTextField</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span><span class="o">.</span><span class="n">setEditable</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="n">nameField</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">sizeField</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="n">typeField</span><span class="o">.</span><span class="n">setBackground</span><span class="p">(</span><span class="n">Color</span><span class="o">.</span><span class="n">WHITE</span><span class="p">);</span>
<span class="o">//</span> <span class="n">setting</span> <span class="n">Panel</span> <span class="ow">and</span> <span class="n">adding</span> <span class="n">components</span> <span class="n">to</span> <span class="n">Panel</span>
<span class="n">panel</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">GridBagLayout</span><span class="p">());</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">BorderLayout</span><span class="p">());</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">setLayout</span><span class="p">(</span><span class="n">new</span> <span class="n">FlowLayout</span><span class="p">(</span><span class="n">FlowLayout</span><span class="o">.</span><span class="n">CENTER</span><span class="p">));</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">nameLabel</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">nameField</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">browse</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">CENTER</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">sizeLabel</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">sizeField</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">typeLabel</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">typeField</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">start</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">CENTER</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">address</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">GridBagConstraints</span><span class="o">.</span><span class="n">WEST</span><span class="p">,</span><span class="n">new</span> <span class="n">Insets</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">));</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"GreyscaleBasic"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">BOLD</span><span class="p">,</span><span class="mi">20</span><span class="p">),</span><span class="n">nameLabel</span><span class="p">,</span><span class="n">sizeLabel</span><span class="p">,</span><span class="n">typeLabel</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"Times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">PLAIN</span><span class="p">,</span><span class="mi">14</span><span class="p">),</span><span class="n">nameField</span><span class="p">,</span><span class="n">sizeField</span><span class="p">,</span><span class="n">typeField</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"tahoma"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">BOLD</span><span class="p">,</span><span class="mi">20</span><span class="p">),</span><span class="n">cross</span><span class="p">,</span><span class="n">address</span><span class="p">,</span><span class="n">minimize</span><span class="p">);</span>
<span class="n">setFont</span><span class="p">(</span><span class="n">new</span> <span class="n">Font</span><span class="p">(</span><span class="s2">"times new roman"</span><span class="p">,</span><span class="n">Font</span><span class="o">.</span><span class="n">PLAIN</span><span class="p">,</span><span class="mi">20</span><span class="p">),</span><span class="n">start</span><span class="p">,</span><span class="n">browse</span><span class="p">,</span><span class="n">status</span><span class="p">,</span><span class="n">title</span><span class="p">);</span>
<span class="o">//</span> <span class="n">add</span> <span class="n">Listeners</span>
<span class="n">browse</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">start</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">cross</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="n">minimize</span><span class="o">.</span><span class="n">addActionListener</span><span class="p">(</span><span class="n">this</span><span class="p">);</span>
<span class="o">//</span> <span class="nb">set</span> <span class="n">Top</span> <span class="n">Level</span> <span class="n">Container</span> <span class="n">Properties</span>
<span class="n">flowPanel</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">minimize</span><span class="p">);</span>
<span class="n">flowPanel</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">cross</span><span class="p">);</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">flowPanel</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">EAST</span><span class="p">);</span>
<span class="n">upperBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">title</span><span class="p">);</span> <span class="o">//</span><span class="ow">not</span> <span class="n">centered</span><span class="p">,</span> <span class="n">towards</span> <span class="n">left</span>
<span class="n">add</span><span class="p">(</span><span class="n">upperBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">NORTH</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">CENTER</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">addMouseListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mousePressed</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">screenX</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getXOnScreen</span><span class="p">();</span>
<span class="n">screenY</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getYOnScreen</span><span class="p">();</span>
<span class="n">myX</span> <span class="o">=</span> <span class="n">getX</span><span class="p">();</span>
<span class="n">myY</span> <span class="o">=</span> <span class="n">getY</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">addMouseMotionListener</span><span class="p">(</span><span class="n">new</span> <span class="n">MouseMotionAdapter</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">mouseDragged</span><span class="p">(</span><span class="n">MouseEvent</span> <span class="n">me</span><span class="p">)</span>
<span class="p">{</span>
<span class="nb">int</span> <span class="n">deltaX</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getXOnScreen</span><span class="p">()</span> <span class="o">-</span> <span class="n">screenX</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">deltaY</span> <span class="o">=</span> <span class="n">me</span><span class="o">.</span><span class="n">getYOnScreen</span><span class="p">()</span> <span class="o">-</span> <span class="n">screenY</span><span class="p">;</span>
<span class="n">setLocation</span><span class="p">(</span><span class="n">myX</span> <span class="o">+</span> <span class="n">deltaX</span> <span class="p">,</span> <span class="n">myY</span> <span class="o">+</span> <span class="n">deltaY</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="n">setSize</span><span class="p">(</span><span class="mi">800</span><span class="p">,</span><span class="mi">400</span><span class="p">);</span>
<span class="n">setResizable</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="n">setLocationRelativeTo</span><span class="p">(</span><span class="n">null</span><span class="p">);</span>
<span class="n">setUndecorated</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">setDefaultCloseOperation</span><span class="p">(</span><span class="n">DISPOSE_ON_CLOSE</span><span class="p">);</span>
<span class="n">getRootPane</span><span class="p">()</span><span class="o">.</span><span class="n">setBorder</span><span class="p">(</span><span class="n">BorderFactory</span><span class="o">.</span><span class="n">createLineBorder</span><span class="p">(</span><span class="n">new</span> <span class="n">Color</span><span class="p">(</span><span class="mi">170</span><span class="p">,</span><span class="mi">111</span><span class="p">,</span><span class="mi">236</span><span class="p">),</span><span class="mi">5</span><span class="p">));</span>
<span class="n">getRootPane</span><span class="p">()</span><span class="o">.</span><span class="n">setDefaultButton</span><span class="p">(</span><span class="n">start</span><span class="p">);</span>
<span class="n">setVisible</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">browse</span><span class="o">.</span><span class="n">requestFocus</span><span class="p">();</span> <span class="o">//</span> <span class="n">this</span> <span class="n">must</span> <span class="n">exist</span> <span class="n">here</span> <span class="p">(</span><span class="n">after</span> <span class="n">frame</span> <span class="ow">is</span> <span class="n">visible</span><span class="p">),</span> <span class="k">else</span> <span class="n">will</span> <span class="ow">not</span> <span class="n">work</span>
<span class="n">new</span> <span class="n">Thread</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">while</span><span class="p">(</span><span class="n">true</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">retrieveServerIP</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">})</span><span class="o">.</span><span class="n">start</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">setFont</span><span class="p">(</span><span class="n">Font</span> <span class="n">f</span><span class="p">,</span><span class="n">JComponent</span><span class="o">...</span><span class="n">c</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">for</span><span class="p">(</span><span class="n">JComponent</span> <span class="n">C</span><span class="p">:</span><span class="n">c</span><span class="p">)</span>
<span class="n">C</span><span class="o">.</span><span class="n">setFont</span><span class="p">(</span><span class="n">f</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">openFile</span><span class="p">(</span><span class="n">File</span> <span class="n">path</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">File</span> <span class="n">f</span><span class="o">=</span><span class="n">path</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">counter</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="n">fileSize</span><span class="o">=</span><span class="n">f</span><span class="o">.</span><span class="n">length</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="n">fileSize</span><span class="o">==</span><span class="mi">0</span><span class="n">L</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"No Valid file found on specified location"</span><span class="p">,</span><span class="s2">"File not found"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">String</span> <span class="n">extension</span><span class="o">=</span><span class="n">f</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span><span class="o">.</span><span class="n">lastIndexOf</span><span class="p">(</span><span class="s2">"."</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">f</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span><span class="o">.</span><span class="n">length</span><span class="p">())</span><span class="o">.</span><span class="n">toUpperCase</span><span class="p">();</span>
<span class="n">long</span> <span class="n">tempSize</span><span class="o">=</span><span class="p">(</span><span class="n">long</span><span class="p">)</span><span class="n">fileSize</span><span class="p">;</span>
<span class="k">while</span><span class="p">(</span><span class="n">tempSize</span><span class="o">></span><span class="mi">1000</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">counter</span><span class="o">++</span><span class="p">;</span>
<span class="n">tempSize</span><span class="o">/=</span><span class="mi">1024</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"JAVA"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"C"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"CPP"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"CS"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"CSS"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"HTML"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"JS"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"PHP"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"XML"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"VB"</span><span class="p">))</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" Source File"</span><span class="p">;</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"JPG"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"BMP"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"PNG"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"GIF"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"TIFF"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"ICO"</span><span class="p">))</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" File (Image/Icon)"</span><span class="p">;</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MPG"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MPEG"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MP4"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"AVI"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"3GP"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"RMVB"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"WMV"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MKV"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"VOB"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MOV"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"FLV"</span><span class="p">))</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" File (Video)"</span><span class="p">;</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MP3"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"WMA"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"FLAC"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"AAC"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"AMF"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"AMR"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"M4A"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"M4R"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"OGG"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MP2"</span><span class="p">)</span><span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"WAV"</span><span class="p">))</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" File (Audio)"</span><span class="p">;</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"EXE"</span><span class="p">)</span> <span class="o">||</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"CMD"</span><span class="p">)</span> <span class="o">||</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"BAT"</span><span class="p">)</span> <span class="o">||</span> <span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"DMG"</span><span class="p">)</span> <span class="o">||</span><span class="n">extension</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"MSI"</span><span class="p">))</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" File (Executable File/Script)"</span><span class="p">;</span>
<span class="k">else</span>
<span class="n">extension</span><span class="o">+=</span><span class="s2">" Document/File"</span><span class="p">;</span>
<span class="n">nameField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">toString</span><span class="p">());</span>
<span class="n">sizeField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="n">tempSize</span> <span class="o">+</span><span class="s2">" "</span><span class="o">+</span><span class="n">sizeArray</span><span class="p">[</span><span class="n">counter</span><span class="p">]);</span>
<span class="n">typeField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="n">extension</span><span class="p">);</span>
<span class="n">startFlag</span><span class="o">=</span><span class="n">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">actionPerformed</span><span class="p">(</span><span class="n">ActionEvent</span> <span class="n">ae</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">String</span> <span class="n">s</span><span class="o">=</span><span class="n">ae</span><span class="o">.</span><span class="n">getActionCommand</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"Browse"</span><span class="p">))</span>
<span class="p">{</span>
<span class="nb">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">jfc</span><span class="o">.</span><span class="n">showOpenDialog</span><span class="p">(</span><span class="n">null</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="n">ret</span><span class="o">==</span><span class="n">JFileChooser</span><span class="o">.</span><span class="n">APPROVE_OPTION</span><span class="p">)</span>
<span class="n">openFile</span><span class="p">(</span><span class="n">jfc</span><span class="o">.</span><span class="n">getSelectedFile</span><span class="p">());</span>
<span class="n">start</span><span class="o">.</span><span class="n">requestFocusInWindow</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"X"</span><span class="p">))</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"-"</span><span class="p">))</span>
<span class="n">setState</span><span class="p">(</span><span class="n">JFrame</span><span class="o">.</span><span class="n">ICONIFIED</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"Send File"</span><span class="p">))</span>
<span class="k">if</span><span class="p">(</span><span class="n">startFlag</span><span class="p">)</span>
<span class="n">new</span> <span class="n">Thread</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">startServer</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">})</span><span class="o">.</span><span class="n">start</span><span class="p">();</span>
<span class="k">else</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Select a Valid file first ( size > 0B )"</span><span class="p">,</span><span class="s2">"Error"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">private</span> <span class="n">void</span> <span class="n">startServer</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">start</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="n">browse</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">false</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Waiting for Client to Connect"</span><span class="p">);</span>
<span class="n">double</span> <span class="n">startTime</span><span class="p">,</span><span class="n">endTime</span><span class="p">;</span>
<span class="o">//</span> <span class="n">time</span> <span class="n">variables</span><span class="p">;</span>
<span class="n">byte</span> <span class="n">b</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="n">byte</span><span class="p">[</span><span class="mi">100000</span><span class="p">];</span>
<span class="o">//</span> <span class="n">array</span> <span class="n">to</span> <span class="n">retrieve</span> <span class="n">data</span> <span class="kn">from</span> <span class="nn">server</span> <span class="ow">and</span> <span class="n">send</span> <span class="n">to</span> <span class="n">client</span>
<span class="n">double</span> <span class="n">done</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="o">//</span> <span class="n">done</span> <span class="ow">is</span> <span class="n">used</span> <span class="n">to</span> <span class="n">count</span> <span class="n">the</span> <span class="n">percentage</span>
<span class="nb">int</span> <span class="n">read</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="o">//</span> <span class="n">read</span> <span class="n">counts</span> <span class="n">the</span> <span class="nb">bytes</span> <span class="n">read</span> <span class="p">(</span><span class="n">within</span> <span class="mi">4</span> <span class="nb">bytes</span> <span class="n">integer</span> <span class="nb">range</span><span class="p">)</span> <span class="ow">in</span> <span class="n">WHILE</span> <span class="n">loop</span>
<span class="n">String</span> <span class="n">data</span><span class="p">,</span><span class="n">fileName</span> <span class="o">=</span> <span class="n">nameField</span><span class="o">.</span><span class="n">getText</span><span class="p">()</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="n">nameField</span><span class="o">.</span><span class="n">getText</span><span class="p">()</span><span class="o">.</span><span class="n">lastIndexOf</span><span class="p">(</span><span class="s2">"</span><span class="se">\"</span><span class="s2">)+1);</span>
<span class="o">//</span> <span class="n">data</span> <span class="ow">is</span> <span class="n">the</span> <span class="n">data</span> <span class="n">to</span> <span class="n">be</span> <span class="n">sent</span> <span class="n">via</span> <span class="n">BR</span><span class="p">,</span> <span class="n">stores</span> <span class="p">(</span><span class="n">filename</span> <span class="o">+</span> <span class="n">length</span><span class="p">)</span>
<span class="o">//</span> <span class="n">fileName</span> <span class="n">stores</span> <span class="n">the</span> <span class="n">name</span> <span class="n">of</span> <span class="n">the</span> <span class="n">file</span>
<span class="o">//</span> <span class="n">constructing</span> <span class="n">streams</span>
<span class="n">BufferedReader</span> <span class="n">br</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">read</span> <span class="n">String</span> <span class="ow">and</span> <span class="n">long</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">PrintWriter</span> <span class="n">pw</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">write</span> <span class="n">String</span> <span class="ow">and</span> <span class="n">long</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">BufferedInputStream</span> <span class="n">bis</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span><span class="n">to</span> <span class="n">write</span> <span class="n">file</span> <span class="n">contents</span> <span class="p">(</span><span class="n">byte</span> <span class="n">stream</span><span class="p">)</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">BufferedOutputStream</span> <span class="n">bos</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span><span class="n">to</span> <span class="n">read</span> <span class="n">byte</span> <span class="n">data</span> <span class="n">via</span> <span class="n">Socket</span>
<span class="n">FileInputStream</span> <span class="n">fis</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">to</span> <span class="n">read</span> <span class="n">actual</span> <span class="n">file</span> <span class="n">using</span> <span class="n">byte</span> <span class="n">stream</span>
<span class="n">ServerSocket</span> <span class="n">ss</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">this</span> <span class="n">will</span> <span class="nb">open</span> <span class="n">a</span> <span class="n">socket</span> <span class="n">on</span> <span class="n">port</span> <span class="mi">4000</span> <span class="n">on</span> <span class="n">local</span> <span class="n">system</span>
<span class="n">Socket</span> <span class="n">s</span><span class="o">=</span><span class="n">null</span><span class="p">;</span>
<span class="o">//</span> <span class="n">this</span> <span class="n">will</span> <span class="n">serve</span> <span class="n">a</span> <span class="n">local</span> <span class="n">port</span> <span class="k">for</span> <span class="n">a</span> <span class="n">client</span>
<span class="o">//</span> <span class="n">now</span> <span class="n">allocating</span> <span class="n">memory</span> <span class="n">to</span> <span class="n">objects</span> <span class="ow">and</span> <span class="n">starting</span> <span class="n">main</span> <span class="n">logic</span>
<span class="n">data</span><span class="o">=</span><span class="n">fileName</span><span class="o">+</span><span class="s2">"/"</span><span class="o">+</span><span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">fileSize</span><span class="p">);</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">ss</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ServerSocket</span><span class="p">(</span><span class="mi">4000</span><span class="p">);</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">accept</span><span class="p">();</span>
<span class="n">br</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedReader</span><span class="p">(</span><span class="n">new</span> <span class="n">InputStreamReader</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getInputStream</span><span class="p">()));</span>
<span class="n">pw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">PrintWriter</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getOutputStream</span><span class="p">());</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="n">IOException</span> <span class="n">ioe</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="n">ioe</span><span class="o">.</span><span class="n">toString</span><span class="p">(),</span><span class="s2">"error"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">pw</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
<span class="n">pw</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Begining File Transfer"</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="err">!</span><span class="p">(</span><span class="n">br</span><span class="o">.</span><span class="n">readLine</span><span class="p">()</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"Recieved"</span><span class="p">)))</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Client not Ready or connection was closed"</span><span class="p">,</span><span class="s2">"Retry"</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Faliure"</span><span class="p">);</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">bis</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedInputStream</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getInputStream</span><span class="p">());</span>
<span class="n">bos</span> <span class="o">=</span> <span class="n">new</span> <span class="n">BufferedOutputStream</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">getOutputStream</span><span class="p">());</span>
<span class="n">fis</span> <span class="o">=</span> <span class="n">new</span> <span class="n">FileInputStream</span><span class="p">(</span><span class="n">nameField</span><span class="o">.</span><span class="n">getText</span><span class="p">());</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Sending file to "</span><span class="o">+</span><span class="n">s</span><span class="p">);</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">remove</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">jpb</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">repaint</span><span class="p">();</span>
<span class="n">revalidate</span><span class="p">();</span>
<span class="n">startTime</span><span class="o">=</span><span class="n">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="p">();</span>
<span class="k">while</span><span class="p">((</span><span class="n">read</span><span class="o">=</span><span class="n">fis</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">b</span><span class="p">))</span><span class="o">!=-</span><span class="mi">1</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">done</span><span class="o">+=</span><span class="n">read</span><span class="p">;</span>
<span class="n">ServerSwingWorker</span> <span class="n">ssw</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ServerSwingWorker</span><span class="p">(</span><span class="n">done</span><span class="p">,</span><span class="n">fileSize</span><span class="p">,</span><span class="n">read</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">bos</span><span class="p">,</span><span class="n">jpb</span><span class="p">);</span>
<span class="n">ssw</span><span class="o">.</span><span class="n">execute</span><span class="p">();</span>
<span class="k">while</span><span class="p">(</span><span class="err">!</span><span class="p">(</span><span class="n">ssw</span><span class="o">.</span><span class="n">isDone</span><span class="p">())){}</span>
<span class="p">}</span>
<span class="n">bos</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="n">endTime</span><span class="o">=</span><span class="n">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="p">();</span>
<span class="n">browse</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">start</span><span class="o">.</span><span class="n">setEnabled</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
<span class="n">nameField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">sizeField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">typeField</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">""</span><span class="p">);</span>
<span class="n">remove</span><span class="p">(</span><span class="n">jpb</span><span class="p">);</span>
<span class="n">lowerBorder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="n">add</span><span class="p">(</span><span class="n">lowerBorder</span><span class="p">,</span><span class="n">BorderLayout</span><span class="o">.</span><span class="n">SOUTH</span><span class="p">);</span>
<span class="n">startFlag</span><span class="o">=</span><span class="n">false</span><span class="p">;</span>
<span class="n">repaint</span><span class="p">();</span>
<span class="n">revalidate</span><span class="p">();</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Sent 100 %"</span><span class="p">);</span>
<span class="n">double</span> <span class="n">time</span><span class="o">=</span><span class="p">(</span><span class="n">endTime</span><span class="o">-</span><span class="n">startTime</span><span class="p">)</span><span class="o">/</span><span class="mi">1000</span><span class="p">;</span>
<span class="n">double</span> <span class="n">speed</span><span class="o">=</span><span class="p">(</span><span class="n">fileSize</span><span class="o">/</span><span class="n">time</span><span class="p">)</span><span class="o">/</span><span class="mi">1048576</span><span class="p">;</span>
<span class="n">String</span> <span class="n">speedString</span><span class="o">=</span><span class="n">String</span><span class="o">.</span><span class="n">valueOf</span><span class="p">(</span><span class="n">speed</span><span class="p">);</span>
<span class="n">speedString</span><span class="o">=</span><span class="p">((</span><span class="n">speedString</span><span class="o">.</span><span class="n">indexOf</span><span class="p">(</span><span class="s2">"."</span><span class="p">)</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span><span class="o"><</span><span class="n">speedString</span><span class="o">.</span><span class="n">length</span><span class="p">()</span><span class="err">?</span><span class="n">speedString</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">speedString</span><span class="o">.</span><span class="n">indexOf</span><span class="p">(</span><span class="s2">"."</span><span class="p">)</span><span class="o">+</span><span class="mi">3</span><span class="p">):</span><span class="n">speedString</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">));</span>
<span class="n">pw</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="n">time</span><span class="p">);</span>
<span class="n">pw</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="n">pw</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="n">speedString</span><span class="p">);</span>
<span class="n">pw</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="s2">"Time taken is "</span><span class="o">+</span><span class="n">time</span><span class="o">+</span><span class="s2">"nSpeed is "</span><span class="o">+</span><span class="n">speedString</span><span class="o">+</span><span class="s2">" MBPS"</span><span class="p">,</span><span class="s2">"File Sent (Server)"</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Select File and Start Server"</span><span class="p">);</span>
<span class="o">//</span> <span class="n">reset</span> <span class="k">for</span> <span class="n">further</span> <span class="n">operation</span>
<span class="n">status</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="s2">"Select File and Start Server"</span><span class="p">);</span>
<span class="n">bis</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">bos</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">fis</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">br</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">pw</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">ss</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="n">s</span><span class="o">.</span><span class="n">close</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">catch</span><span class="p">(</span><span class="n">IOException</span> <span class="n">ioe</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">JOptionPane</span><span class="o">.</span><span class="n">showMessageDialog</span><span class="p">(</span><span class="n">null</span><span class="p">,</span><span class="n">ioe</span><span class="o">.</span><span class="n">toString</span><span class="p">(),</span><span class="s2">"Error"</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="n">System</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">final</span> <span class="n">String</span><span class="o">...</span><span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span><span class="p">(</span> <span class="n">args</span><span class="o">.</span><span class="n">length</span><span class="o">==</span><span class="mi">1</span> <span class="o">&&</span> <span class="err">!</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="s2">"%1"</span><span class="p">))</span> <span class="p">)</span>
<span class="n">new</span> <span class="n">Server</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
<span class="k">else</span>
<span class="n">new</span> <span class="n">Server</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">ServerSwingWorker</span> <span class="n">extends</span> <span class="n">SwingWorker</span>
<span class="p">{</span>
<span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">;</span>
<span class="n">final</span> <span class="n">double</span> <span class="n">done</span><span class="p">,</span><span class="n">size</span><span class="p">;</span>
<span class="n">byte</span> <span class="n">b</span><span class="p">[]</span> <span class="o">=</span> <span class="n">new</span> <span class="n">byte</span><span class="p">[</span><span class="mi">100000</span><span class="p">];</span>
<span class="n">final</span> <span class="nb">int</span> <span class="n">read</span><span class="p">;</span>
<span class="n">BufferedOutputStream</span> <span class="n">bos</span><span class="p">;</span>
<span class="n">ServerSwingWorker</span><span class="p">(</span><span class="n">double</span> <span class="n">done</span><span class="p">,</span><span class="n">double</span> <span class="n">size</span><span class="p">,</span><span class="nb">int</span> <span class="n">read</span><span class="p">,</span><span class="n">byte</span> <span class="n">b</span><span class="p">[],</span><span class="n">BufferedOutputStream</span> <span class="n">bos</span><span class="p">,</span><span class="n">JProgressBar</span> <span class="n">jpb</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">this</span><span class="o">.</span><span class="n">done</span> <span class="o">=</span> <span class="n">done</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="n">size</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">read</span> <span class="o">=</span> <span class="n">read</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">jpb</span> <span class="o">=</span> <span class="n">jpb</span> <span class="p">;</span>
<span class="n">this</span><span class="o">.</span><span class="n">bos</span> <span class="o">=</span> <span class="n">bos</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">protected</span> <span class="n">Void</span> <span class="n">doInBackground</span><span class="p">()</span> <span class="n">throws</span> <span class="ne">Exception</span>
<span class="p">{</span>
<span class="n">bos</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">read</span><span class="p">);</span>
<span class="k">return</span> <span class="n">null</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">protected</span> <span class="n">void</span> <span class="n">done</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">final</span> <span class="n">double</span> <span class="n">temp</span><span class="o">=</span><span class="p">(</span><span class="n">done</span><span class="o">/</span><span class="n">size</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">;</span>
<span class="n">SwingUtilities</span><span class="o">.</span><span class="n">invokeLater</span><span class="p">(</span><span class="n">new</span> <span class="n">Runnable</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">String</span> <span class="n">tString</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span><span class="o">.</span><span class="n">toString</span><span class="p">();</span>
<span class="nb">int</span> <span class="n">index</span><span class="o">=</span><span class="n">tString</span><span class="o">.</span><span class="n">indexOf</span><span class="p">(</span><span class="s2">"."</span><span class="p">);</span>
<span class="nb">int</span> <span class="n">breakPoint</span><span class="o">=</span> <span class="p">(</span><span class="n">index</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span><span class="o">></span><span class="n">tString</span><span class="o">.</span><span class="n">length</span><span class="p">()</span><span class="err">?</span><span class="n">tString</span><span class="o">.</span><span class="n">length</span><span class="p">():(</span><span class="n">index</span><span class="o">+</span><span class="mi">3</span><span class="p">);</span>
<span class="n">tString</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Double</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span><span class="o">.</span><span class="n">toString</span><span class="p">();</span>
<span class="n">tString</span><span class="o">=</span><span class="n">tString</span><span class="o">.</span><span class="n">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">breakPoint</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setString</span><span class="p">(</span><span class="s2">"Sending : "</span><span class="o">+</span><span class="n">tString</span><span class="o">+</span><span class="s2">" %"</span><span class="p">);</span>
<span class="n">jpb</span><span class="o">.</span><span class="n">setValue</span><span class="p">((</span><span class="nb">int</span><span class="p">)</span><span class="n">temp</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Just let us know if you are not able to understand any method or class used here</p>Getting Started With Chef-solo2014-01-30T12:59:00+05:302014-01-30T12:59:00+05:30Peeyush_Guptatag:thegeekyway.com,2014-01-30:/learning-basics-chef-solo-example/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/6521.OC_Chef_Logo.png"><img alt="Chef-Logo" src="//thegeekyway.com/uploads/images/2014/01/6521.OC_Chef_Logo-300x236.png"></a></p>
</div>
<p>Chef is an amazing tool from opscode to automate your work. Though it is very useful, it is also very complex to understand and work with. Before discussing about Chef-solo, here are some terminologies related to chef that we should learn first.</p>
<p><strong>Recipes</strong>: These are the files that contain the …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/6521.OC_Chef_Logo.png"><img alt="Chef-Logo" src="//thegeekyway.com/uploads/images/2014/01/6521.OC_Chef_Logo-300x236.png"></a></p>
</div>
<p>Chef is an amazing tool from opscode to automate your work. Though it is very useful, it is also very complex to understand and work with. Before discussing about Chef-solo, here are some terminologies related to chef that we should learn first.</p>
<p><strong>Recipes</strong>: These are the files that contain the configuration. Simply, these files have the code that is going to be used to configure your server.</p>
<p><strong>Cookbooks</strong>: A collection of recipes is called a cookbook.</p>
<p><strong>Chef-client</strong>: A tool which runs on the server that is supposed to be configured by chef.</p>
<p><strong>Chef-server</strong>: This is the server that provides cookbooks/recipes to chef-client.</p>
<p><strong>Chef-solo</strong>: An independent chef configuration tool that doesn’t need chef-server to work.</p>
<p>Chef-solo is the easiest way to learn how chef works. In this article we will see the working of chef-solo with an example. The first step is to install chef. Chef is installed as a rubygem. So, install rubygem and chef using following commands. (I am using Fedora. Use sudo apt-get for debain based systems.)</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>sudo<span class="w"> </span>yum<span class="w"> </span>install<span class="w"> </span>ruby<span class="w"> </span>rubygems<span class="w"> </span>ruby-devel
$<span class="w"> </span>gem<span class="w"> </span>install<span class="w"> </span>chef
</code></pre></div>
<p>You can check if your installation was successful or not by running ‘chef-solo’ command as sudo/root. It will most probably give an error “No cookbook found”. That’s alright, if you are getting any other error, then you need to check the installation again. So, now we are going to write a recipe to print “Hello chef” on the command prompt.</p>
<p>First of all, make a directory where you are going to do all your work. Let’s call it chef.</p>
<div class="highlight"><pre><span></span><code># mkdir chef
</code></pre></div>
<p>Now, we need to make the recipe. Let’s call the recipe hellochef. Here is how you can create it:</p>
<div style="text-align:center">
<p><img alt="" src="https://lh3.googleusercontent.com/JdMrZumIlNVIEI3ptyo02q4-gMyS7aK55eDpgLz0bffvFgYt85eUjNdYFW3OEApnqsMU8A_hXwirTrTeBh-N9VNvqREBQuKDUHhJ3yXfOyy9sKyExIFWegv5oA"></p>
</div>
<p>Now write the line ‘puts “Hello Chef!” ‘ in the file default.rb and close it. It is a ruby file, so it’s better you make yourself familiar with ruby before moving ahead with chef. Now that we have our recipe, next step is to redirect chef-solo to our recipe. We need to tell chef-solo that which cookbook we want to run. To do that create a node.json inside chef/ directory and put the recipe info inside it.</p>
<div style="text-align:center">
<p><strong><img alt="" src="https://lh6.googleusercontent.com/96TAdoropZDJ43-RziG1XATorRT5mJ9IZsLQWhCnWzo31PCPyJmZ61i_rRP9cBPcNbGfODxaokvtgNHAYthQi5Xu4eiCszll2xT7I0xAd8Rd_pTgvXOvhm57iw"></strong></p>
</div>
<p>Save and close the file. Now, you just need one more thing, that is chef-solo configuration file. Come back to your home directory and open a file named solo.rb and write the following info in the file:</p>
<div style="text-align:center">
<p><strong><img alt="" src="https://lh3.googleusercontent.com/efLRvQ_ifxyy7BnJAc_tIMHYf6d-uD_86EE83otimqO6rkCLbzoPK6mKp8vs4OjYEAwyFEdWwTcczVZKXnf7-o5wwM2fMofdTlcz2tfWa6f9t_DkZ6xSGbkbLw"></strong></p>
</div>
<p>Remember to change the locations of files relative to your system! Save and close the file. Now, we are ready to run chef-solo. Run 'chef-solo -c solo.rb' from command line and you should get an output like this:</p>
<div style="text-align:center">
<p><strong><img alt="" src="https://lh5.googleusercontent.com/oSvDyCeJjl1-7_sxTFgmEkTI66xcUh5Fh1IAAvftt7Cg_TvzYOLAdSJjA0dY6YgMEmbCsngCzKEHrldaw-bB7Py9sFLEhb2Z3u8FIs2F7tppJ6sVy-EgT-RcxQ"></strong></p>
</div>
<p>The -c option tells chef-solo about the location of the solo.rb configuration file. You can do a lot more with chef-solo than just printing Hello chef! This will get you started. Happy cooking!</p>Beginning Android, for Java / C++ programmers (Android-Part 0)2014-01-27T20:35:00+05:302014-01-27T20:35:00+05:30gagan93tag:thegeekyway.com,2014-01-27:/beginandroid/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/01.jpg5a51b1a1-4063-466c-945d-6178840074d0Larger.jpg" title="Beginning Android"><img alt="Beginning Android" src="//thegeekyway.com/uploads/images/2014/01/01.jpg5a51b1a1-4063-466c-945d-6178840074d0Larger-300x225.jpg" title="Beginning Android"></a></p>
</div>
<p>Most of the people now have Android devices. This attracts a lot of people to begin career as an Android Developer. Although creating games with sophisticated graphics isn't easy, but creating a simple Android app is. What you need to start Android Application Development is good knowledge of OOPS concept …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/01.jpg5a51b1a1-4063-466c-945d-6178840074d0Larger.jpg" title="Beginning Android"><img alt="Beginning Android" src="//thegeekyway.com/uploads/images/2014/01/01.jpg5a51b1a1-4063-466c-945d-6178840074d0Larger-300x225.jpg" title="Beginning Android"></a></p>
</div>
<p>Most of the people now have Android devices. This attracts a lot of people to begin career as an Android Developer. Although creating games with sophisticated graphics isn't easy, but creating a simple Android app is. What you need to start Android Application Development is good knowledge of OOPS concept.</p>
<p>The method used by most of the people to start Android is using Eclipse as an IDE (Integrated Development Environment). The experienced Android programmers may opt for Android Studio. Sometimes its just the matter of choice/comfort with a particular IDE.</p>
<p><strong>Prerequisite knowledge : </strong>Many people go for Android after doing Core Java. Few try it with C++ and/or basic programming knowledge. While it is possible to start with Android with a good understanding of OOP Concepts (Object Oriented Programming), but those having knowledge of Core Java always have advantage because Android uses rich Java libraries like <strong>java.io, java.net, java.lang, java.util</strong> etc. A bit XML is also required but simple interface provided by Eclipse can handle this part.</p>
<p>Fore people from C++ background, it will require a bit of effort to learn Java & join hands with Android. There is an option to use Android NDK (Native Development Kit) using which you can use C++ native code which runs on Android phones. But as recommended by most of the developers, writing an entire Android application in Native code is <strong>not the right way. </strong> Generally, NDK is used to create some performance critical code or for obfuscating a part of APK so that it is not easily decompiled.</p>
<p><strong>Android, Decompilation and Solution : </strong> As most of the Java developers must be knowing, CLASS files are easily decompiled using a simple and free software, same is the case with Android. Android binary files (APK) are easily decompiled. Some effective methods like Obfuscation are used to protect the code which modifies the code in such a way so that it is not easily readable by programmers. Same programs and methods apply to Android also as it is based on Java. Obfuscation tools are available for Android which makes it difficult for source to be leaked completely.</p>
<p><strong>IDE for Android Beginners : </strong></p>
<ol>
<li>To begin development, we just need Java Development Kit (JDK) <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html">available here</a> and Android SDK (Software Development Kit) <a href="http://developer.android.com/sdk/index.html?utm_source=weibolife"><u>available here</u></a>.</li>
<li>Install the JDK using default settings and extract the Android SDK to required location.</li>
<li>Open eclipse.exe from Eclipse folder from the extracted location.</li>
<li>The IDE loads within few seconds and opens a window where we can start creating a new project.* Basics of Android and First Simple Android Application will be discussed in next tutorial.</li>
</ol>
<p><a href="http://www.thegeekyway.com/setting-up-eclipse-ide-for-android-application-development/" title="Link"> <p style="font-size: xx-large">Link to First Android Tutorial</p></a></p>JAVA - Getting Started with Introduction2014-01-26T22:58:00+05:302014-01-26T22:58:00+05:30Manpreet_Singhtag:thegeekyway.com,2014-01-26:/learning-java-introduction/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/java_logo.jpg" title="Introduction to java"><img alt="Introduction_to_java" src="//thegeekyway.com/uploads/images/2014/01/java_logo.jpg"></a></p>
</div>
<p>Before learning JAVA, it is advisable that you should have a programming background. Even if you re not from programming background, I will try to make it as simple as I can.</p>
<p>JAVA is one of the most powerful language. The various software which are powered by this programming language …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/java_logo.jpg" title="Introduction to java"><img alt="Introduction_to_java" src="//thegeekyway.com/uploads/images/2014/01/java_logo.jpg"></a></p>
</div>
<p>Before learning JAVA, it is advisable that you should have a programming background. Even if you re not from programming background, I will try to make it as simple as I can.</p>
<p>JAVA is one of the most powerful language. The various software which are powered by this programming language are <strong>Rhino</strong>( open source implementation of JavaScript ), <strong>Sun SOPT</strong>( wireless, battery powered experimental platform ), <strong>ThinkFree</strong>( java version of Microsoft Office ), <strong>NASA World Wide</strong>( java application like Google earth ), <strong>UltraMixer</strong>( DJ mixing software ) and above all <strong>Android</strong> applications as well.</p>
<p>It is a programming language, which is based on the concepts of C++ ( inheritance,OOPs etc. ). It works on the motto</p>
<p><u><em>"Write Once, Run Anywhere".</em></u></p>
<h2>How does JAVA works?</h2>
<ol>
<li>Create a source file in this language and save it with .java extension.</li>
<li>Path of the source file is given to the JAVA compiler( javac ), the work of this compiler is to check for the errors, exception handling etc and then converting the .java file into a Byte-code, this byte-code is stored in a file with the extension .class . Byte-code is platform independent.</li>
<li>Now, we just require a JVM( Java Virtual Machine ), which will take byte code( .class file ) and then execute it.</li>
</ol>
<h2>How JAVA changed the Internet?</h2>
<ul>
<li><strong>Applets</strong>: A JAVA applet is a small program which is embedded in a web-page. Whenever a web-page ( embedded with applet ) is loaded at that point, Byte-Code is transferred and it is executed by the JVM( Java Virtual Machine ).</li>
<li><strong>Security</strong>: Whenever the JAVA code/software is distributed, it is always downloaded in the form of byte code, rather than the code itself. It is one of the major differences from C++, in which the source code is always distributed and anyone can make changes, implement it in his own way etc.</li>
<li><strong>Portability</strong>: It works on the principle of "Write Once, Run Anywhere". JAVA( language ) is platform independent, whereas the JVM( executes byte-code ) is platform dependent.</li>
</ul>
<h2>Features of JAVA:</h2>
<ol>
<li>Simple</li>
<li>Purely object oriented</li>
<li>Secure</li>
<li>Portable</li>
<li>Robust</li>
<li>Better look and feel than C/C++</li>
</ol>
<h2>Various Terms :</h2>
<ul>
<li><strong>JDK</strong>: Java Development Kit( JDK ) is consists of Java compiler, JVM and JRE.</li>
<li><strong>JRE</strong>: Java Run-time Environment( JRE ) consists of the JVM, along with libraries and supporting files(appletviewer etc.) as well. JRE is a part of JDK.</li>
<li><strong>JVM</strong>: Java Virtual Machine( JVM ) is a virtual machine which executes byte-code. It is provided along with JRE.</li>
<li><strong>Environment variables</strong>: Environment variables is a dynamic "object" on a computer, which stores a particular value, and this value is referred frequently by certain programs on the system.</li>
</ul>
<h2>Requirements:</h2>
<ol>
<li>JDK 7 <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html?rel=nofollow">Download it From Here</a></li>
<li>Notepad/Notepad++(or any text editor)</li>
<li>Do NOT use IDE, as it may protect you from some of the details which really matter.</li>
<li>After installing JDK, we have to set an Environment Variable named "path"</li>
</ol>
<p><strong>Setting the Environment variable ( in Windows 7 and 8)</strong></p>
<ul>
<li>Right Click on My Computer and select Properties.</li>
<li>Select Advanced System Settings, then go to Advanced Tab.</li>
<li>Click on Environment Variables, then click on New.</li>
<li>Variable name: path</li>
<li>Variable value: give the path directory till bin folder, in most cases the value will be <strong>C:Program FilesJavajdk1.7.0_45bin</strong></li>
</ul>
<p><strong>NOTE:</strong> Those who are using Linux or Windows XP, use Google for setting up the environment variables.</p>CSS - Border Radius for rounded corners2014-01-25T22:30:00+05:302014-01-25T22:30:00+05:30Anooptag:thegeekyway.com,2014-01-25:/css-border-radius-rounded-corners/<p>Border radius is used to create rounded corners see the following.</p>
<p>[codepen_embed height=200 theme_id=1 slug_hash='emxdk' user='ianoop' default_tab='css' animations='run'/]</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/css3-web-design-examples.jpg" title="css border radius"><img alt="css border radius" src="//thegeekyway.com/uploads/images/2013/12/css3-web-design-examples.jpg"></a></p>
</div>
<p style="line-height: 1.5em;">Above code fore border radius is equivalent to</p>
<div class="highlight"><pre><span></span><code><span class="nt">border-top-right-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-top-left-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-bottom-left-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-bottom-right-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">or</span>
<span class="nt">border-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
</code></pre></div>
<p>You can specify rounded …</p><p>Border radius is used to create rounded corners see the following.</p>
<p>[codepen_embed height=200 theme_id=1 slug_hash='emxdk' user='ianoop' default_tab='css' animations='run'/]</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/css3-web-design-examples.jpg" title="css border radius"><img alt="css border radius" src="//thegeekyway.com/uploads/images/2013/12/css3-web-design-examples.jpg"></a></p>
</div>
<p style="line-height: 1.5em;">Above code fore border radius is equivalent to</p>
<div class="highlight"><pre><span></span><code><span class="nt">border-top-right-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-top-left-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-bottom-left-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">border-bottom-right-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
<span class="nt">or</span>
<span class="nt">border-radius</span><span class="o">:</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="w"> </span><span class="nt">20px</span><span class="o">;</span>
</code></pre></div>
<p>You can specify rounded corners with different set of values.</p>
<p><strong>One :</strong> only one value set for all four corners<br>
<em>border-radius: 10px</em></p>
<p><strong>Two values :</strong> * top-left + bottom-right and top-right + bottom-right , diagonally<em><br>
</em>border-radius: 20px 30px*</p>
<p><strong>Three values:</strong> <em>top-left , top-right + bottom-left , bottom-right</em><br>
border-radius: 10px 30px 5px</p>
<p><strong>Four values:</strong> <em>top-left , top-right, bottom-right, bottom-left</em><br>
border-radius: 20px 10px 15px 30px;</p>
<p>[codepen_embed height=200 theme_id=1 slug_hash='logmL' user='ianoop' default_tab='result' animations='run'/]</p>
<p>You can even have different horizontal and vertical rounding</p>
<p><em>border-radius: 10px/30px /*Horizontal radius / Vertical Radius*/</em></p>
<p>[codepen_embed height=200 theme_id=1 slug_hash='amzfr' user='ianoop' default_tab='result' animations='run'/]</p>
<p>Few other example:</p>
<p>[codepen_embed height=200 theme_id=1 slug_hash='aqeBl' user='ianoop' default_tab='result' animations='run'/]</p>ASP. Net Basics : Deny access to a particular folder2014-01-24T00:36:00+05:302014-01-24T00:36:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-24:/asp-net-basics-deny-access-to-file-or-folder/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/ASP.NET_.ARTICLE.Deny_Access_to_user_cover_page.png"><img alt="ASP.NET.ARTICLE.Deny_Access_to_user_cover_page" src="//thegeekyway.com/uploads/images/2014/01/ASP.NET_.ARTICLE.Deny_Access_to_user_cover_page.png"></a></p>
</div>
<p>Setting authentication is most important part of asp.net web application project, there can a time where you don't want any user to access a particular folder. For example while creating a Gallery project we don't want any user to access the /pictures folder.</p>
<p><strong>ASP.net supports 3 modes of …</strong></p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/ASP.NET_.ARTICLE.Deny_Access_to_user_cover_page.png"><img alt="ASP.NET.ARTICLE.Deny_Access_to_user_cover_page" src="//thegeekyway.com/uploads/images/2014/01/ASP.NET_.ARTICLE.Deny_Access_to_user_cover_page.png"></a></p>
</div>
<p>Setting authentication is most important part of asp.net web application project, there can a time where you don't want any user to access a particular folder. For example while creating a Gallery project we don't want any user to access the /pictures folder.</p>
<p><strong>ASP.net supports 3 modes of authentication.</strong></p>
<ul>
<li>Windows authentication</li>
<li>Forms authentication</li>
<li>Passport authentication</li>
</ul>
<p>Windows authentication means that you build authentication with build in authentication of windows authentication system.</p>
<p>With form authentication we work with the database where all the roles of user are stored in database and every time we land on the page it will make connection to database and verify the database whether the user is allowed or not to access this page.</p>
<p>And with passport authentication you are authenticating the users by passport credentials, a system of username and password stored by Microsoft itself.</p>
<p>In this tutorial we will focus on forms authentication. In forms authentication model it requires configuration in the web-files in web.config file and then creating set of pages.</p>
<p>The part of web.config file which we will edit looks like this :-</p>
<p>If you see line 46, you will see that the line <strong>authentication mode="Windows"</strong> we need to change the value to <strong>"forms"</strong> and save the changes.</p>
<h3>Locking down the website</h3>
<p>Locking down the website means that whenever the user is navigated to a website then they are automatically redirected to the login page or the home page.<br>
Let us assume we want to protect the page named "TheGeekyWay.aspx" stored in "TheGeekyWay" folder. Now using forms authentication we can block both TheGeekyWay.aspx page and TheGeekyWay folder.</p>
<p>Here is the hierarchy of project of this tutorial :- </p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/TUT.ASP_.ACCESS.1.png"><img alt="TUT.ASP.ACCESS.1" src="//thegeekyway.com/uploads/images/2014/01/TUT.ASP_.ACCESS.1.png"></a></p>
</div>
<p>Now if we want to Lockout the TheGeekyWay folder. In the web.config file, navigate to the end of system.web element, in the above web.config file its on the 74th line, so on the 75th line add the following code</p>
<p>This code will only allow people with admin roles and will deny any other user similarly we can block TheGeekyWay.aspx page by using the following code</p>
<h3>Common mistakes people make while adding code to web.config</h3>
<p>Sometimes we add deny code earlier than allow code, so no access is given to any of the user. So this must be kept in mind that deny code must always be used after allow code.</p>Z-Index in CSS2014-01-19T00:52:00+05:302014-01-19T00:52:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-19:/z-index-in-css/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/zindex.png"><img alt="z-index" src="//thegeekyway.com/uploads/images/2014/01/zindex.png"></a></p>
</div>
<h1>What is Z-Index ?</h1>
<p>Z-Index is the property of CSS which is used to order the elements behind and in front in the form of stack. You can consider this a virtual stack where the higher value of z-index is placed infront and the lower value of z index is placed …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/zindex.png"><img alt="z-index" src="//thegeekyway.com/uploads/images/2014/01/zindex.png"></a></p>
</div>
<h1>What is Z-Index ?</h1>
<p>Z-Index is the property of CSS which is used to order the elements behind and in front in the form of stack. You can consider this a virtual stack where the higher value of z-index is placed infront and the lower value of z index is placed at the back.</p>
<p>To understand this let us consider the html and css code :-</p>
<p>[codepen_embed height=620 theme_id=3 slug_hash='wnDcv' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<p>This is just a simple code to create 2 boxes and displaying them.The next step is to overlap these two boxes since HTML works on stack basis which means that the bottom code will be displayed on top, so now updating the code we get :-</p>
<p>[codepen_embed height=620 theme_id=3 slug_hash='ctnjB' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<p>This is just the normal css code with margins defined, but <strong>What if we want DIV1 to be placed above DIV2 ?</strong> , here Z-Index property comes into effect. Various attributes of z-index are :-</p>
<p>Value Description</p>
<hr>
<p>auto The browser determines the stack order of the element (based on its order in the document). This is default
<em>number</em> An integer that defines the stack order of the element. Negative values are allowed
inherit The value of the zIndex property is inherited from parent element</p>
<p>Now adding the z-index property in the CSS code we get the following output.</p>
<p>[codepen_embed height=620 theme_id=3 slug_hash='vizcB' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<h2>Did it worked ?</h2>
<p>The answer is <p style="color: #f80b06;"><strong>No</strong></p> ! The reason being that the z-index value is pretty much ignored by the browser unless the elements are positioned. We can use <strong>position:relative</strong> declaration to both elements. Now if we use the code we will get the following output :-</p>
<p>[codepen_embed height=620 theme_id=3 slug_hash='EzLmu' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<p><strong>Remember :</strong> We should use larger values, the reason behind is that we should leave room for more values to add and values between 4 and 5 there is no whole number between it and in future we might need to add more z-index between these 2 div's, that's not that important but in advanced designs it might create a problem.</p>
<h1>What z-index cannot do ? (in respect to overlapping)</h1>
<p>Let us consider a child element of DIV2, a DIV3 which has the z index greater than DIV1 and DIV2, but since its the child element of DIV2 so it will be overlapped with DIV2 and DIV1 will be on the top. To demonstrate this let us consider an example below :-</p>
<p>[codepen_embed height=620 theme_id=3 slug_hash='dCxfl' user='Deep_Singh' default_tab='Result' animations='run'/]</p>
<hr>PYTHON - Getting Started2014-01-15T13:03:00+05:302014-01-15T13:03:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-01-15:/python/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"><img alt="1624592_800549696627341_1247213667_n" src="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"></a></p>
</div>
<hr>
<p style="color: #000000"> </p>
<p style="color: #000000">Why Python ?</p>
<hr>
<p style="color: #000000">Python is a **high-level programming** language whose syntax is said to be clear and expressive. Python has a large standard library. Python is an easy to understand language we can learn this language in a few days and for making any gaming software we should must have good …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"><img alt="1624592_800549696627341_1247213667_n" src="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"></a></p>
</div>
<hr>
<p style="color: #000000"> </p>
<p style="color: #000000">Why Python ?</p>
<hr>
<p style="color: #000000">Python is a **high-level programming** language whose syntax is said to be clear and expressive. Python has a large standard library. Python is an easy to understand language we can learn this language in a few days and for making any gaming software we should must have good knowledge of **GUI development** using that language. GUI development using Python is very easy and understandable as compared to other language like C, C++or Java.</p>
<p style="color: #000000">Starting Python</p>
<hr>
<p style="color: #000000">Creating a simple Hello world program</p>
<ol>
<li><p style="color: #000000">We need a python compiler to compile python programs which can be downloaded from <strong>www.python.org</strong></p></li>
<li><p style="color: #000000">Writing a program in <strong>text file</strong> or <strong>console</strong></p></li>
</ol>
<p style="color: #000000">**Text File**</p>
<div class="highlight"><pre><span></span><code>print 'hello world'
</code></pre></div>
<div style="text-align:center">
<p>Compiling and running the program are done using single command <strong>python ‘filename’</strong> <a href="//thegeekyway.com/uploads/images/2014/01/screen1.png"><img alt="screen1" src="//thegeekyway.com/uploads/images/2014/01/screen1.png"></a></p>
</div>
<p style="color: #000000">**Console**</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/screen2.png"><img alt="screen2" src="//thegeekyway.com/uploads/images/2014/01/screen2.png"></a></p>
</div>
<p style="color: #000000">To get exit from command line interface just type the command **exit()** .</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-5.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-5.png"></a></p>
</div>
<p style="color: #000000">To get help about some methods related to python or the keywords we can type help in command line interface .Follow these steps</p>
<ol>
<li><p style="color: #000000">Type python in command line to start the interpreter of python.</p></li>
<li><p style="color: #000000">Type <strong>help</strong> .</p></li>
<li><p style="color: #000000">To get help about <strong>keywords</strong> or <strong>modules</strong> available in python just type ethe word keyword.</p></li>
</ol>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-51.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-51.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-52.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-52.png"></a></p>
</div>
<p style="color: #000000">All these keywords will be discussed in the coming tutorials one by one .As we can see there are nearly **31 keywords** available in python 2.6.5.</p>
<p style="color: #000000">**Print statement**</p>
<hr>
<p style="color: #000000">Print statement is used to print the data in python.Printing a statement in python is just so simple as we dont need to add any libraries for using print command as which we have to do in other languages.We have seen how to print "hello world", we can print same "hello world" in different way by writing 2 words in different inverted commas and printing it .To print 2 words at the same time we use comma operator between them.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-54.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-54.png"></a></p>
</div>
<p style="color: #000000">**Variable** are printed by just writing the name of variables and no inbverted commas are required .Variables will be covered in next tutorial ,there we will be showing how to print the variables . </p>
<p style="color: #000000">Comments</p>
<hr>
<p style="color: #000000">Comments are used to document our code . We write comments in python by using **symbol \#** and writing any expression ,line,or character in front of the \#.Compiler ignores the words or expression in comment.</p>
<p style="color: #000000">There are two types of comments available in python:</p>
<h3><p style="color: #000000"><strong>Single line comments</strong></p></h3>
<p style="color: #000000">\# is used to write the single line comments . </p>
<div class="highlight"><pre><span></span><code>print 'hello world' #will print hello world
</code></pre></div>
<p style="color: #000000">**will print hello world** is a comment and will be ignored by the compiler .</p>
<h3><p style="color: #000000"><strong>Multiple Line comments</strong></p></h3>
<p style="color: #000000">To write multiline comments the symbol is """ """ .</p>
<div class="highlight"><pre><span></span><code>print 'hello world' """ this is a
multiline comment """
</code></pre></div>
<h3><p style="color: #000000"> Any query related to this tutorial can be asked .In our next Tutorial we will discuss about variables .</p></h3>Understanding Conditional Statements: if else2014-01-12T23:08:00+05:302014-01-12T23:08:00+05:30sanyam_khuranatag:thegeekyway.com,2014-01-12:/conditional-statements-if-else/<p>In this tutorial, we'll uncover something much interesting which would simplify our work as a programer and help us to check if a given condition is true or not. We'll study about the Conditional statements like <strong>if - else</strong> , <strong>if-else if-else</strong>, <strong>nested if -else</strong> and <strong>switch</strong> (continued in next tutorial)</p>
<p><u><strong>Understanding …</strong></u></p><p>In this tutorial, we'll uncover something much interesting which would simplify our work as a programer and help us to check if a given condition is true or not. We'll study about the Conditional statements like <strong>if - else</strong> , <strong>if-else if-else</strong>, <strong>nested if -else</strong> and <strong>switch</strong> (continued in next tutorial)</p>
<p><u><strong>Understanding if else statements :</strong></u></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Conditional-statements-if-else1.png" title="Conditional statements in C : if else, nested if-else"><img alt="Conditional statements in C : if else, nested if-else" src="//thegeekyway.com/uploads/images/2014/01/Conditional-statements-if-else1.png"></a></p>
</div>
<p>To make the control of the program use conditional flow we need to use conditional statements, as the name suggests, this type of flow of control depends upon a particular condition ie either if it is true or it is false etc.</p>
<p>We'll first look at an if statement by considering this example :-</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program would simply accept a value from user, and if the value entered is equal to 5 ie of course 5, then we would get a message printed on console "Number entered is 5" , if value entered is not 5, nothing would happen.</p>
<p>Note: whenever you write an if statement, you should have an opening and closing parenthesis as shown (in case you need to have multiple statements to be executed for an if statement) else, the program with just single statement in if block should not necessarily consist a block, so the above program would work if</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>is written as</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
</code></pre></div>
<p>So, we just instructed the computer to print a message, if a given condition happens to be true (which is entered value is 5 in this case).</p>
<p>Now, consider, we wanted program to print a message if the value entered is not 5, ie, if value entered is 5, it would print the message "Number entered is 5" otherwise, it would print "Number entered is not 5" . Let's see how this can be done with the help of if-else</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>Let's say if we need to print message for if a number is 2, it should print "Number entered is 2" , if it's 5, it should print "Number entered is 5" otherwise, it should print "Number is neither 2 nor 5. Let's see how it can be done with if-else statements</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 5"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program could include any no of if - else if - else statements (which means no. of else if statements in between if and else can be increased )</p>
<p>To understand nested if-else let's see the following program:</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">num</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Enter a number n"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span><span class="s2">"%d"</span>,<span class="o">&</span><span class="nv">num</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is even"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">if</span><span class="ss">(</span><span class="nv">num</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="ss">)</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not 2"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span><span class="s2">"Number entered is not even"</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above program first checks if a number is even or not, if it's even then it checks if number is 2 or not.</p>
<p>Note : To check number is even or not, we used modulus operator as discussed in <a href="http://www.thegeekyway.com/operators-c-language/" title="Operators in the C language">Tutorial on Operators</a> . Here we checked, if the number when divided by 2 leaves remainder 0 ie, it is entirely divisible by 2, then it's even else it's not even.</p>
<p>In the next tutorial, we'll know about another conditional statement, switch statement.</p>Operators in the C language2014-01-09T22:57:00+05:302014-01-09T22:57:00+05:30sanyam_khuranatag:thegeekyway.com,2014-01-09:/operators-c-language/<p>Welcome to your next tutorial in the C language, today, we’ll discuss about operators in the C language. We have already discussed about <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">Variables in C language</a>, and <a href="http://www.thegeekyway.com/learn-c-variables/" title="Learn C – More about variables">how we can store data in variables</a> input by user, next we may need to update this data , or make various …</p><p>Welcome to your next tutorial in the C language, today, we’ll discuss about operators in the C language. We have already discussed about <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">Variables in C language</a>, and <a href="http://www.thegeekyway.com/learn-c-variables/" title="Learn C – More about variables">how we can store data in variables</a> input by user, next we may need to update this data , or make various operations on the variables to produce desired results. This can be done through Operators in the C language.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Operators_in_C1.png" title="Operators in C"><img alt="Operators_in_C" src="//thegeekyway.com/uploads/images/2014/01/Operators_in_C1.png"></a></p>
</div>
<p>Operators can be classified broadly as follows :</p>
<ul>
<li>Arithmetic Operators</li>
<li>Logical Operators</li>
<li>Relational Operators</li>
<li>Assignment Operator</li>
<li>Bitwise</li>
<li>Conditional Operator</li>
<li>Miscellaneous Operators</li>
</ul>
<p>Now we’ll discuss about each category one by one, let us assume that we have declared two variables num1 and num2 and initialized them as shown below.</p>
<div class="highlight"><pre><span></span><code>int num1 = 3;
int num2 = 6;
</code></pre></div>
<p>Let's have another variable named res for storing result, so that we can analyze the value of result after applying each operator and understand their functioning.</p>
<p>But before this, we need to understand that there are basically two kinds of <em><strong>Operators depending on the number of operands</strong> they operate upon</em>. They are :-</p>
<p><strong>Unary Operators</strong> : which operate on only one operand.</p>
<p><strong>Binary Operators</strong> : which requires two operands to be operated upon.</p>
<p>Now, we'll carry on with understanding different types of operator.</p>
<p><strong>Arithmetic Operators</strong></p>
<p>They are binary operators used to do arithmetic operations like addition , subtraction , multiplication , division etc.</p>
<p><em><u>+ used for addition</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num1 + num2;
</code></pre></div>
<p>This would add the two values ie 5 and 6 and store the result to variable res which is 9 in this case.</p>
<p><em><u>- used for subtraction</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 - num1;
</code></pre></div>
<p>This would subtract num1 from num2 and then store the result in variable res which is 3 in this case.</p>
<p><em><u>* used for multiplication</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num1 * num2;
</code></pre></div>
<p>This would multiply num1 and num2 and store the result in variable res which is 18 in this case.</p>
<p><em><u> / used for division</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 / num1;
</code></pre></div>
<p>This would divide the two values ie num2 by num1 and then store the result in variable res which is 2 in this case.</p>
<p><em><u>% (modulus operator )</u></em> like</p>
<div class="highlight"><pre><span></span><code>res = num2 % num1;
</code></pre></div>
<p>This operator will divide num2 by num1 and then gives us the <em>REMAINDER</em> which will be stored in variable res and it's value would be 0 in this case , since, when 6 is divided by 3, it would give remainder 0.</p>
<p><strong>Logical Operators</strong>:</p>
<p>They are included in logical expressions and are of following types :</p>
<ul>
<li>Logical AND (&&)</li>
<li>Logical NOT ( ! )</li>
<li>Logical OR ( || )</li>
</ul>
<p>Note that in Logical AND and Logical OR, we use the respective symbol two times as mentioned. We'll study their complete usage in the coming tutorials specially in <em><u>Loops</u></em> and <em><u>Conditional Statements</u> .</em></p>
<p><strong>Relational Operator:</strong></p>
<p>As the name suggests, they are used to depict relations among variables. Mostly they return either a true (1) value or a false (0) value. They are of the following types :</p>
<ul>
<li>Less than or equal to ( \<= )</li>
<li>More than or equal to ( >= )</li>
<li>Not equal to ( != )</li>
</ul>
<p>Again, they would be better understood when we'll discuss about Conditional Statements and Loops, so we'll discuss them in the upcoming tutorials, just remember the types of operators for now.</p>
<p><strong>Assignment Operator:</strong></p>
<p>They are used to assign values to a variables, we are already using this operator since we studied about assigning a value to our variables, so consider the following :</p>
<div class="highlight"><pre><span></span><code>int num = 5;
</code></pre></div>
<p>Here ( = ) ie assignment operator is used to assign value 5 to variable num.</p>
<p><strong>Miscellaneous Operators</strong>:</p>
<p>There are other operators too, but we have covered the most important operators here.</p>
<p>Unary + and Unary - are some of the operators you would see often. As the name suggests they operate on only one operand. To understand what they do, consider the following code snippet :</p>
<div class="highlight"><pre><span></span><code>int num = 5;
num = (+num);
printf("Value of num after Unary + is %d n",&num);
num = -num;
printf("Value of num after Unary - is %d",&num);
</code></pre></div>
<p>Including all the necessary header files and other stuff required, it would produce the output as follows:</p>
<p>Value of num after Unary + is 5</p>
<p>Value of num after Unary - is -5</p>
<p>So, you understood it right Unary + doesn't changed the sign of the value, while unary - reversed the sign of integer value, so +5 become -5 , and it would be vice-versa if we have a negative value in variable.</p>
<p>In the next post, we'll study about Conditional statements and then about Loops in C and their usage, they would help us understand the concept of operators like logical and relational operator in much more detail. If you have any doubts/question, please feel free to flood them in the comments section, we'll help you !</p>Asp.net Basics : Uploading a file in asp.net using validations (Part 5 of 5)2014-01-07T05:22:00+05:302014-01-07T05:22:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-07:/asp-net-basics-uploading-file-asp-net-using-validations-part-5/<p><center>
<u>This is a part 5 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p><u>Contents of this 5 part tutorial</u><br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net …</a></p><p><center>
<u>This is a part 5 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p><u>Contents of this 5 part tutorial</u><br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete ASP.Net file uploading using all the Validations one at a time.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart5.png"><img alt="CoverImageASPNetFileUploadPart5" src="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart5.png"></a></p>
</div>
<hr>
<h2><strong><em>*File Upload in asp.net : v1.4final</em></strong>*</h2>
<p>In last 4 tutorials we discussed how to upload the file on server and validate the file with various validations. This is the last part of tutorial where all the validations are used at once.</p>
<p>Here is the final C# code clubbed into all one</p>
<div class="highlight"><pre><span></span><code><span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Configuration</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Data</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Linq</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">Security</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">UI</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">UI</span><span class="o">.</span><span class="n">HtmlControls</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">UI</span><span class="o">.</span><span class="n">WebControls</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">UI</span><span class="o">.</span><span class="n">WebControls</span><span class="o">.</span><span class="n">WebParts</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Xml</span><span class="o">.</span><span class="n">Linq</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Drawing</span><span class="p">;</span>
<span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">IO</span><span class="p">;</span>
<span class="n">public</span><span class="w"> </span><span class="n">partial</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">_Default</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Web</span><span class="o">.</span><span class="n">UI</span><span class="o">.</span><span class="n">Page</span><span class="w"> </span>
<span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="n">protected</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">Page_Load</span><span class="p">(</span><span class="n">object</span><span class="w"> </span><span class="n">sender</span><span class="p">,</span><span class="w"> </span><span class="n">EventArgs</span><span class="w"> </span><span class="n">e</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">false</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">protected</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">Button1_Click</span><span class="p">(</span><span class="n">object</span><span class="w"> </span><span class="n">sender</span><span class="p">,</span><span class="w"> </span><span class="n">EventArgs</span><span class="w"> </span><span class="n">e</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">true</span><span class="p">;</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">flag1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">isFileEmpty</span><span class="p">();</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">flag2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="p">();</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">flag3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">checkFileSize</span><span class="p">();</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">flag2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">flag3</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">saveFile</span><span class="p">();</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Success ! File uploaded successfully"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File Not uploaded <br> Please refer the notes below"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">isFileEmpty</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">HasFile</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">This</span><span class="w"> </span><span class="n">part</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">just</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">internal</span><span class="w"> </span><span class="n">coding</span><span class="w"> </span><span class="n">no</span><span class="w"> </span><span class="n">need</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">display</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="n">on</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="n">screen</span><span class="w"> </span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Path</span><span class="o">.</span><span class="n">GetExtension</span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb nb-Type">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">".pdf"</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image2</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File Extension"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File Type"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Wrong file extension."</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image2</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"No file type available"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"X"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Wrong file extension."</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image2</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File type"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Type0</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">checkFileSize</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">filesize</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">PostedFile</span><span class="o">.</span><span class="n">ContentLength</span><span class="p">)</span><span class="o">/</span><span class="mi">1024</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">filesize</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&&</span><span class="n">filesize</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">2048</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" File size "</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image3</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">filesize</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"No file posted"</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image3</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File size limit exceeded <br>(greater than 2MB)"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Size</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image3</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">saveFile</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">SaveAs</span><span class="p">(</span><span class="n">Server</span><span class="o">.</span><span class="n">MapPath</span><span class="p">(</span><span class="s2">"~/uploads/"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">));</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>and here are the outputs, all the outputs are explained within the description of image</p>
<p><strong>Situation : Uploading a file which has a limit of 2MB and the file type limit is it should be a ".pdf" file.</strong></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-1.png" title="This is the structure of file upload system, this image should be used as reference while studying the code"><img alt="This is the structure of file upload system, this image should be used as reference while studying the code" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-1.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-2.png" title="This is a condition where the file type uploaded is correct but the file size limit exceeded."><img alt="This is a condition where the file type uploaded is correct but the file size limit exceeded." src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-2.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-4.png" title="This is the state where the file size limit is under 2MB but the file type is incorrect."><img alt="This is the state where the file size limit is under 2MB but the file type is incorrect." src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-4.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-3.png" title="This is the state where all the conditions are true and the file is finally uploaded."><img alt="This is the state where all the conditions are true and the file is finally uploaded." src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-3.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-5.png" title="This is the state where no file is uploaded and the user clicks the upload button."><img alt="This is the state where no file is uploaded and the user clicks the upload button." src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut5-5.png"></a></p>
</div>
<p>That's all for file uploading system. Hope you enjoyed the article. Please share it in/with your groups/circles/friends and do comment if liked the article.</p>Asp.net Basics : Uploading a file in asp.net using validations (Part 4 of 5)2014-01-06T16:00:00+05:302014-01-06T16:00:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-06:/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/<p><center>
<u>This is a part 4 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net …</a></p><p><center>
<u>This is a part 4 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete ASP.Net file uploading using all the Validations one at a time.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut4-1.png"><img alt="FileUploadTut4-1" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut4-1.png"></a></p>
</div>
<hr>
<h2><strong><em>*File Upload in asp.net : v1.3 (File Extension Validation)</em></strong>*</h2>
<p>This is the final part of the 5 part tutorial where we'll discuss about the FileExtension validations during file uploading. Some of the professional companies demand that the resume should be in .doc or .docx or .pdf extension only, so the main point arises what if the user uploads the file in some other extension (for example : in .txt file), there should be a validator for that to validate the extension of the file. Fortunately in Microsoft Visual Studio we can do that, there is a method <strong>GetExtension()</strong> which gets the extension of the file and has a return type of <strong>string</strong>.</p>
<p>GetExtension() method is saved in class <strong>Path</strong> which is the part of <strong>System.IO</strong> and returns a string. The complete syntax for using FileExtension validation is :-</p>
<blockquote>
<p><strong>string</strong> GetFileExtension = <strong>Path</strong>.<strong>GetExtension</strong>(FileUpload1.FileName);</p>
</blockquote>
<p>where FileUpload1 is the fileupload control and FileName gets the current file which is being uploaded.</p>
<p>Using this method we can get the file extension and we can compare the values using if-else.</p>
<p>The complete <a href="http://www.thegeekyway.com/tag/c-2/">C#</a> code is</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Path</span><span class="o">.</span><span class="n">GetExtension</span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">GetFileExtension</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">".pdf"</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File Extension OK ! File will be uploaded"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">GreenYellow</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Provide</span><span class="w"> </span><span class="n">upload</span><span class="w"> </span><span class="n">code</span><span class="w"> </span><span class="p">(</span><span class="n">discussed</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">part</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">tutorial</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"File Extension detected : '"</span><span class="w"> </span><span class="o">+</span><span class="n">GetFileExtension</span><span class="o">+</span><span class="w"> </span><span class="s2">"'. Required file extension : '.pdf' "</span><span class="o">+</span><span class="s2">"File Not uploaded"</span><span class="w"> </span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Extension</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<p>Output of the current code will be</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut4-2.png"><img alt="FileUploadTut4-2" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut4-2.png"></a></p>
</div>
<p>Thats the final part of the file upload system, in the next tutorial we'll add up all the part of this tutorial and present the final part of asp.net file upload tutorial.</p>Customize GRUB2/BURG Screen Using Grub Customizer2014-01-05T19:33:00+05:302014-01-05T19:33:00+05:30Abhishek_Rawaltag:thegeekyway.com,2014-01-05:/customize-grub2-burg-screen-using-grub/<p><strong>GRUB Customizer</strong> is the application with graphical interface to configure GRUB2/BURG settings, visuals & menu-entries.</p>
<blockquote>
<p><strong>Features:</strong></p>
<p>- supports GRUB2 & BURG</p>
<p>- rename, remove & move the menu-entries</p>
<p>- Installation of bootloader to MBR</p>
<p>- settings like default distro, Kernel params, BG image, text colors etc. to customize the look of GRUB2/BURG</p>
</blockquote>
<p>You can simple …</p><p><strong>GRUB Customizer</strong> is the application with graphical interface to configure GRUB2/BURG settings, visuals & menu-entries.</p>
<blockquote>
<p><strong>Features:</strong></p>
<p>- supports GRUB2 & BURG</p>
<p>- rename, remove & move the menu-entries</p>
<p>- Installation of bootloader to MBR</p>
<p>- settings like default distro, Kernel params, BG image, text colors etc. to customize the look of GRUB2/BURG</p>
</blockquote>
<p>You can simple right click on above entries & then you can Edit, Rename, Remove, Move up or down, remove from submenu or add another submenu easily. Everything can be customized within user interface.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_004.png" title="Grub Customizer -> List Configuration"><img alt="List Configuration" src="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_004.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_005.png" title="Grub Customizer -> General Settings"><img alt="Grub Customizer -> General Settings" src="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_005.png"></a></p>
</div>
<p>Advanced settings are for advanced users who wanna customize as per their wish.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer-settings_006.png" title="Grub Customizer -> Advanced settings"><img alt="Grub Customizer -> Advanced settings" src="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer-settings_006.png"></a></p>
</div>
<p>I am using elementaryOS at this moment, I have picked .PNG image from deviantart & adjusted color of my choice. And, clicked on 'save settings' on left top corner & well that's done.</p>
<p>Just to make sure, you can run '<strong>sudo update-grub</strong>'.</p>
<blockquote>
<p><strong>Note : I have noticed that current/latest version of Grub customizer doesn't work with .jpeg BG image files, so make sure it is of .png else it won't work.</strong></p>
</blockquote>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_003.png" title="Grub Customizer -> Appearance settings"><img alt="Grub Customizer -> Appearance settings" src="//thegeekyway.com/uploads/images/2014/01/Grub-Customizer_003.png"></a></p>
</div>
<p style="color: #da243a;">To install **Grub Customizer** in Ubuntu & it's derivatives, open Terminal & add PPA by typing :</p>
<div class="highlight"><pre><span></span><code>sudo add-apt-repository ppa:danielrichter2007/grub-customizer
</code></pre></div>
<p>Now update the repository,,</p>
<div class="highlight"><pre><span></span><code>sudo apt-get update
</code></pre></div>
<p>Now install the Grub-customizer</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install grub-customizer
</code></pre></div>
<p style="color: #da243a;">To install **Grub Customizer** in other Linux distribution :</p>
<p><u><em>Install the following packages :</em></u></p>
<p><strong>- cmake</strong></p>
<p><strong>- gcc-c++</strong> or <strong>g++</strong></p>
<p><strong>- libgtkmm-3.0-dev</strong> or <strong>gtkmm30-devel</strong></p>
<p><strong>- libssl-dev</strong> or <strong>openssl-devel</strong></p>
<p><strong>- libarchive-dev</strong> or <strong>libarchive-devel</strong></p>
<p><strong>- gettext</strong></p>
<p>Download the source from <a href="https://launchpad.net/grub-customizer/+download" title="Download Now!"><strong>HERE</strong></a></p>
<p>Go to source directory & use <strong>cmake</strong>, <strong>make</strong> & <strong>make install</strong> to install grub customizer. (You can find detailed information on 'How To Compile application from source code' from <a href="http://www.thegeekyway.com/install-application-source-code-2/" title="Install application from source-code">THIS LINK</a>)</p>
<p>After installing & modding the GRUB2 loader, here's what my new beautiful GRUB2 screen looks like (sorry for blur image, my camera is crap) :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/bootscreen.jpg"><img alt="bootscreen" src="//thegeekyway.com/uploads/images/2014/01/bootscreen.jpg"></a></p>
</div>
<p>That's it.</p>Asp.net Basics : Uploading a file in asp.net using validations (Part 3 of 5)2014-01-05T01:03:00+05:302014-01-05T01:03:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-05:/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/<p><center>
<u>This is a part 3 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net …</a></p><p><center>
<u>This is a part 3 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete ASP.Net file uploading using all the Validations one at a time.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart3.png"><img alt="CoverImageASPNetFileUploadPart3" src="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart3.png"></a></p>
</div>
<hr>
<h2><strong><em>*File Upload in asp.net : v1.2 (File Size Validation)</em></strong>*</h2>
<p>File upload limit is one of the important controls for file upload control, if file upload control is not provided in uploading of file then there can be huge files uploaded which can kill the server cache. There is neither a javascript function which can provide access to client side and get the file size.</p>
<p>This is basically an upgrade to our <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">v1.1 of our file upload system</a>. So now server can check for whether a file is uploaded and can also check the file size. There are 2 steps for limiting the file size, first we have to declare \<httpRuntime> element in our web.config file and then creating a C# code.</p>
<p>The attributes of httpRuntime element are</p>
<div class="highlight"><pre><span></span><code> executionTimeout = "HH:MM:SS"
maxRequestLength = "number"
requireRootedSaveAsPath = "[True|False]"
enable = "[True|False]"
sendCacheControlHeader = "[True|False]"
shutdownTimeout = "HH:MM:SS"
delayNotificationTimeout = "HH:MM:SS"
minFreeThreads = "number"
minLocalRequestFreeThreads = "number"
appRequestQueueLimit = "number"
enableKernelOutputCache = "[True|False]"
enableVersionHeader = "[True|False]"
apartmentThreading = "[True|False]"
waitChangeNotification = "number"
maxWaitChangeNotification = "number"
enableHeaderChecking = "[True|False]"
requestLengthDiskThreshold = "number"
useFullyQualifiedRedirectUrl = "[True|False]"
</code></pre></div>
<h2>Step 1 : Configuring the web.config file.</h2>
<p>The web.config gives us httpRuntime element. The attribute executionTimeout and maxRequestLength is required for this purpose but you can explore other options also. The default size for maxRequestLength is 4MB. Here is the screenshot of web.config file :-</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadT3.1.png"><img alt="FileUploadT3.1" src="//thegeekyway.com/uploads/images/2014/01/FileUploadT3.1.png"></a></p>
</div>
<p>here executionTimeout = "999" means that the file will upload for 999 seconds if the upload time is greater than 999 seconds then the upload will fail and connection will reset, and maxRequestLength = "2097151" means that it will upload max of 2097151KB or 2GB.</p>
<h2>Step 2 : C# coding.</h2>
<p>Since web.config is configured now we have to code the <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> file.<br>
The <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> code is :-</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">checkFileSize</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">true</span><span class="p">;</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">filesize</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">PostedFile</span><span class="o">.</span><span class="n">ContentLength</span><span class="p">;</span>
<span class="w"> </span><span class="n">Label8</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">filesize</span><span class="p">)</span><span class="o">/</span><span class="mi">1024</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"Kb"</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">filesize</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">1048576</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" Everything OK ! File uploaded "</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Green</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Provide</span><span class="w"> </span><span class="n">upload</span><span class="w"> </span><span class="n">code</span><span class="w"> </span><span class="p">(</span><span class="n">discussed</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">part</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">tutorial</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" File size limit exceeded. File size should be less than 1 MB "</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<p>The output of the above C# code is :-</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut3-2.png"><img alt="FileUploadTut3-2" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut3-2.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut3-3.png"><img alt="FileUploadTut3-3" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut3-3.png"></a></p>
</div>
<p>That's all for the FileSize validation. In the next section we will discuss about the FileType or FileExtension validation.</p>
<hr>Asp.net Basics : Uploading a file in asp.net using validations (Part 2 of 5)2014-01-04T18:10:00+05:302014-01-04T18:10:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-04:/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/<p><center>
<u>This is a part 2 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net …</a></p><p><center>
<u>This is a part 2 of the 5 part tutorial.</u><br>
If you are visiting this page for the first time then you must visit part 1 of the tutorial<br>
on how to upload a file on server <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">here</a>
</center></p>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete ASP.Net file uploading using all the Validations one at a time.</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart2.png"><img alt="CoverImageASPNetFileUploadPart2" src="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUploadPart2.png"></a></p>
</div>
<h2><strong><em>*File Upload in asp.net : v1.1 (Empty File Validation)</em></strong>*</h2>
<p>The first validation used is the empty file validation, what if the user doesn't upload a file and simply pres the upload button, here is the output if the user presses only the upload button. Then an exception error will be generated on the server side.</p>
<p>Now since in <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">tutorial 1</a> we discussed on how to upload file to server without any validations now we are using our first validation <strong>Empty File Validation</strong> it checks whether the user has selected the file or not in file control panel or if the user has directly clicked the upload button without selecting the file. If the user has directly clicked the upload button then an exception <strong>System.IO.DirectoryNotFoundException</strong> will be generated.</p>
<p>Here is the image below showing the details of the exception.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/DirectoryNotFoundException.png"><img alt="DirectoryNotFoundException" src="//thegeekyway.com/uploads/images/2014/01/DirectoryNotFoundException.png"></a> <p style="text-decoration: underline; color: #000000;">2 ways of handling this exception</p></p>
</div>
<hr>
<h3>1. Using try-catch mechanism</h3>
<p>Now since DirectoryNotFound Expression is thrown by the server then it can be caught by the try-catch mechanism.<br>
The <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> code for method 1 (try-catch) is :-</p>
<div class="highlight"><pre><span></span><code><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">isFileEmpty</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">true</span><span class="p">;</span>
<span class="w"> </span><span class="n">try</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Everything OK! File Uploaded"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Green</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="n">Provide</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">C</span><span class="c1"># code for uploading the file which was discussed in part 1 of this tutorial</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">catch</span><span class="w"> </span><span class="p">(</span><span class="n">DirectoryNotFoundException</span><span class="w"> </span><span class="n">dnfe</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">_substring</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dnfe</span><span class="o">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_substring</span><span class="o">.</span><span class="n">Substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">72</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">". Please upload a valid file."</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<p>The output of the above code will be following images</p>
<p><strong>Image 1 :</strong> The interface for the upload mechanism<br>
<strong>Image 2 :</strong> Clicking the upload button when no file is selected.<br>
<strong>Image 3 :</strong> Clicking the upload button when a file is selected and uploaded.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-1.png"><img alt="FileUploadTut2-1" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-1.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-3.png"><img alt="FileUploadTut2-2" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-2.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-3.png"><img alt="FileUploadTut2-3" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2-3.png"></a></p>
</div>
<h3>2. Using Boolean property "HasFile"</h3>
<p>Another way of handling this exception is by using the Boolean property "HasFile" which will return the value as true or false. If the value returned is false then you can return the error message else it can continue with the uploading of file.<br>
The <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> code for method 2 (Boolean method) is :-</p>
<div class="highlight"><pre><span></span><code><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">isFileEmpty</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">true</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">HasFile</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Everything OK! File Uploaded"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Green</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="n">Provide</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">C</span><span class="c1"># code for uploading the file which was discussed in part 1 of this tutorial</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//not_success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="s2">"Please upload a valid file."</span><span class="p">;</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2.2-1.png"><img alt="FileUploadTut2.2-1" src="//thegeekyway.com/uploads/images/2014/01/FileUploadTut2.2-1.png"></a></p>
</div>
<p>Thats all for the empty file validations property. In the next part of our tutorial we'll discuss about the SizeValidation( Limiting the size of upload on the server).</p>
<hr>Asp.net Basics : Uploading a file in asp.net using validations (Part 1 of 5)2014-01-04T15:59:00+05:302014-01-04T15:59:00+05:30Deep_Singhtag:thegeekyway.com,2014-01-04:/uploading-a-file-in-asp-part-1/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUpload.png"><img alt="CoverImageASPNetFileUpload" src="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUpload.png"></a></p>
</div>
<hr>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete …</a></p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUpload.png"><img alt="CoverImageASPNetFileUpload" src="//thegeekyway.com/uploads/images/2014/01/CoverImageASPNetFileUpload.png"></a></p>
</div>
<hr>
<p>Contents of this 5 part tutorial<br>
Part 1 : <a href="http://www.thegeekyway.com/uploading-a-file-in-asp-part-1/">Basics of ASP.Net file uploading, uploading a file to a server.</a><br>
Part 2 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-2/">ASP.Net file uploading using EmptyFile Validations.</a><br>
Part 3 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-3/">ASP.Net file uploading using FileSize validation.</a><br>
Part 4 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-a-file-in-asp-net-using-validations-part-4/">ASP.Net file uploading using FileExtension validator.</a><br>
Part 5 : <a href="http://www.thegeekyway.com/asp-net-basics-uploading-file-asp-net-using-validations-part-5/">Complete ASP.Net file uploading using all the Validations one at a time.</a></p>
<p>This is the Part 1 of the 5 part tutorial on how to upload file onto the server using asp.net and validate the file.</p>
<h2><strong>File Upload in asp.net : Basics</strong></h2>
<p>Following controls are required for uploading a file to server using asp.net</p>
<ul>
<li><strong>File upload control :</strong> It will only be used to select file which will be uploaded on server, it will not be used to upload the file.</li>
<li><strong>Button :</strong> To upload a file we need a button, drag and drop the button on the screen from toolbox and change the button name to "Upload File".</li>
<li><strong>A label control :</strong> To display the status of the upload.</li>
</ul>
<h2><strong>File Upload in asp.net : v1.0 (no validations used, the file will be uploaded as it is) </strong></h2>
<p>This is the basic form of file upload, where no validations are being used, user can upload as much large file as possible. But one thing must be noted that this method should be strictly avoided as there is no validations used in this method (validations explained in v1.1 and v1.2) and can be a subject to DDoS attacks (Denial of service) and can shut down the server. This code is just to show how to upload a file and save it on server.</p>
<p>Here is the back end structure I am using on Microsoft Visual Studio 2008.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUpload41.png"><img alt="FileUpload4" src="//thegeekyway.com/uploads/images/2014/01/FileUpload41.png"></a></p>
</div>
<p>Here a file upload control is used which is used to select the file and an upload button which will upload the file. The second row will display the characteristics of the file, whether the file is a ".pdf" or a ".doc" (and other extensions) file and display the image of file type. The second row is being hidden with a panel until the file isnt uploaded.</p>
<p>Let us take a ".pdf" file which will be uploaded on the server under the "uploads" folder. The main code used for uploading a file onto server is :-</p>
<div class="highlight"><pre><span></span><code><span class="o">.</span>
<span class="o">.</span>
<span class="n">FileUpload1</span><span class="o">.</span><span class="n">SaveAs</span><span class="p">(</span><span class="n">Server</span><span class="o">.</span><span class="n">MapPath</span><span class="p">(</span><span class="s2">"~/uploads/"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">));</span>
<span class="o">.</span>
<span class="o">.</span>
</code></pre></div>
<p>Complete explanation of the code :-</p>
<blockquote>
<p><strong>FileUpload1 :</strong> The contol name of the FileUpload in toolbox, administrators can use any file name.</p>
<p><strong>SaveAs</strong> : Its the method found in <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols%28v=vs.110%29.aspx">System.Web.UI.WebControls</a> namespace and in System.Web (System.Web.dll) assembly, its main purpose is to save the file on the server.</p>
<p><strong>MapPath :</strong> This method is used to return the complete physical path for the virtual path that you pass in the method.</p>
</blockquote>
<p>Now let us upload a file on the server for example "SampleFile.pdf" is being uploaded on the server. The output once the file is uploaded is :-</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/FileUpload2.png"><img alt="FileUpload2" src="//thegeekyway.com/uploads/images/2014/01/FileUpload2.png"></a></p>
</div>
<p>The complete cs code for v1.0 is (remember this doesn't use any validations, only retrieves the name and the file extension of the file) :-</p>
<div class="highlight"><pre><span></span><code><span class="n">protected</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">Page_Load</span><span class="p">(</span><span class="n">object</span><span class="w"> </span><span class="n">sender</span><span class="p">,</span><span class="w"> </span><span class="n">EventArgs</span><span class="w"> </span><span class="n">e</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Hiding</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">panel</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="nb">load</span><span class="w"> </span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">false</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">protected</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">Button1_Click</span><span class="p">(</span><span class="n">object</span><span class="w"> </span><span class="n">sender</span><span class="p">,</span><span class="w"> </span><span class="n">EventArgs</span><span class="w"> </span><span class="n">e</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="n">calling</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">valid_file</span><span class="w"> </span><span class="n">method</span><span class="p">,</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">then</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">doesnot</span><span class="w"> </span><span class="n">exist</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">validate</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">validate_file</span><span class="p">();</span>
<span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">validate</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Getting</span><span class="w"> </span><span class="n">all</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">details</span>
<span class="w"> </span><span class="n">GetFileDetails</span><span class="p">();</span><span class="w"> </span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">If</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">exist</span><span class="w"> </span><span class="n">then</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">saved</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">virtual</span><span class="w"> </span><span class="n">directory</span>
<span class="w"> </span><span class="n">SaveFile</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">validate_file</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Code</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">FileUpload1</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="ow">not</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">HasFile</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Panel1</span><span class="o">.</span><span class="n">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">true</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Label2</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"No File Uploaded"</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">called</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="n">namespace</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">Drawing</span>
<span class="w"> </span><span class="n">Label2</span><span class="o">.</span><span class="n">ForeColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb nb-Type">Color</span><span class="o">.</span><span class="n">Red</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">SaveFile</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">File_Upload_Status</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Valid FIle"</span><span class="p">;</span>
<span class="w"> </span><span class="n">Image1</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//success.png"</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="n">Invoking</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">saveas</span><span class="w"> </span><span class="n">method</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">saves</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">directory</span>
<span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">SaveAs</span><span class="p">(</span><span class="n">Server</span><span class="o">.</span><span class="n">MapPath</span><span class="p">(</span><span class="s2">"~/uploads/"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">));</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">GetFileDetails</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Path</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">called</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="n">using</span><span class="w"> </span><span class="n">System</span><span class="o">.</span><span class="n">IO</span><span class="w"> </span><span class="n">namespace</span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">FileExtension</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Path</span><span class="o">.</span><span class="n">GetExtension</span><span class="p">(</span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">);</span>
<span class="w"> </span><span class="nb nb-Type">int</span><span class="w"> </span><span class="n">FileSize</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">PostedFile</span><span class="o">.</span><span class="n">ContentLength</span><span class="p">;</span>
<span class="w"> </span><span class="n">Label11</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FileUpload1</span><span class="o">.</span><span class="n">FileName</span><span class="p">;</span>
<span class="w"> </span><span class="n">Label12</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">FileSize</span><span class="o">/</span><span class="mi">1024</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"KB"</span><span class="p">;</span>
<span class="w"> </span><span class="n">Label13</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FileExtension</span><span class="p">;</span>
<span class="w"> </span><span class="o">//</span><span class="w"> </span><span class="n">Using</span><span class="w"> </span><span class="n">only</span><span class="w"> </span><span class="s2">".pdf"</span><span class="w"> </span><span class="n">extension</span><span class="p">,</span><span class="w"> </span><span class="n">but</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">use</span><span class="w"> </span><span class="n">all</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="n">extensions</span><span class="w"> </span><span class="n">using</span><span class="w"> </span><span class="k">if</span><span class="o">-</span><span class="k">else</span><span class="w"> </span><span class="n">statement</span><span class="o">.</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">FileExtension</span><span class="o">.</span><span class="n">ToLower</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">".pdf"</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Image3</span><span class="o">.</span><span class="n">ImageUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"~//images//pdf.png"</span><span class="p">;</span>
<span class="w"> </span><span class="n">Label14</span><span class="o">.</span><span class="n">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"PDF file"</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<p>We will discuss about our first validation (empty file validation) in our next tutorial.</p>
<hr>CSS Regex selector - Using Regular Expression in CSS2014-01-01T13:53:00+05:302014-01-01T13:53:00+05:30Anooptag:thegeekyway.com,2014-01-01:/css-regex-selector-using-regular-expression-css/<p>Just like JQuery, CSS also offer a great way to filter out elements with selectors using <a href="http://www.w3.org/TR/selectors/#attribute-selectors">Regular Expressions</a>.</p>
<h4>Selecting on Attribute and value</h4>
<p>[[att=str]]{style="font-family: 'courier new', courier;"} :- attribute value is exactly matching to str</p>
<p>[[att*=str] ]{style="font-family: 'courier new', courier;"}:- attribute value contains str - value can …</p><p>Just like JQuery, CSS also offer a great way to filter out elements with selectors using <a href="http://www.w3.org/TR/selectors/#attribute-selectors">Regular Expressions</a>.</p>
<h4>Selecting on Attribute and value</h4>
<p>[[att=str]]{style="font-family: 'courier new', courier;"} :- attribute value is exactly matching to str</p>
<p>[[att*=str] ]{style="font-family: 'courier new', courier;"}:- attribute value contains str - value can contain str anywhere either in middle or at end.</p>
<p>[[att\^=str]]{style="font-family: 'courier new', courier;"} :- attribute value starting with str</p>
<p>[[att\$=str]]{style="font-family: 'courier new', courier;"} :- attribute value ends with str</p>
<p>[att] :- Elements which contain attribute att with any value</p>
<p>Example:</p>
<iframe width="100%" height="350px" src="http://codepen.io/ianoop/embed/nCJdi" scrollbar="none"></iframe>
<h4>Applying CSS with Pseudo-class :not</h4>
<p>This quite interesting using :not selector to filter out on element properties. Using not selector you can distinguish them from other elements. In this selector also you can use regular expressions to find your elements.</p>
<p>For example:</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="k">input</span><span class="err">:</span><span class="ow">not</span><span class="p">(</span><span class="o">[</span><span class="n">DISABLED</span><span class="o">]</span><span class="p">)</span><span class="err">{</span>
<span class="w"> </span><span class="n">background</span><span class="o">-</span><span class="nl">color</span><span class="p">:</span><span class="w"> </span><span class="n">yellow</span><span class="p">;</span>
<span class="w"> </span><span class="err">}</span>
</code></pre></div>
<iframe width="100%" height="250px" src="http://codepen.io/ianoop/embed/AcBfl" scrollbar="none"></iframe>Install HTML5 Capable Browser In Raspberry Pi2013-12-29T10:19:00+05:302013-12-29T10:19:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-29:/install-html5-capable-browser-raspberry-pi/<p>'Web' is the epiphany port available for Raspberry Pi users, which is HTML5 capable browser & it's predicted to be shipped in future Raspbian release. Well, if you're impatient for the official release of 'Web' & wanna try out this awesome browser, here's few steps which will let you enjoy the beta …</p><p>'Web' is the epiphany port available for Raspberry Pi users, which is HTML5 capable browser & it's predicted to be shipped in future Raspbian release. Well, if you're impatient for the official release of 'Web' & wanna try out this awesome browser, here's few steps which will let you enjoy the beta version of 'Web' in your Raspberry Pi.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/2013-12-29-011813_1360x768_scrot.png"><img alt="Rpi_Web_scrot" src="//thegeekyway.com/uploads/images/2013/12/2013-12-29-011813_1360x768_scrot.png"></a></p>
</div>
<p>[For me, if you have noticed, the icons within the browser is not showing up properly, maybe some files are conflicting, for which i will take care later on]</p>
<p>To install 'Web' in Raspberry pi :</p>
<p>Open <strong>/etc/apt/source.list</strong> with <strong>'root'</strong> & add the following line:</p>
<div class="highlight"><pre><span></span><code><span class="k">deb</span><span class="w"> </span><span class="s">http://raspberrypi.collabora.com</span><span class="w"> </span><span class="kp">wheezy</span><span class="w"> </span><span class="kp">web</span>
</code></pre></div>
<p>Now, update the repository & upgrade the system by entering following commands in terminal :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get update && sudo apt-get upgrade
</code></pre></div>
<p>Now install 'Web' browser by entering following commands in terminal :</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install epiphany-browser cgroup-bin libraspberrypi0 libwayland-client0 libwayland-cursor0 libwayland-server0a
</code></pre></div>
<p>Latest kernel supports cgroups (also known as control groups) which limits & isolates the resources like CPU, memory, I/O usages. To activate it, add <strong>cgroup_enable=memory</strong> in Kernel command line.</p>
<p>That's it. Enjoy surfing.</p>Java Reflection: class "Method" to inovoke methods and get metadata2013-12-28T23:57:00+05:302013-12-28T23:57:00+05:30Anooptag:thegeekyway.com,2013-12-28:/java-reflection-class-method/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/java_logo2.jpg"><img alt="Java Logo" src="//thegeekyway.com/uploads/images/2013/12/java_logo2.jpg"></a></p>
</div>
<p>using <strong><em>[java.lang.<a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.html">reflect</a>.<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html">Method</a>]{style="font-family: courier new,courier;"}</em></strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html"> </a>you can get metadata/signature of methods (including private) and can invoke accessible methods at runtime.</p>
<p>Class Method provides a great tool to list all methods and find information about methods. following tutorial will provide you more detail of class …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/java_logo2.jpg"><img alt="Java Logo" src="//thegeekyway.com/uploads/images/2013/12/java_logo2.jpg"></a></p>
</div>
<p>using <strong><em>[java.lang.<a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.html">reflect</a>.<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html">Method</a>]{style="font-family: courier new,courier;"}</em></strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html"> </a>you can get metadata/signature of methods (including private) and can invoke accessible methods at runtime.</p>
<p>Class Method provides a great tool to list all methods and find information about methods. following tutorial will provide you more detail of class Method and invoking a method at runtime.</p>
<ul>
<li><a href="#methodList">Getting List of Methods</a></li>
<li><a href="#metadata">Getting metadata of methods</a></li>
<li><a href="#methodobject">Getting Method Object</a></li>
<li><a href="#invokemethod">Invoke methods at Runtime</a></li>
<li><a href="#ways">Ways of invoking methods at Runtime</a></li>
</ul>
<p #methodList></p>
<h2 id="getting-list-of-method-objects" style="font-size: 1.2em;">Getting List of Method Objects</h2>
<div class="highlight"><pre><span></span><code><span class="o">//</span><span class="n">Getting</span><span class="w"> </span><span class="n">all</span><span class="w"> </span><span class="n">methods</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">java</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">reflect</span><span class="o">.</span><span class="n">Method</span><span class="w"> </span><span class="n">itself</span><span class="o">.</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="n">loading</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">at</span><span class="w"> </span><span class="n">run</span><span class="w"> </span><span class="n">time</span>
<span class="n">Method</span><span class="p">[]</span><span class="w"> </span><span class="n">methodList</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Class</span><span class="o">.</span><span class="n">forName</span><span class="p">(</span><span class="s2">"java.lang.reflect.Method"</span><span class="p">)</span><span class="o">.</span><span class="n">getDeclaredMethods</span><span class="p">();</span>
<span class="o">//</span><span class="n">you</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">also</span><span class="w"> </span><span class="n">use</span><span class="w"> </span><span class="n">object</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">class</span><span class="p">,</span><span class="w"> </span><span class="n">but</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">case</span><span class="w"> </span><span class="n">you</span><span class="w"> </span><span class="n">need</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">create</span><span class="w"> </span><span class="n">an</span><span class="w"> </span><span class="n">object</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="k">class</span>
<span class="n">Method</span><span class="p">[]</span><span class="w"> </span><span class="n">methodList</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">myClass</span><span class="o">.</span><span class="n">getDeclaredMethods</span><span class="p">();</span>
</code></pre></div>
<h2 id="getting-informationmetadata-about-methods" style="font-size: 1.2em;">Getting information/metadata about methods</h2>
<p>Expand following code snippet.</p>
<p>Following code will print whole method signature. Alternatively you can use specific method to get <strong>parameters, returnType</strong> and <strong>exceptions</strong>.</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="ss">(</span><span class="nv">Method</span><span class="w"> </span><span class="nv">method</span><span class="w"> </span>:<span class="w"> </span><span class="nv">methodList</span><span class="ss">)</span>{
<span class="w"> </span><span class="nv">System</span>.<span class="nv">out</span>.<span class="nv">println</span><span class="ss">(</span><span class="nv">method</span>.<span class="nv">toString</span><span class="ss">)</span><span class="c1">; </span>
<span class="w"> </span>}
<span class="nv">Following</span><span class="w"> </span><span class="nv">methods</span><span class="w"> </span><span class="nv">are</span><span class="w"> </span><span class="nv">availble</span><span class="w"> </span><span class="nv">in</span><span class="w"> </span><span class="nv">class</span><span class="w"> </span><span class="nv">java</span>.<span class="nv">lang</span>.<span class="nv">reflect</span>.<span class="nv">Method</span>
<span class="cm">/*</span>
<span class="cm">invoke</span>
<span class="cm">equals</span>
<span class="cm">toString</span>
<span class="cm">hashCode</span>
<span class="cm">getModifiers</span>
<span class="cm">getName</span>
<span class="cm">getAnnotation</span>
<span class="cm">getDeclaredAnnotations</span>
<span class="cm">getDeclaringClass</span>
<span class="cm">getFactory</span>
<span class="cm">getGenericInfo</span>
<span class="cm">getGenericInfo</span>
<span class="cm">getParameterTypes</span>
<span class="cm">getReturnType</span>
<span class="cm">getTypeParameters</span>
<span class="cm">isSynthetic</span>
<span class="cm">toGenericString</span>
<span class="cm">copy</span>
<span class="cm">getGenericSignature</span>
<span class="cm">getGenericParameterTypes</span>
<span class="cm">getParameterAnnotations</span>
<span class="cm">getParameterCount</span>
<span class="cm">isVarArgs</span>
<span class="cm">acquireMethodAccessor</span>
<span class="cm">getAnnotatedReturnType</span>
<span class="cm">getAnnotationBytes</span>
<span class="cm">getDefaultValue</span>
<span class="cm">getExceptionTypes</span>
<span class="cm">getGenericExceptionTypes</span>
<span class="cm">getGenericReturnType</span>
<span class="cm">getMethodAccessor</span>
<span class="cm">handleParameterNumberMismatch</span>
<span class="cm">hasGenericInformation</span>
<span class="cm">isBridge</span>
<span class="cm">isDefault</span>
<span class="cm">setMethodAccessor</span>
<span class="cm">specificToGenericStringHeader</span>
<span class="cm">specificToStringHeader</span>
<span class="cm">*/</span>
</code></pre></div>
<p #methodobject></p>
<h2 id="getting-a-method-object" style="font-size: 1.2em;">getting a Method object</h2>
<div class="highlight"><pre><span></span><code><span class="c1">//Load a class and get method object</span>
<span class="n">Class</span><span class="w"> </span><span class="n">clazz</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Class</span><span class="p">.</span><span class="n">forName</span><span class="p">(</span><span class="s">"theGeekyWay.myClass"</span><span class="p">);</span>
<span class="n">Method</span><span class="w"> </span><span class="n">method</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">clazz</span><span class="p">.</span><span class="n">getMethod</span><span class="p">(</span><span class="s">"myPublicMethod"</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="n">class</span><span class="p">);</span>
<span class="c1">//Note: getMethod throws NoSuchMethodException (if method not available) , SecurityException (if method not accessible)</span>
</code></pre></div>
<p #invokemethod></p>
<h2 id="invoking-methods-at-runtime" style="font-size: 1.2em;">Invoking methods at Runtime</h2>
<p style="font-family: courier new,courier;">public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException,</p>
<p style="font-family: courier new,courier;">InvocationTargetException</p>
<p>Method invoke take variable length parameter, so you need to provide parameter type and number of parameter as calling method.</p>
<div class="highlight"><pre><span></span><code>MyClass myclassobj = new MyClass();
Method method = myclassobj .getMethod("myPublicMethod", String.class);
method.invoke(myclassobj , "invoked a method through reflection");
//invoke method throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
</code></pre></div>
<p>If <strong><em>method is static</em></strong> just pass <strong> <p style="font-family: courier new,courier;">null</p></strong> in place of classObject</p>
<div class="highlight"><pre><span></span><code>Class clazz1 = Class.forName("theGeekyWay.MyClass");
Method method1 = clazz1.getMethod("aStaticMethod"); //No mathod parameter
method1.invoke(null);
//Note: my static method doesn't have any parameter
</code></pre></div>
<p #ways></p>
<h2 id="ways-of-loading-class-at-run-time-and-invoking-method-at-run-time" style="font-size: 1.2em;">Ways of loading class at run time and invoking method at run time</h2>
<div class="highlight"><pre><span></span><code><span class="c1">//invoking a method using 100% Reflection</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Class</span><span class="w"> </span><span class="n">clazz</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Class</span><span class="p">.</span><span class="n">forName</span><span class="p">(</span><span class="s">"theGeekyWay.MyClass"</span><span class="p">);</span>
<span class="w"> </span><span class="n">Method</span><span class="w"> </span><span class="n">method</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">clazz</span><span class="p">.</span><span class="n">getMethod</span><span class="p">(</span><span class="s">"myPublicMethod"</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="n">class</span><span class="p">);</span>
<span class="w"> </span><span class="n">method</span><span class="p">.</span><span class="n">invoke</span><span class="p">((</span><span class="n">theGeekyWay</span><span class="p">.</span><span class="n">MyClass</span><span class="p">)</span><span class="n">clazz</span><span class="p">.</span><span class="n">newInstance</span><span class="p">(),</span><span class="w"> </span><span class="s">"invoked a method through reflection"</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">NoSuchMethodException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InvocationTargetException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">IllegalAccessException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">ClassNotFoundException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">InstantiationException</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">printStackTrace</span><span class="p">();</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<p>Another way to load class at run time and call method</p>
<div class="highlight"><pre><span></span><code> try {
Class clzz = Class.forName("theGeekyWay.MyClass");
((MyClass)clzz.newInstance()).myPublicMethod("hkh........"); // make sure
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
</code></pre></div>
<p>Not sure what exactly the difference between these two ways of invoking method. In both way loading of class happening at runtime. In 1st way invoke is being used while in second ways object.method is being used.</p>How play videos on Raspberry Pi - OMXPlayer2013-12-23T23:55:00+05:302013-12-23T23:55:00+05:30Anooptag:thegeekyway.com,2013-12-23:/play-videos-raspberry-pi-omxplayer/<p>OMX player is a command line player which can be controlled via <a href="#android">Android app</a> or over <a href="#SSH">SSH</a>.</p>
<h2 id="omx-playercan-play-all-common-files-including-.avi-.mp4-and-mkv-without-any-extra-codec-with-subtitle." style="font-size: 1em; font-weight: 300;">OMX player can play all common files including .avi, .mp4 and mkv without any extra codec with subtitle.</h2>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/RaspberryPi_Logo-617x640.jpg" title="Raspberry Pi OMX Player"><img alt="Raspberry Pi OMX Player" src="//thegeekyway.com/uploads/images/2013/12/RaspberryPi_Logo-617x640.jpg"></a></p>
</div>
<!--more-->
<h2 id="installing-omx-player" style="font-size: 1.2em;">Installing OMX Player</h2>
<p>Download and install player from source</p>
<div class="highlight"><pre><span></span><code><span class="n">Stable</span><span class="w"> </span><span class="n">Build</span><span class="w"> </span><span class="p">(</span><span class="n">Already</span><span class="w"> </span><span class="n">added</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">Raspbian …</span></code></pre></div><p>OMX player is a command line player which can be controlled via <a href="#android">Android app</a> or over <a href="#SSH">SSH</a>.</p>
<h2 id="omx-playercan-play-all-common-files-including-.avi-.mp4-and-mkv-without-any-extra-codec-with-subtitle." style="font-size: 1em; font-weight: 300;">OMX player can play all common files including .avi, .mp4 and mkv without any extra codec with subtitle.</h2>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/RaspberryPi_Logo-617x640.jpg" title="Raspberry Pi OMX Player"><img alt="Raspberry Pi OMX Player" src="//thegeekyway.com/uploads/images/2013/12/RaspberryPi_Logo-617x640.jpg"></a></p>
</div>
<!--more-->
<h2 id="installing-omx-player" style="font-size: 1.2em;">Installing OMX Player</h2>
<p>Download and install player from source</p>
<div class="highlight"><pre><span></span><code><span class="n">Stable</span><span class="w"> </span><span class="n">Build</span><span class="w"> </span><span class="p">(</span><span class="n">Already</span><span class="w"> </span><span class="n">added</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">Raspbian</span><span class="w"> </span><span class="n">also</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">ArcLinux</span><span class="p">):</span><span class="w"> </span>
<span class="n">wget</span><span class="w"> </span><span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">downloads</span><span class="o">/</span><span class="n">torarin</span><span class="o">/</span><span class="n">omxplayer_0</span><span class="o">.</span><span class="mf">3.3</span><span class="o">~</span><span class="n">git20131216</span><span class="o">~</span><span class="n">b34143c_armhf</span><span class="o">.</span><span class="n">deb</span>
<span class="n">Experimental</span><span class="w"> </span><span class="n">Build</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">Android</span><span class="w"> </span><span class="n">app</span><span class="w"> </span><span class="n">prefers</span><span class="w"> </span><span class="n">experimental</span><span class="w"> </span><span class="n">build</span><span class="p">)</span>
<span class="n">wget</span><span class="w"> </span><span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">downloads</span><span class="o">/</span><span class="n">torarin</span><span class="o">/</span><span class="n">omxplayer</span><span class="o">/</span><span class="n">omxplayer_3</span><span class="o">~</span><span class="n">experimental</span><span class="o">~</span><span class="n">git20121208</span><span class="o">~</span><span class="n">ebfa6fc6_armhf</span><span class="o">.</span><span class="n">deb</span>
<span class="n">Install</span><span class="p">:</span>
<span class="n">sudo</span><span class="w"> </span><span class="n">dpkg</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="n">mxplayer_0</span><span class="o">.</span><span class="mf">3.3</span><span class="o">~</span><span class="n">git20131216</span><span class="o">~</span><span class="n">b34143c_armhf</span><span class="o">.</span><span class="n">deb</span>
</code></pre></div>
<p>*Note: OMXPlayer will not play MPEG-2 files, you need purchase licence to play. *</p>
<p>{#section style="font-size: 1.2em;"}</p>
<p #android></p>
<h2 id="get-android-app" style="font-size: 1.2em;">Get Android App</h2>
<p><a href="https://play.google.com/store/apps/details?id=com.raspberry.remote&hl=en">Raspberry Pi Remote Control Google Play link</a></p>
<p>Open and connect as shown below</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/RaspberryPi.png"><img alt="RaspberryPi" src="//thegeekyway.com/uploads/images/2013/12/RaspberryPi.png"></a></p>
</div>
<p>In path give <p style="font-family: 'courier new', courier;">/media </p>(Any USB attached to it will be listed here). You can browse like file browser. Even to root. just select the file and click play.</p>
<p><strong>Limitations: </strong>This app works sometime choppy but works well with Play/Stop. If doen't respond just wait for some time and use again. Keep in mind you are playing on card size machine not on high end desktop.</p>
<p #SSH></p>
<h2 id="control-raspberrypi-omxplayer-via-ssh" style="font-size: 1.2em;">Control RaspberryPi / OMXPlayer via SSH</h2>
<p>SSH should be enabled on raspi but if not you can enable it by following.</p>
<p>Run: sudo raspi-config</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/raspi-config.png"><img alt="raspi-config" src="//thegeekyway.com/uploads/images/2013/12/raspi-config.png"></a></p>
</div>
<p>Advance --> SSH and enable it.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/SSH.png"><img alt="SSH" src="//thegeekyway.com/uploads/images/2013/12/SSH.png"></a></p>
</div>
<p>For more info about <a href="http://www.thegeekyway.com/raspberry-pi-change-password-auto-login-remote-desktop/" title="Raspberry Pi: Change Password, Auto Login and Remote Desktop">raspi-config and other configuration, see this tutorial</a>.</p>
<p>Now, on your computer, open terminal Emulator (On Mac/Linux you already have it).</p>
<p>For windows download small software for<a href="http://www.putty.org/">Terminal Emulator, <strong>PuTTy </strong></a>, and open it.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/putty.png"><img alt="putty" src="//thegeekyway.com/uploads/images/2013/12/putty.png"></a></p>
</div>
<p><em>(In host name or ip address, I already given </em><em> <p style="color: #ff0000;"> <p style="font-family: 'courier new', courier;">\<userName>@\<ip> </p></p></em><em>, So I don't have to give username again. it will only asks for password in next step.)</em></p>
<p>Provide your password in next step, and you will land on home of pi in command prompt version. Here you can run any command which will be executed on Raspi directly.</p>
<p>Now just browse to your media file and run</p>
<div class="highlight"><pre><span></span><code><span class="nv">omxplayer</span><span class="w"> </span><span class="o">-</span><span class="nv">s</span><span class="w"> </span>
<span class="k">For</span><span class="w"> </span><span class="nv">HDMI</span><span class="w"> </span><span class="ss">(</span><span class="nv">video</span><span class="o">+</span><span class="nv">audio</span><span class="ss">)</span>
<span class="nv">omxplayer</span><span class="w"> </span><span class="o">-</span><span class="nv">o</span><span class="w"> </span><span class="nv">hdmi</span>
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/omxPlayer-command.png"><img alt="omxPlayer-command" src="//thegeekyway.com/uploads/images/2013/12/omxPlayer-command.png"></a></p>
</div>
<p>Here are some more OMXPlayer commands</p>
<div class="highlight"><pre><span></span><code><span class="nl">http</span><span class="p">:</span><span class="o">//</span><span class="n">lukasz</span><span class="o">-</span><span class="n">skalski</span><span class="p">.</span><span class="n">com</span><span class="o">/</span><span class="k">index</span><span class="p">.</span><span class="n">php</span><span class="o">/</span><span class="n">projekty</span><span class="o">-</span><span class="n">inne</span><span class="o">/</span><span class="n">raspberry</span><span class="o">-</span><span class="n">control</span><span class="o">-</span><span class="n">control</span><span class="o">-</span><span class="n">raspberry</span><span class="o">-</span><span class="nf">pi</span><span class="o">-</span><span class="k">with</span><span class="o">-</span><span class="n">your</span><span class="o">-</span><span class="n">android</span><span class="o">-</span><span class="n">device</span><span class="p">.</span><span class="n">html</span>
<span class="err">#</span><span class="w"> </span><span class="n">dpkg</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="n">omxplayer_0</span><span class="mf">.0.1</span><span class="o">-</span><span class="n">arm</span><span class="p">.</span><span class="n">deb</span>
<span class="err">####</span><span class="w"> </span><span class="p">....</span><span class="w"> </span><span class="n">fails</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="n">missing</span><span class="w"> </span><span class="n">dependencies</span><span class="w"> </span><span class="p">...</span>
<span class="err">#</span><span class="w"> </span><span class="n">apt</span><span class="o">-</span><span class="k">get</span><span class="w"> </span><span class="o">-</span><span class="n">f</span><span class="w"> </span><span class="n">install</span>
<span class="err">###</span><span class="w"> </span><span class="n">fetches</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">missing</span><span class="w"> </span><span class="n">dependencies</span><span class="p">,</span><span class="w"> </span><span class="n">configures</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">sym</span><span class="w"> </span><span class="n">links</span><span class="w"> </span><span class="n">etc</span>
<span class="err">$</span><span class="w"> </span><span class="n">omxplayer</span><span class="w"> </span><span class="o">--</span><span class="n">help</span>
<span class="k">Usage</span><span class="err">:</span><span class="w"> </span><span class="n">omxplayer</span><span class="w"> </span><span class="o">[</span><span class="n">OPTIONS</span><span class="o">]</span><span class="w"> </span><span class="o">[</span><span class="n">FILE</span><span class="o">]</span>
<span class="n">Options</span><span class="w"> </span><span class="err">:</span>
<span class="o">-</span><span class="n">h</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">help</span><span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">help</span>
<span class="o">-</span><span class="n">a</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">alang</span><span class="w"> </span><span class="k">language</span><span class="w"> </span><span class="n">audio</span><span class="w"> </span><span class="k">language</span><span class="w"> </span><span class="err">:</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">g</span><span class="p">.</span><span class="w"> </span><span class="n">ger</span>
<span class="o">-</span><span class="n">n</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">aidx</span><span class="w"> </span><span class="k">index</span><span class="w"> </span><span class="n">audio</span><span class="w"> </span><span class="n">stream</span><span class="w"> </span><span class="k">index</span><span class="w"> </span><span class="err">:</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">g</span><span class="p">.</span><span class="w"> </span><span class="mi">1</span>
<span class="o">-</span><span class="n">o</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">adev</span><span class="w"> </span><span class="n">device</span><span class="w"> </span><span class="n">audio</span><span class="w"> </span><span class="k">out</span><span class="w"> </span><span class="n">device</span><span class="w"> </span><span class="err">:</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">g</span><span class="p">.</span><span class="w"> </span><span class="n">hdmi</span><span class="o">/</span><span class="k">local</span>
<span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">info</span><span class="w"> </span><span class="k">dump</span><span class="w"> </span><span class="n">stream</span><span class="w"> </span><span class="nf">format</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="k">exit</span>
<span class="o">-</span><span class="n">s</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">stats</span><span class="w"> </span><span class="n">pts</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="n">stats</span>
<span class="o">-</span><span class="n">p</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">passthrough</span><span class="w"> </span><span class="n">audio</span><span class="w"> </span><span class="n">passthrough</span>
<span class="o">-</span><span class="n">d</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">deinterlace</span><span class="w"> </span><span class="n">deinterlacing</span>
<span class="o">-</span><span class="n">w</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">hw</span><span class="w"> </span><span class="n">hw</span><span class="w"> </span><span class="n">audio</span><span class="w"> </span><span class="n">decoding</span>
<span class="o">-</span><span class="mi">3</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="mi">3</span><span class="n">d</span><span class="w"> </span><span class="n">switch</span><span class="w"> </span><span class="n">tv</span><span class="w"> </span><span class="k">into</span><span class="w"> </span><span class="mi">3</span><span class="n">d</span><span class="w"> </span><span class="n">mode</span>
<span class="o">-</span><span class="n">y</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">hdmiclocksync</span><span class="w"> </span><span class="n">adjust</span><span class="w"> </span><span class="n">display</span><span class="w"> </span><span class="n">refresh</span><span class="w"> </span><span class="n">rate</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">video</span>
<span class="o">-</span><span class="n">t</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="o">--</span><span class="n">sid</span><span class="w"> </span><span class="k">index</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="n">subtitle</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="k">index</span>
<span class="o">-</span><span class="n">r</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="c1">--refresh adjust framerate/resolution to video</span>
</code></pre></div>
<p>Play via SSH and feel like a pro.</p>Vagrant up And Beyond!2013-12-23T16:28:00+05:302013-12-23T16:28:00+05:30Peeyush_Guptatag:thegeekyway.com,2013-12-23:/vagrant-beyond-2/<p>When you are working on a project with a team, a big question is how to make them work on same environment? One solution is to make them work on same machine, but this gets messy as everyone is making changes. The other solution is to provide everyone with same …</p><p>When you are working on a project with a team, a big question is how to make them work on same environment? One solution is to make them work on same machine, but this gets messy as everyone is making changes. The other solution is to provide everyone with same environment. And that’s where Vagrant comes! In this tutorial we will learn how to use Vagrant.</p>
<p>Vagrant gives you portable work environments which can be controlled by a pre-defined workflow. The idea is when you are working on a project and there are a lot of dependencies, configuration settings, scripts involved, you can combine them all up with vagrant and pass it around to be used by everyone. Let’s start with getting a VM up and running.</p>
<p>Though Vagrant comes with default support for VirtualBox, it can be used with other providers too like VMware, AWS etc. Here I am using VirtualBox on Fedora 19 as provider. You can download and install VirtualBox from <a href="https://www.virtualbox.org/wiki/Downloads">here</a>. Next step is to install Vagrant, which you can get from <a href="http://www.vagrantup.com/">here</a>.</p>
<p>Now, after installing VirtualBox and Vagrant, you can add the box and run the machine. We will learn more about boxes, first run these two commands:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>vagrant<span class="w"> </span>init<span class="w"> </span>precise32<span class="w"> </span>http://files.vagrantup.com/precise32.box
$<span class="w"> </span>vagrant<span class="w"> </span>up
</code></pre></div>
<p>It will take some time to set everything up and running the virtual machine. Now you can simply do:</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>vagrant<span class="w"> </span>ssh
</code></pre></div>
<p>and you will be able to login in your virtual machine. Coming back to boxes, they are nothing but the base images, which are used to clone a virtual machine. This makes the process faster instead of running a machine from scratch. You can also add new boxes to the vagrant environment.</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>vagrant<span class="w"> </span>box<span class="w"> </span>add<span class="w"> </span>precise32<span class="w"> </span>http://files.vagrantup.com/precise32.box
</code></pre></div>
<p>So, how do you control the whole environment? Well, that’s why we have a Vagrantfile! The primary objective of a Vagrantfile is to describe the type, attributes etc. of the virtual machine. There is one Vagrantfile per project and it takes care of all the installation, script execution etc. When you do “vagrant init” in a directory, a Vagrantfile is placed there. Now, you can use this Vagrantfile to launch virtual machines from this directory. If you want to do it from another directory, you will first have to do “vagrant init” there or you would have to copy Vagrantfile in that directory.</p>
<p>Vagrantfile can be ported from one platform to another. It is written in Ruby, but you don’t need to learn Ruby to specify attributes through it. Let’s say, after launching a machine, you want to run a shell script on it. The content of the Vagrantfile would be:</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Screenshot-from-2013-12-23-152311.png"><img alt="Screenshot from 2013-12-23 15:23:11" src="//thegeekyway.com/uploads/images/2013/12/Screenshot-from-2013-12-23-152311.png"></a></p>
</div>
<p>As you can see here, the name of the box and machine is fedora1 and I have given the provisioner as shell and the script that is going to run (after the virtual machine is booted) is bootstrap.sh. Here bootstrap.sh is present in the same directory, you can also give the absolute./relative path to you shell script. You can modify the Vagrant file and use</p>
<div class="highlight"><pre><span></span><code><span class="o">$</span><span class="w"> </span><span class="n">vagrant</span><span class="w"> </span><span class="n">reload</span>
</code></pre></div>
<p>to reflect the changes in already running virtual machine.You only need to learn how to assign values to different variables to make new Vagrantfiles. This should be enough to get you started. To check out their documentation, please visit the official website <a href="http://www.vagrantup.com">here</a>.</p>Arduino Uno Pin Functions2013-12-23T10:23:00+05:302013-12-23T10:23:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-23:/arduino-uno-pin-functions/<p>Arduino Uno's Pin labelling is displayed in image below, and detailed function of each & every pin is mentioned below the image.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ArduinoUno_R3_Front.jpg"><img alt="Arduino Pin Functions" src="//thegeekyway.com/uploads/images/2013/12/ArduinoUno_R3_Front.jpg"></a></p>
</div>
<p><u><strong>PIN FUNCTIONS</strong></u> <strong>:<br>
</strong></p>
<p><strong>USB To PC :</strong> It is used to communicate Arduino via Universal Serial Bus to PC for programming/sending data to Arduino serially.</p>
<p><strong>7V to 12V DC Input …</strong></p><p>Arduino Uno's Pin labelling is displayed in image below, and detailed function of each & every pin is mentioned below the image.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ArduinoUno_R3_Front.jpg"><img alt="Arduino Pin Functions" src="//thegeekyway.com/uploads/images/2013/12/ArduinoUno_R3_Front.jpg"></a></p>
</div>
<p><u><strong>PIN FUNCTIONS</strong></u> <strong>:<br>
</strong></p>
<p><strong>USB To PC :</strong> It is used to communicate Arduino via Universal Serial Bus to PC for programming/sending data to Arduino serially.</p>
<p><strong>7V to 12V DC Input :</strong> For external supply, the voltage range of 7V to 12V DC is recommended. The 9V battery can be used to power your Arduino Uno board.</p>
<p><strong>Reset Button :</strong> It Resets the Arduino board if pressed.</p>
<p><strong>ICSP :</strong> Abbreviated as <em>In Circuit Serial Programming</em> which consists of MOSI, MISO, SCK, RESET, VCC, GND. It is either used to program USB or Microcontroller (For UNO, ATmega 328P-PU). ICSP allows the user to program the microcontroller when it is in circuits, In Arduino UNO it allows to program ATmega328P-PU directly with AVR instructions without using Arduino IDE.</p>
<p><strong>SDA :</strong> Serial Data, It is the bidirectional data line that is used by I2C.</p>
<p><strong>SCL :</strong> Serial Clock, It is used to indicate that data is ready on bidirectional data line that is used by I2C.</p>
<p><strong>AREF :</strong> Analog Read Reference, It is mainly used for analogReference() function calls, as default it is not required but to use it you have to add some voltage source between 0V to 5V in AREF Pin which will be considered as accurate reference voltage.</p>
<p><strong>GND :</strong> Ground.</p>
<p><strong>SCK :</strong> Serial Clock, which is used by SPI (Serial Peripheral Interface). It is clock generated by 'Master' which is used to clock the data to the 'Slave'.</p>
<p><strong>MOSI :</strong> Master Out Slave In, The data is transmitted from Master to Slave. (Master -> Slave)</p>
<p><strong>MISO :</strong> Master In Slave Out, The data is transmitted to Master from Slave. (Slave -> Master)</p>
<p><strong>SS :</strong> Slave Select, It is used to select the 'Slave'. Make high to SS pin to deactivate & make low to activate it.</p>
<p><strong>INT1 & INT0 :</strong> These are hardware interrupts, it calls the ISR (Interrupt Service Routine) when the pin change occurs.</p>
<p><strong>TX :</strong> Transmit, It is used to transmit TTL serial data. It is also referred as 'outwards' since it 'transmits' data from Arduino to other connected peripheral device.</p>
<p><strong>RX :</strong> Receive, It is used to receive TTL serial data. IT is also referred as 'inwards' since it 'receives' data from external hardware to Arduino.</p>
<p><strong>Vin :</strong> Voltage In, If you're powering your Arduino board from USB nothing is obtained from Vin pin. But, if you're powering the board with external supply then that supply is directly obtained from Vin pin. However, the supply obtained at Vin pin is usually lesser by 1V than voltage supplied to Power pin due to reverse polarity protection diode.</p>
<p><strong>5V Pin :</strong> It is used to power external components connected to Arduino which needs 5V.</p>
<p><strong>3.3V Pin :</strong> It is used to power external components connected to Arduino which needs 3.3V</p>
<p><strong>IORef :</strong> Input Output Voltage Reference, It allows shields connected to Arduino board to check whether the board is running at 3.3V or 5V.</p>Raspberry Pi: Change Password, Auto Login and Remote Desktop2013-12-22T00:56:00+05:302013-12-22T00:56:00+05:30Anooptag:thegeekyway.com,2013-12-22:/raspberry-pi-change-password-auto-login-remote-desktop/<p>I have already given tutorial for <a href="http://www.thegeekyway.com/raspberry-pi-how-to-install-and-first-boot/" title="Installation and first boot">RaspberryPi First Installation and Boot</a>. Let's go with advance configuration of RasPi.</p>
<h1 id="things-to-do-after-1st-boot-of-raspberry-pi" style="font-size: 1em;">Things to do after 1st boot of Raspberry Pi</h1>
<ul>
<li><a href="#changePass">Changing Default Password</a></li>
<li><a href="#AddUser">Adding New User to Raspi</a></li>
<li><a href="#autoLogin">Auto Login to GUI without Entering username/password</a></li>
<li><a href="#Remote">Setting up Remote Desktop: Connect Raspi from …</a></li></ul><p>I have already given tutorial for <a href="http://www.thegeekyway.com/raspberry-pi-how-to-install-and-first-boot/" title="Installation and first boot">RaspberryPi First Installation and Boot</a>. Let's go with advance configuration of RasPi.</p>
<h1 id="things-to-do-after-1st-boot-of-raspberry-pi" style="font-size: 1em;">Things to do after 1st boot of Raspberry Pi</h1>
<ul>
<li><a href="#changePass">Changing Default Password</a></li>
<li><a href="#AddUser">Adding New User to Raspi</a></li>
<li><a href="#autoLogin">Auto Login to GUI without Entering username/password</a></li>
<li><a href="#Remote">Setting up Remote Desktop: Connect Raspi from another computer</a></li>
</ul>
<p #changePass></p>
<h2 id="changing-default-password" style="font-size: 1.3em;">1. Changing Default Password</h2>
<p>Open terminal and run following command</p>
<div class="highlight"><pre><span></span><code>sudo raspi-config
</code></pre></div>
<p>You will see following screen. Navigate using <p style="font-family: 'courier new', courier;">arrow keys</p> and <p style="font-family: 'courier new', courier;">tab.</p> Hit <p style="font-family: 'courier new', courier;"> Enter</p> to select .</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/raspi-config.png"><img alt="raspi-config" src="//thegeekyway.com/uploads/images/2013/12/raspi-config.png"></a></p>
</div>
<p>On selecting <p style="font-family: 'courier new', courier;">Change User Password</p> , Hit <p style="font-family: 'courier new', courier;">Enter</p> to give new password.</p>
<p #AddUser></p>
<h2 id="adding-new-user-to-pi" style="font-size: 1.3em;">2. Adding new user to Pi:</h2>
<p>To add new user just run following command</p>
<div class="highlight"><pre><span></span><code>sudo adduser
</code></pre></div>
<p>It will create a new user and asks for password, Create password for new user here.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/raspi-addUser.png"><img alt="raspi-addUser" src="//thegeekyway.com/uploads/images/2013/12/raspi-addUser.png"></a></p>
</div>
<p #autoLogin></p>
<h2 id="raspi-auto-login-to-gui-without-entering-usernamepassword" style="font-size: 1.3em;">3. Raspi Auto Login to GUI without entering username/password</h2>
<p>Run again following command and select <p style="font-family: 'courier new', courier;">Enable boot to Desktop/S</p> <p style="font-family: 'courier new', courier;">cratch and select 2nd option</p></p>
<div class="highlight"><pre><span></span><code>sudo raspi-config
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Auto-Login-GUI.png"><img alt="Auto-Login-GUI" src="//thegeekyway.com/uploads/images/2013/12/Auto-Login-GUI.png"></a></p>
</div>
<h5 id="auto-login-using-command-version" style="font-size: 1em;">Auto-Login using Command version</h5>
<div class="highlight"><pre><span></span><code><span class="nv">sudo</span><span class="w"> </span><span class="nv">nano</span><span class="w"> </span><span class="o">/</span><span class="nv">etc</span><span class="o">/</span><span class="nv">inittab</span>
<span class="cm">/*Find following line*/</span>
<span class="mi">1</span>:<span class="mi">2345</span>:<span class="nv">respawn</span>:<span class="o">/</span><span class="nv">sbin</span><span class="o">/</span><span class="nv">getty</span><span class="w"> </span><span class="mi">115200</span><span class="w"> </span><span class="nv">tty1</span>
<span class="cm">/*and comment it by adding # in front of it*/</span>
<span class="sc">#1</span>:<span class="mi">2345</span>:<span class="nv">respawn</span>:<span class="o">/</span><span class="nv">sbin</span><span class="o">/</span><span class="nv">getty</span><span class="w"> </span><span class="mi">115200</span><span class="w"> </span><span class="nv">tty1</span>
<span class="cm">/*After that add new line with following content*/</span>
<span class="mi">1</span>:<span class="mi">2345</span>:<span class="nv">respawn</span>:<span class="o">/</span><span class="nv">bin</span><span class="o">/</span><span class="nv">login</span><span class="w"> </span><span class="o">-</span><span class="nv">f</span><span class="w"> </span><span class="nv">pi</span><span class="w"> </span><span class="nv">tty1</span><span class="w"> </span><span class="o">/</span><span class="nv">dev</span><span class="o">/</span><span class="nv">tty1</span><span class="w"> </span><span class="mi">2</span><span class="o">>&</span><span class="mi">1</span>
<span class="cm">/*Ctrl+X to exit, Y to save followed by enter twice*/</span>
</code></pre></div>
<h5 id="auto-start-to-gui" style="font-size: 1em;">Auto Start to GUI</h5>
<div class="highlight"><pre><span></span><code><span class="o">/*</span><span class="n">Edit</span><span class="w"> </span><span class="n">rc</span><span class="p">.</span><span class="n">local</span><span class="w"> </span><span class="n">using</span><span class="w"> </span><span class="n">following</span><span class="w"> </span><span class="n">line</span><span class="o">*/</span>
<span class="n">sudo</span><span class="w"> </span><span class="n">nano</span><span class="w"> </span><span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">rc</span><span class="p">.</span><span class="n">local</span>
<span class="o">/*</span><span class="n">Scroll</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="nb">and</span><span class="w"> </span><span class="n">add</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">following</span><span class="w"> </span><span class="n">above</span><span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="mi">0</span><span class="p">:</span><span class="o">*/</span>
<span class="n">su</span><span class="w"> </span><span class="o">-</span><span class="n">l</span><span class="w"> </span><span class="n">pi</span><span class="w"> </span><span class="o">-</span><span class="n">c</span><span class="w"> </span><span class="n">startx</span>
<span class="o">/*</span><span class="n">Ctrl</span><span class="o">+</span><span class="n">X</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="nb">exit</span><span class="p">,</span><span class="w"> </span><span class="n">Y</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="nb">save</span><span class="w"> </span><span class="n">followed</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="n">enter</span><span class="w"> </span><span class="n">twice</span><span class="o">*/</span>
</code></pre></div>
<p #Remote></p>
<h2 id="raspi-setup-remote-desktop-from-windows-machine" style="font-size: 1.3em;">4. Raspi Setup Remote Desktop from windows machine</h2>
<p>Step 1: In Respi open terminal and run following command</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install xrdp
</code></pre></div>
<p>It will install <p style="font-family: 'courier new', courier;">xrdp on respi, which will be automatically started on boot up. </p>After installing xrdp, reboot your pi and you can verify in boot process, you can see following line</p>
<ul>
<li><p style="font-family: 'courier new', courier;">xrdp sesman</p>*</li>
</ul>
<p>Step2: Open Remote control application from windows (Run --> mstsc) , Enter local IP of respi and proceed.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/MS-RemoteDesktop.png"><img alt="MS-RemoteDesktop" src="//thegeekyway.com/uploads/images/2013/12/MS-RemoteDesktop.png"></a></p>
</div>
<p>After successful connection xVnc will ask to authorize at raspberry pi</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Respi-Sesman-xvnc.png"><img alt="Respi-Sesman-xvnc" src="//thegeekyway.com/uploads/images/2013/12/Respi-Sesman-xvnc.png"></a></p>
</div>
<p>On successful login to xVnc you will see following screen and then Raspberry pi on your another machine</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/raspi-remote-login.png"><img alt="raspi-remote-login" src="//thegeekyway.com/uploads/images/2013/12/raspi-remote-login.png"></a></p>
</div>
<h2 id="control-raspberry-pi-over-ssh-see-this-tutorial" style="font-size: 1.3em;"><a href="http://www.thegeekyway.com/play-vidoes-raspberry-pi-omxplayer/#SSH" title="http://www.thegeekyway.com/play-vidoes-raspberry-pi-omxplayer/#SSH">Control Raspberry Pi over SSH: See this Tutorial</a></h2>CSS3 Animations: Getting started2013-12-21T00:34:00+05:302013-12-21T00:34:00+05:30Anooptag:thegeekyway.com,2013-12-21:/css3-animations-getting-started/<p>CSS animations is a great tool design website in very cool fashion without any gif images and reduced page size.<br>
~(Keep\ in\ mind\ IE8\ and\ IE9\ are\ stone\ age\ browsers\ and\ CSS3\ animation\ won't\ work\ with\ it).~<br>
There are two basic keyword used to generate animations using <p style="font-family: 'courier new', courier;">animation</p> and <p style="font-family: 'courier new', courier;"> \@keyframes …</p></p><p>CSS animations is a great tool design website in very cool fashion without any gif images and reduced page size.<br>
~(Keep\ in\ mind\ IE8\ and\ IE9\ are\ stone\ age\ browsers\ and\ CSS3\ animation\ won't\ work\ with\ it).~<br>
There are two basic keyword used to generate animations using <p style="font-family: 'courier new', courier;">animation</p> and <p style="font-family: 'courier new', courier;"> \@keyframes</p> .<!--more--></p>
<p>Example: Animation Demo </p>
<iframe width="100%" height="350px" src="http://codepen.io/ianoop/embed/ndvrm" scrollbar="none"></iframe>
<ul>
<li><p style="font-family: 'courier new', courier;"><strong>animation: \<animationName> \<animationTime> \<numberOfAnimation></strong> :</p><em> Defined as class property<br>
</em><strong> <p style="font-family: 'courier new', courier;">\@keyframes :</p></strong>* A Rule which define your animation.</li>
</ul>
<p>Note: you need to add <strong><em>-webkit-</em></strong> suffix to above properties in order to work in Chrome and safari.</p>
<p>One more a simple CSS3 loader like Facebook loading </p>
<iframe width="100%" height="350px" src="http://codepen.io/ianoop/embed/LeqAh" scrollbar="none"></iframe>
<h5>How does it works:</h5>
<p>See in HTML and CSS tab, there is 3 divs and only one <p style="font-family: courier new,courier;">\@keyframes </p>rule has been applied. Only difference is delay in animation.</p>
<p style="font-family: courier new,courier;">Infinite</p>
<p>property allowing this animation to run in infinite loop.</p>
<p>One more thing is noticeable is <p style="font-family: courier new,courier;">alternate</p> property in <p style="font-family: courier new,courier;">animation</p>. watch carefully 2st div it is completing animation and then again going in reverse direction which is making this animation smooth.</p>
<p>Note: Infinite animation is not working after embedding. Please click on Edit this pen to open at CodePen.</p>Raspberry Pi - How to install and first boot2013-12-18T23:01:00+05:302013-12-18T23:01:00+05:30Anooptag:thegeekyway.com,2013-12-18:/raspberry-pi-how-to-install-and-first-boot/<p>Installing and setting up <a href="http://www.raspberrypi.org/">Raspberry Pi</a> is fairly easy in following steps</p>
<ul>
<li>Buy RasPi from <a href="www.element14.com">Element14</a> / (<a href="http://www.crazypi.com/">CrazyPi</a>in India)</li>
<li>Get a SD card (>4GB) and format in FAT32 system (<a href="#windowsformat">Windows Method</a> / <a href="#LinuxFormat">Linux Method</a>)</li>
<li>Download <a href="http://www.raspberrypi.org/downloads">NOOB version of Raspberry Pi from here</a></li>
<li>Extract and copy everything in your SD card</li>
<li>Plug …</li></ul><p>Installing and setting up <a href="http://www.raspberrypi.org/">Raspberry Pi</a> is fairly easy in following steps</p>
<ul>
<li>Buy RasPi from <a href="www.element14.com">Element14</a> / (<a href="http://www.crazypi.com/">CrazyPi</a>in India)</li>
<li>Get a SD card (>4GB) and format in FAT32 system (<a href="#windowsformat">Windows Method</a> / <a href="#LinuxFormat">Linux Method</a>)</li>
<li>Download <a href="http://www.raspberrypi.org/downloads">NOOB version of Raspberry Pi from here</a></li>
<li>Extract and copy everything in your SD card</li>
<li>Plug your HDTV/Monitor using HDMI cable / RCA Cable</li>
<li>Plug the power using any micro USB mobile charger (5v , 0.7 A)</li>
</ul>
<h5><strong> Components of Raspberry-Pi</strong></h5>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/RaspberryPi.jpg"><img alt="RaspberryPi" src="//thegeekyway.com/uploads/images/2013/12/RaspberryPi.jpg"></a></p>
</div>
<p><em><u></em><em>Preparing your SD card for installing Linux variant for RasPi.</em><em></u></em></p>
<h5><p #LinuxFormat></p>Linx Method:</h5>
<p>Install <a href="http://gparted.org/">Gparted utility</a> using following command</p>
<ul>
<li><p style="font-family: 'courier new', courier;">sudo apt-get install gparted</p>*</li>
</ul>
<p>and Run</p>
<ul>
<li><p style="font-family: 'courier new', courier;">sudo gparted</p>*</li>
</ul>
<p>You should be able to see following screen and just select your SD card and format to FAT32.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/gParted.png"><img alt="gParted" src="//thegeekyway.com/uploads/images/2013/12/gParted.png"></a></p>
</div>
<h5><p #windowsformat></p>Windows Method:</h5>
<p>Use <a href="https://www.sdcard.org/downloads/formatter_4/">SDFormatter</a>and select your SD drive and just go for format.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Capture1.png"><img alt="SD Formatter" src="//thegeekyway.com/uploads/images/2013/12/Capture1.png"></a></p>
</div>
<p>After formatting just extract downloaded<a href="http://www.raspberrypi.org/downloads">NOOB version</a>and copy to your SD card.</p>
<p>Now plugin your RasPi to Tv/Monitor using HDMI or RCA Video cable. To connect VGA monitor you can use <em><a href="http://www.thegeekyway.com/connect-raspberry-pi-vga-monitortv/">converter given in this tutorial</a></em></p>
<p><a href="http://www.thegeekyway.com/connect-raspberry-pi-vga-monitortv/">Power with *micro usb mobile charger *</a></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/microUSB.jpg"><img alt="microUSB" src="//thegeekyway.com/uploads/images/2013/12/microUSB.jpg"></a></p>
</div>
<p>Note: Please check your charger specification it should be 5 V and ampere range between 0.7-1.2A.</p>
<p>Power on your charger and you should be able to see following screen after some time</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Raspi_Options.png"><img alt="Raspi_Options" src="//thegeekyway.com/uploads/images/2013/12/Raspi_Options-1024x970.png"></a></p>
</div>
<p>Select Raspbian as recommended and proceed.</p>
<p>Note down default <p style="font-family: 'courier new', courier;">user/password : pi/raspberry</p></p>
<p>Note: If you go ahead with default option you will be in command line without any GUI. to change into GUI you just have to run command <strong><em> <p style="font-family: 'courier new', courier; color: #0000ff;">startx </p></em></strong>and you will get your GUI desktop.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/raspi.png"><img alt="raspi" src="//thegeekyway.com/uploads/images/2013/12/raspi.png"></a></p>
</div>
<p><strong>Note:</strong> Raspi has only two USB port , you should be using it for keyboard and mouse for initial setup.</p>
<p><strong>SD Card</strong>: Faster SD card is recommended, should be Class 10.</p>OpenStack - Understanding The Architecture2013-12-18T15:04:00+05:302013-12-18T15:04:00+05:30Peeyush_Guptatag:thegeekyway.com,2013-12-18:/openstack-understanding-architecture/<p>OpenStack is a cloud computing project to provide an Infrastructure as a Service(IaaS), jointly launched by RackSpace and NASA. A collection of open-source technologies combined together make it a massively scalable cloud operating system. One big quality of OpenStack is it’s simplicity, anyone can easily tie-up different hosts …</p><p>OpenStack is a cloud computing project to provide an Infrastructure as a Service(IaaS), jointly launched by RackSpace and NASA. A collection of open-source technologies combined together make it a massively scalable cloud operating system. One big quality of OpenStack is it’s simplicity, anyone can easily tie-up different hosts and provide cloud storage. Openstack has become a big phenomenon in past three years since its launch in 2010. It has its reach to almost every tech giant with more than 10,000 developers working on it.</p>
<p>Before we start discussing anything, the very first thing is to deploy OpenStack. Yes, it is that easy! You can deploy it without any knowledge of underlying architecture. Go to <a href="http://www.devstack.org">Devstack</a>, run the script and you are done!</p>
<p>Now, the code of OpenStack is quite large, with more than 1.5 million lines of code. Fortunately, it has a good modular design. In this article, I would be discussing about the architecture of OpenStack. OpenStack has a modular architecture and is divided into 7 components. We will discuss them one by one. The name in the parentheses are the code-names for respective services.</p>
<p><strong>1) Openstack Compute(Nova)</strong>: It is the central brain unit of the system. Nova is responsible to get the requests from user, choosing an appropriate host and running the instance on that host. It is a big component which uses the underlying hypervisor technology like KVM, qemu etc. It is designed to manage and automate pools of computer resources. Nova is also further divided into different parts including nova-volume, nova-network, nova-scheduler etc.</p>
<p><strong>2) OpenStack Object Storage(Swift)</strong>: Swift is a scalable redundant storage system. It is responsible for keeping the data safe. Swift is a reliable data storage that maintains a number of replicas of the data and also ensures integrity. New storage can be easily added using swift.</p>
<p><strong>3) OpenStack Block Storage(Cinder)</strong>: When user spawns an instance, she also needs some persistent block storage to work with. Here comes the cinder! It manages the creation, attaching and detaching of the block devices to servers. It can also be used to creates snapshots for backing up the data.</p>
<p><strong>4) OpenStack Networking(Neutron)</strong>: Neutron (formerly known as Quantum), is responsible for managing networks and IP addresses. It can manage the networks in three ways: using flat network manager, flat DHCP network manager or VLAN network manager. Neutron is capable of providing both fixed and flexible IPs to the instances.</p>
<p><strong>5)</strong> <strong>OpenStack Dashboard(Horizon)</strong>: Horizon provides administrators and users a graphical interface to access, provision and automate cloud-based resources. This web-based dashboard has a lot of features that make launching of an instance and administrating an easy task.</p>
<p><strong>6)</strong> <strong>OpenStack Identity (Keystone)</strong>: Keystone is the one stop point of authentication. To access some features, or to initiate some tasks, users/services needs to authenticate themselves first. The idea behind keystone is to make the authentication process centralized. No matter the user, the admin or any project, everyone has to go through keystone first. Keystone supports several modes of authentication including the standard username-password matching and token based authentication.</p>
<p><strong>7)</strong> <strong>OpenStack Image Service(Glance)</strong>: Glance is a service dedicated to the operations of disk and server images. It provides discovery, registration and delivery services for the images. Glance supports several formats like qcow2, vmdk etc. It can store the images in any of the back-ends like OpenStack Object Storage.</p>Learn C - More about variables2013-12-16T11:51:00+05:302013-12-16T11:51:00+05:30sanyam_khuranatag:thegeekyway.com,2013-12-16:/learn-c-variables/<style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Welcome to your next tutorial on the Learn C language, here we'll have a deeper look at the use of variables in C language. In previous <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">tutorial about variables in C</a>, we defined a variable sam of integer type and then initialized it with …</p><style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Welcome to your next tutorial on the Learn C language, here we'll have a deeper look at the use of variables in C language. In previous <a href="http://www.thegeekyway.com/variables-in-c/" title="Learning about Variables in C">tutorial about variables in C</a>, we defined a variable sam of integer type and then initialized it with a value (10), this can be done in simpler manner in just one line. If you haven't read the previous tutorial, then please read it first, and you can read the <a href="http://www.thegeekyway.com/category/programming/c/">Learn C tutorial Series</a> .</p>
<p><strong>Declaration and Initialization together :</strong></p>
<p>So, we'll have a look at how to do this :</p>
<div class="highlight"><pre><span></span><code>int sam = 10;
</code></pre></div>
<p>In the above statement, I have declared my integer type variable named sam, and then initialised it with a value.</p>
<p>So, I had two options, one to do something like mentioned above, or have declaration in one line and initialization in other as :</p>
<div class="highlight"><pre><span></span><code>int sam;
sam = 10;
</code></pre></div>
<p>Now, we'll see how to take values from our user</p>
<p><strong>Accepting values from user :</strong></p>
<p>We can store values in our variables depending on choice of end user. This is done through a function know as scanf() and is defined under header file stdio.h (Standard Input/Output). We first define a variable named value.</p>
<div class="highlight"><pre><span></span><code>int value;
</code></pre></div>
<p>Now, we'll accept input form user as :</p>
<div class="highlight"><pre><span></span><code>scanf(“%d”, &value);
</code></pre></div>
<p>Note : scanf() function is somewhat like printf function defined before, but with the function name it's necessary to have ampersand (&) before variable name. This operator is used for reference, in simple terms, this helps in passing the address of memory location that is reserved for our new variable. We'll discuss about these things in detail in later tutorial, till now, just see it's done in this way.</p>
<p>Note: scanf() function should always contain ampersand before your variable name.</p>
<p>Now, we'll write a program here which will accept an input from user and then print the same on the screen.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">value</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Enter</span><span class="w"> </span><span class="nv">value</span><span class="w"> </span>:<span class="w"> </span>“<span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">scanf</span><span class="ss">(</span>“<span class="o">%</span><span class="nv">d</span>”<span class="w"> </span>,<span class="w"> </span><span class="o">&</span><span class="nv">value</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Value</span><span class="w"> </span><span class="nv">entered</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="o">%</span><span class="nv">d</span>”,<span class="nv">value</span><span class="ss">)</span><span class="c1">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>The above code snippet would produce the following output :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/C_TUT_2.1.png" title="Output for learning about variables in C"><img alt="Learn C variables Program Output" src="//thegeekyway.com/uploads/images/2013/12/C_TUT_2.1.png"></a></p>
</div>
<p>Note: Output compiled in Turbo C with DosBox for Windows, rather than gcc.</p>
<p>Note here, that whatever value end user enters, the same is printed on the screen.</p>
<p><strong>Accepting user Input and displaying output for variables of different data types :</strong></p>
<p>Now, we'll see how you can accept and return different types of variables :</p>
<p>For character type variable , you can do as :</p>
<div class="highlight"><pre><span></span><code>char ch;
scanf(“%c” , &ch);
printf(“%c”, ch);
</code></pre></div>
<p>For float type variable, you can do as :</p>
<div class="highlight"><pre><span></span><code><span class="nb nb-Type">float</span><span class="w"> </span><span class="n">fvar</span><span class="p">;</span>
<span class="n">scanf</span><span class="p">(</span><span class="err">“</span><span class="o">%</span><span class="n">f</span><span class="err">”</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">fvar</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="err">“</span><span class="o">%</span><span class="n">f</span><span class="err">”</span><span class="p">,</span><span class="w"> </span><span class="n">fvar</span><span class="p">);</span>
</code></pre></div>
<p>And so on, you can accept and return the values in variables of different kinds, what you have to remember is , you have to specify the placeholders in C in order to enter and retrieve values in variables, and if you want to accept input, use scanf function with ampersand, and for output use just variable name.</p>
<p>In the next tutorial we'll learn about Operators in C, what they are used for, and then with help of operators manipulate the values stored in variable, and implement all this through a small program to understand about what all we've discussed in these tutorials.</p>
<p>If you like the posts, don't forget to share this with your friends. For any doubts, let us know through comments, we'll answer you and clear your doubts ASAP.</p>Java 7 : Try-with-resources - Close resources automatically with AutoCloseable interface2013-12-15T21:19:00+05:302013-12-15T21:19:00+05:30Anooptag:thegeekyway.com,2013-12-15:/java-7-try-resources-close-resources-automatically-autocloseable-interface/<p>Every time we write code with FileInputStream, <p style="font-family: courier new,courier;">BufferReader</p> etc. we need to close those resource explicitly in finally block.</p>
<p>Now from Java 7, there are two interfaces included <p style="font-family: courier new,courier;">Closeable</p> and <p style="font-family: courier new,courier;">AutoCloseable</p> with single abstract method <p style="font-family: courier new,courier;">close(),</p> which enables implementing class to close resources aromatically after try block.</p>
<p>Lets see how …</p><p>Every time we write code with FileInputStream, <p style="font-family: courier new,courier;">BufferReader</p> etc. we need to close those resource explicitly in finally block.</p>
<p>Now from Java 7, there are two interfaces included <p style="font-family: courier new,courier;">Closeable</p> and <p style="font-family: courier new,courier;">AutoCloseable</p> with single abstract method <p style="font-family: courier new,courier;">close(),</p> which enables implementing class to close resources aromatically after try block.</p>
<p>Lets see how it works.</p>
<p>Conventional way of using FileInputStream</p>
<div class="highlight"><pre><span></span><code><span class="nt">File</span><span class="w"> </span><span class="nt">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">new</span><span class="w"> </span><span class="nt">File</span><span class="o">(</span><span class="s2">"C:\Document1.txt"</span><span class="o">);</span>
<span class="w"> </span><span class="nt">InputStream</span><span class="w"> </span><span class="nt">fis</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">null</span><span class="o">;</span>
<span class="w"> </span><span class="nt">try</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">fis</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">new</span><span class="w"> </span><span class="err">FileInputStream(file)</span><span class="p">;</span>
<span class="w"> </span><span class="err">System.out.println(file.getAbsolutePath())</span><span class="p">;</span>
<span class="w"> </span><span class="err">int</span><span class="w"> </span><span class="err">readLocation</span><span class="p">;</span>
<span class="w"> </span><span class="err">while</span><span class="w"> </span><span class="err">((readLocation</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">fis.read())</span><span class="w"> </span><span class="err">!=</span><span class="w"> </span><span class="err">-1)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="err">System.out.print((char)</span><span class="w"> </span><span class="err">readLocation)</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="nt">catch</span><span class="w"> </span><span class="o">(</span><span class="nt">IOException</span><span class="w"> </span><span class="nt">e</span><span class="o">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">e.printStackTrace()</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="nt">finally</span><span class="p">{</span>
<span class="w"> </span><span class="err">if</span><span class="w"> </span><span class="err">(fis</span><span class="w"> </span><span class="err">!=</span><span class="w"> </span><span class="err">null)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="err">try</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="err">fis.close()</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="nt">catch</span><span class="w"> </span><span class="o">(</span><span class="nt">IOException</span><span class="w"> </span><span class="nt">e</span><span class="o">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">e.printStackTrace()</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="err">}</span>
<span class="w"> </span><span class="err">}</span>
</code></pre></div>
<p>Now with using Try-with-Resource</p>
<div class="highlight"><pre><span></span><code><span class="nt">try</span><span class="w"> </span><span class="o">(</span><span class="nt">InputStream</span><span class="w"> </span><span class="nt">fileInputStream</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">new</span><span class="w"> </span><span class="nt">FileInputStream</span><span class="o">(</span><span class="nt">new</span><span class="w"> </span><span class="nt">File</span><span class="o">(</span><span class="s2">"C:\Document1.txt"</span><span class="o">)))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">int</span><span class="w"> </span><span class="err">readLocation</span><span class="p">;</span>
<span class="w"> </span><span class="err">while</span><span class="w"> </span><span class="err">((readLocation</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">fileInputStream.read())</span><span class="w"> </span><span class="err">!=</span><span class="w"> </span><span class="err">-1)</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="err">System.out.print((char)</span><span class="w"> </span><span class="err">readLocation)</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="nt">catch</span><span class="w"> </span><span class="o">(</span><span class="nt">IOException</span><span class="w"> </span><span class="nt">e</span><span class="o">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="err">e.printStackTrace()</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<h4>How it works:</h4>
<p>Compare the changes. Initialization, of resource which needs to be close, happening with try and there is no finally block to close. <a href="http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html" title="Java Doc">FileInputStream</a> implements <a href="http://docs.oracle.com/javase/6/docs/api/java/io/Closeable.html" title="interface in java.io">Closeable</a> which have following code in method <p style="font-family: 'courier new', courier;">close()</p>.</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="nv">public</span><span class="w"> </span><span class="nv">void</span><span class="w"> </span><span class="nv">close</span><span class="ss">()</span><span class="w"> </span><span class="nv">throws</span><span class="w"> </span><span class="nv">IOException</span><span class="w"> </span>{
<span class="w"> </span><span class="nv">synchronized</span><span class="w"> </span><span class="ss">(</span><span class="nv">closeLock</span><span class="ss">)</span><span class="w"> </span>{
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="ss">(</span><span class="nv">closed</span><span class="ss">)</span><span class="w"> </span>{
<span class="w"> </span><span class="k">return</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="nv">closed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">true</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="ss">(</span><span class="nv">channel</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nv">null</span><span class="ss">)</span><span class="w"> </span>{
<span class="w"> </span><span class="cm">/*</span>
<span class="cm"> * Decrement the FD use count associated with the channel</span>
<span class="cm"> * The use count is incremented whenever a new channel</span>
<span class="cm"> * is obtained from this stream.</span>
<span class="cm"> */</span>
<span class="w"> </span><span class="nv">fd</span>.<span class="nv">decrementAndGetUseCount</span><span class="ss">()</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">channel</span>.<span class="nv">close</span><span class="ss">()</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="cm">/*</span>
<span class="cm"> * Decrement the FD use count associated with this stream</span>
<span class="cm"> */</span>
<span class="w"> </span><span class="nv">int</span><span class="w"> </span><span class="nv">useCount</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">fd</span>.<span class="nv">decrementAndGetUseCount</span><span class="ss">()</span><span class="c1">;</span>
<span class="w"> </span><span class="cm">/*</span>
<span class="cm"> * If FileDescriptor is still in use by another stream, the finalizer</span>
<span class="cm"> * will not close it.</span>
<span class="cm"> */</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="ss">((</span><span class="nv">useCount</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">0</span><span class="ss">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nv">isRunningFinalize</span><span class="ss">())</span><span class="w"> </span>{
<span class="w"> </span><span class="nv">close0</span><span class="ss">()</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span>}
</code></pre></div>
<p>Note that you can still use <p style="font-family: courier new,courier;">finally</p> block if you want.</p>
<h4>Closeable vs AutoCloseable</h4>
<p>Here is the code</p>
<div class="highlight"><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="n">interface</span><span class="w"> </span><span class="n">AutoCloseable</span><span class="w"> </span><span class="err">{</span>
<span class="w"> </span><span class="cm">/**</span>
<span class="cm"> * Closes this resource, relinquishing any underlying resources.</span>
<span class="cm"> * This method is invoked automatically on objects managed by the</span>
<span class="cm"> * {@code try}-with-resources statement.</span>
<span class="cm">*/</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="k">close</span><span class="p">()</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="k">Exception</span><span class="p">;</span>
<span class="err">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="n">public</span><span class="w"> </span><span class="n">interface</span><span class="w"> </span><span class="n">Closeable</span><span class="w"> </span><span class="k">extends</span><span class="w"> </span><span class="n">AutoCloseable</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">/**</span>
<span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">Closes</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">stream</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">releases</span><span class="w"> </span><span class="n">any</span><span class="w"> </span><span class="n">system</span><span class="w"> </span><span class="n">resources</span><span class="w"> </span><span class="n">associated</span>
<span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">with</span><span class="w"> </span><span class="n">it</span><span class="o">.</span><span class="w"> </span><span class="n">If</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">stream</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">already</span><span class="w"> </span><span class="n">closed</span><span class="w"> </span><span class="n">then</span><span class="w"> </span><span class="n">invoking</span><span class="w"> </span><span class="n">this</span>
<span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">method</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="n">no</span><span class="w"> </span><span class="n">effect</span><span class="o">.</span>
<span class="w"> </span><span class="o">*</span>
<span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="err">@</span><span class="n">throws</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">an</span><span class="w"> </span><span class="n">I</span><span class="o">/</span><span class="n">O</span><span class="w"> </span><span class="n">error</span><span class="w"> </span><span class="n">occurs</span>
<span class="w"> </span><span class="o">*/</span>
<span class="w"> </span><span class="n">public</span><span class="w"> </span><span class="nb nb-Type">void</span><span class="w"> </span><span class="n">close</span><span class="p">()</span><span class="w"> </span><span class="n">throws</span><span class="w"> </span><span class="n">IOException</span><span class="p">;</span><span class="w"> </span>
<span class="p">}</span>
</code></pre></div>
<p>I hope it clears out when to use AutoCloseable and Closeable..</p>
<p><a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">Oracle Page for Try-With-Resources</a></p>Java 7: Binary literal type2013-12-15T17:21:00+05:302013-12-15T17:21:00+05:30Anooptag:thegeekyway.com,2013-12-15:/java-7-binary-literal-type/<p>From Java 7 Binary type also joins the category of literal with * <p style="font-family: 'courier new', courier;">byte, short, int and long</p>.<em> Which means a binary value can be assigned by using </em><strong>0b </strong><em>or</em><strong> 0B </strong>*prefix to a primitive type.</p>
<p><strong>What is a Literal</strong> : A literal is source code representation of source code. Which can …</p><p>From Java 7 Binary type also joins the category of literal with * <p style="font-family: 'courier new', courier;">byte, short, int and long</p>.<em> Which means a binary value can be assigned by using </em><strong>0b </strong><em>or</em><strong> 0B </strong>*prefix to a primitive type.</p>
<p><strong>What is a Literal</strong> : A literal is source code representation of source code. Which can be directly assigned to any <em>primitive</em> type.</p>
<p>Try following in your code with JDK1.7+</p>
<div class="highlight"><pre><span></span><code>int intType = 0b100;
char charType = 0B1100001;
</code></pre></div>
<p>Why Binary Literals:</p>
<ul>
<li>No need to translate binary value to integer to use them.</li>
<li>Benefit to programs where bit per bit operation is required.</li>
</ul>
<p>Other literal type are following</p>
<div class="highlight"><pre><span></span><code>char capitalC = 'C'; /*Character type or you can use unicode 'u0043' */
byte b = 100; /* byte limit is -128 to 127*/
short s = 10000; /* short limit is -32768 to 32767*/
int i = 100000; /*int limit is -2^31 to 2^31 - 1*/
</code></pre></div>
<p>One more feature introduced related to primitive type.</p>
<p><strong>Underscore in integer type</strong>: To improve readability of code in integer delimiters.</p>
<div class="highlight"><pre><span></span><code>int newInt = 100_00_00;
float newfloat = 3.12_34_67f;
</code></pre></div>Maintaining Sessions in Asp.net2013-12-14T01:33:00+05:302013-12-14T01:33:00+05:30Deep_Singhtag:thegeekyway.com,2013-12-14:/sessions-in-asp-net/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/aspnet_sessions.jpg"><img alt="aspnet_sessions" src="//thegeekyway.com/uploads/images/2013/12/aspnet_sessions.jpg"></a></p>
</div>
<p>Session is amongst the most important part in creating asp.net applications. It is a variable where you can put data between page requests when the pages are either posting or redirecting to each other. It can contain small amount of data containing string, complex arrays or numbers.</p>
<p>In Asp …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/aspnet_sessions.jpg"><img alt="aspnet_sessions" src="//thegeekyway.com/uploads/images/2013/12/aspnet_sessions.jpg"></a></p>
</div>
<p>Session is amongst the most important part in creating asp.net applications. It is a variable where you can put data between page requests when the pages are either posting or redirecting to each other. It can contain small amount of data containing string, complex arrays or numbers.</p>
<p>In Asp.net the copy of session variable is stored for each individual session and session are managed with <strong>cookies</strong>. <strong>Cookies</strong> are data created on server and are sent back to browser in response to http request. In asp.net session cookies are created automatically once the page is loaded into the browser.</p>
<p>During the page load, the server detect a new user and then creates a session cookie for the new user and then sends back to the browser and then use cookies for each subsequent request. Session variable can be of many types from string to complex numbers and arrays.The session variable are stored in <strong>SessionStateItemCollection</strong> Class.</p>
<p>****The format to create a session object in asp.net is</p>
<blockquote>
<h4>Session[key_name] = (any dynamic or arbitrary name)</h4>
</blockquote>
<p>Let us consider an example where we are using a login interface with 2 aspx pages (Session_page.aspx,Landing_Page.aspx) and username and password are being stored in session and displaying the username and password on Landing_Page.aspx.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.1.png" title="Asp.net tutorial : Maintaining sessions in asp.net (Image 1)"><img alt="Asp.net tutorial : Maintaining sessions in asp.net" src="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.1.png"></a></p>
</div>
<p>Let us use "Test_User" as username and "Test_Password" as password. (Note that at this point of tutorial the values are not comparing with the database values we are just assuming that "Test_User" and "Test_Password" are registered and we have reached Landing_page.aspx )</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.2.png" title="Asp.net tutorial : Maintaining sessions in asp.net (Image 2)"><img alt="Asp.net tutorial : Maintaining sessions in asp.net (Image 2)" src="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.2.png"></a></p>
</div>
<p>The main coding is done on the "Login" button where we will fetch the values from text boxes and store the values in sessions.<br>
The <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> code for the login button is given as follows</p>
<div class="highlight"><pre><span></span><code> protected void Login_Button(object sender, EventArgs e)
{
Session["User_name"] = TextBox1.Text;
Session["Password"] = TextBox2.Text;
Response.Redirect("~/Landing_Page.aspx");
}
</code></pre></div>
<p>where TextBox1.Text contains the text entered in the username field and TextBox2.Text contains the text entered in password field, once the login button is pressed the session is created and the username and password is stored in the session variable, now we can use this session variable anywhere within our project.</p>
<p>After clicking the login button here is the Landing_page.aspx where the session variable is used, here is the output of Landing_Page.aspx</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.3.png" title="Asp.net tutorial : Maintaining sessions in asp.net (Image 3)"><img alt="Asp.net tutorial : Maintaining sessions in asp.net (Image 3)" src="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.3.png"></a></p>
</div>
<p>Here is the code for the Landing_Page.aspx</p>
<div class="highlight"><pre><span></span><code>protected void Page_Load(object sender, EventArgs e)
{
Session_Username.Text = Session["User_Name"].ToString();
Session_Passowrd.Text = Session["Password"].ToString();
}
</code></pre></div>
<p>Now since Label7.Text expects a string but there is now way in asp.net that it automatically identifies it as a string so we have to explicitly store the value as a string, now there are 2 ways, the first way is using <a href="http://www.thegeekyway.com/category/programming/c-programming-2/">C#</a> , the code is</p>
<div class="highlight"><pre><span></span><code> Label7.Text = (String)Session["User_Name"];
</code></pre></div>
<p>and the second way is to append .ToString() function to the session variable, the code is given below</p>
<div class="highlight"><pre><span></span><code> Session_Username.Text = Session["User_Name"].ToString();
</code></pre></div>
<p>Some FAQ's related to Sessions using asp.net</p>
<p><strong>Q:</strong> What happens if we close the browser and reopen Landing_Page.aspx, will the session be maintained ?<br>
<strong>A:</strong> The answer is <strong>NO</strong> once the browser is closed the cookies are destroyed and so as the session.</p>
<p><strong>Q:</strong> So what happens if we directly open the Landing_Page.aspx page ?<br>
<strong>A:</strong> Since we directly opened the Landing_Page.aspx page, no session was created for username and password so it will display blank values for username and password. But its not recommended to either redirect or display an error message. For example consider the following code segment</p>
<div class="highlight"><pre><span></span><code><span class="nv">protected</span><span class="w"> </span><span class="nv">void</span><span class="w"> </span><span class="nv">Page_Load</span><span class="ss">(</span><span class="nv">object</span><span class="w"> </span><span class="nv">sender</span>,<span class="w"> </span><span class="nv">EventArgs</span><span class="w"> </span><span class="nv">e</span><span class="ss">)</span>
<span class="w"> </span>{
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="ss">(</span><span class="nv">Session</span>[<span class="s2">"User_Name"</span>]<span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nv">null</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nv">Session</span>[<span class="s2">"Password"</span>]<span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nv">null</span><span class="ss">)</span>
<span class="w"> </span>{<span class="w"> </span><span class="o">//</span><span class="nv">Your</span><span class="w"> </span><span class="nv">redirection</span><span class="w"> </span><span class="nv">code</span><span class="w"> </span><span class="nv">here</span>.
<span class="w"> </span><span class="nv">Top_Header_Text</span>.<span class="nv">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">false</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">Session_Username</span>.<span class="nv">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" No session created, you will be soon redirected to Session_Page.aspx. "</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">Session_Password</span>.<span class="nv">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">false</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">Username</span>.<span class="nv">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">false</span><span class="c1">;</span>
<span class="w"> </span><span class="nv">Password</span>.<span class="nv">Visible</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">false</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span>{
<span class="w"> </span><span class="nv">Session_Username</span>.<span class="nv">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">(</span><span class="nv">String</span><span class="ss">)</span><span class="nv">Session</span>[<span class="s2">"User_Name"</span>]<span class="c1">;</span>
<span class="w"> </span><span class="nv">Session_Password</span>.<span class="nv">Text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">Session</span>[<span class="s2">"Password"</span>].<span class="nv">ToString</span><span class="ss">()</span><span class="c1">;</span>
<span class="w"> </span>}
<span class="w"> </span>}
</code></pre></div>
<p>Then the output will be</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.4.png" title="Asp.net tutorial : Maintaining sessions in asp.net (Image 4)"><img alt="Asp.net tutorial : Maintaining sessions in asp.net (Image 4)" src="//thegeekyway.com/uploads/images/2013/12/ASP.net_Tut1.4.png"></a></p>
</div>
<hr>How to add custom CSS in HTML2013-12-12T23:47:00+05:302013-12-12T23:47:00+05:30Anooptag:thegeekyway.com,2013-12-12:/add-custom-css-html/<p>I have already given the <a href="http://www.thegeekyway.com/css-introduction-and-different-ways-of-using-css/" title="CSS – Introduction and How it works">introduction to CSS</a> and advantages.Now, there are few ways to use your custom CSS in your site/blog/wordpress.</p>
<ul>
<li>Using style tag with HTML elements.</li>
<li>Using Style element inside HTML page</li>
<li>Linking external CSS file in your pages</li>
<li>Importing CSS file inside another CSS …</li></ul><p>I have already given the <a href="http://www.thegeekyway.com/css-introduction-and-different-ways-of-using-css/" title="CSS – Introduction and How it works">introduction to CSS</a> and advantages.Now, there are few ways to use your custom CSS in your site/blog/wordpress.</p>
<ul>
<li>Using style tag with HTML elements.</li>
<li>Using Style element inside HTML page</li>
<li>Linking external CSS file in your pages</li>
<li>Importing CSS file inside another CSS file</li>
</ul>
<h4>1. Using Style tag in HTML element:</h4>
<p>You can simply use style tag like below to apply styling to your element.</p>
<div class="highlight"><pre><span></span><code><span class="nt"><strong</span><span class="w"> </span><span class="na">style=</span><span class="s">"color:White; background:black"</span><span class="nt">></span><span class="w"> </span>Your<span class="w"> </span>Text<span class="w"> </span><span class="nt"></strong></span>
</code></pre></div>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/olJkD">olJkD</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<p>
<script type="text/javascript" src="//codepen.io/assets/embed/ei.js" async></script>
</p>
<h4>2. Using Style element in your page</h4>
<p>Add a <em>style</em> element in your Head element and define your css classes.</p>
<style><!--<br />
.<span class="hiddenSpellError" pre="">myclass</span> {<br />
color:red;<br />
background:black;<br />
}</p>
<p>--></style>
<div class="highlight"><pre><span></span><code> Your Text
</code></pre></div>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/IeBwH">IeBwH</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<h4>3. Linking a external css file in page</h4>
<p>Add a link to your css file and those classes will be applied to your elements. This is best approach to have clean code and managing your CSS.</p>
<div class="highlight"><pre><span></span><code> css" href="customstylesheet.css">
</code></pre></div>
<h4>4. Import a CSS file inside another CSS file</h4>
<p>Use following format to import a CSS file. Remember following import tag should first line in your file.</p>
<div class="highlight"><pre><span></span><code><span class="cp">@import</span><span class="w"> </span><span class="s2">"mynewstylesheet.css"</span><span class="p">;</span>
</code></pre></div>
<p>Note: For point 3 and 4 location of your css file is relative to current file. If you are using direct name.css that means CSS file and HTML/CSS file both are in same directory.</p>CSS - Introduction and How it works2013-12-11T22:57:00+05:302013-12-11T22:57:00+05:30Anooptag:thegeekyway.com,2013-12-11:/css-introduction-and-different-ways-of-using-css/<p>CSS stands for <strong><em>Cascading Style Sheet</em></strong> which is used to style HTML elements.<br>
While loading any webpage browser simply create DOM of requested web page and renders the HTML. CSS is basically a better way of managing HTML attributes <p style="color: #800000;"><strong>style</strong></p> of any element.</p>
<p>To add style we simply use <p style="color: #800000;"><em>Style …</em></p></p><p>CSS stands for <strong><em>Cascading Style Sheet</em></strong> which is used to style HTML elements.<br>
While loading any webpage browser simply create DOM of requested web page and renders the HTML. CSS is basically a better way of managing HTML attributes <p style="color: #800000;"><strong>style</strong></p> of any element.</p>
<p>To add style we simply use <p style="color: #800000;"><em>Style</em> </p>attribute to add color, effect, transition or any other things. Like following</p>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/jLmEx">jLmEx</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<p>
<script type="text/javascript" src="//codepen.io/assets/embed/ei.js" async></script>
Now using CSS
</p>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/ICDxa">ICDxa</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<p>A CSS which is used to style a element is called as <em>CSS Class</em> or simply <em>Class</em> associated with element.</p>
<p>Now, why to use CSS:</p>
<ul>
<li>CSS are stored in separate file which contains all the classes to be applied on element</li>
<li>Easy to manage and avoid duplication. In a website if you want to make all the links to get into red color on hover you can simply just add a particular class and you are done.</li>
<li>You can use same HTML code to different place with different style by just editing small fragment of CSS class</li>
</ul>
<p style="text-decoration: underline;">Different ways of using CSS:</p>
<h6>(click on result tab and hover mouse on link)</h6>
<ol>
- #### Using class attribute in element.
</ol>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/Ikupj">Ikupj</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<ol>
- #### Element ID specific css
</ol>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/HtmGa">HtmGa</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>
<ol>
- #### Directly using HTML tag as css class
</ol>
<p>See the Pen <a href="http://codepen.io/ianoop/pen/AdCnL">AdCnL</a> by ianoop (<a href="http://codepen.io/ianoop">\@ianoop</a>) on <a href="http://codepen.io">CodePen</a></p>Install Alternative To Arduino IDE2013-12-11T08:42:00+05:302013-12-11T08:42:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-11:/install-alternative-arduino-ide/<p>Arduino, the world famous opensource electronic prototyping platform is undoubtedly the best series of boards used by makers & Engineers. However, the official Arduino IDE which is used to load the arduino sketch is bit clumsy. I never really liked Arduino IDE, so after searching a lot, I came to know …</p><p>Arduino, the world famous opensource electronic prototyping platform is undoubtedly the best series of boards used by makers & Engineers. However, the official Arduino IDE which is used to load the arduino sketch is bit clumsy. I never really liked Arduino IDE, so after searching a lot, I came to know about 'Stino', the plugin for world's slickest text editor Sublime Text 2. Using Stino you can edit arduino codes and even upload sketches to your Arduino board. All you have to do is follow the steps mentioned below.</p>
<p><strong>Step 1 : Download Arduino IDE .tgz</strong></p>
<p>Yeah, we need Arduino IDE for their libraries which we'll import in Sublime Text 2. So, download Arduino IDE.tgz from the <a href="http://arduino.cc/en/main/software" title="Download Arduino">Arduino Website</a> as per your system architecture.</p>
<p>Now extract the downloaded .tgz & copy the extracted folder to /home directory.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Home_005.png" title="Extracted Arduino moved to /home directory"><img alt="Home_005" src="//thegeekyway.com/uploads/images/2013/12/Home_005.png" title="Extracted Arduino moved to /home directory"></a></p>
</div>
<p><strong>Step 2 : Install Sublime text Editor</strong></p>
<p>View <a href="http://www.thegeekyway.com/install-sublime-text-editor-gnulinux/" title="Steps to install Sublime Text"><strong>THIS ARTICLE</strong></a> for procedure to install Sublime Text editor in your Linux system.</p>
<p><strong>Step 3 : Installing Package control in Sublime Text</strong></p>
<p>Now you've installed Sublime Text 2, but to install plugins for Arduino in Sublime text you have to add sublime package control. Sublime package control can discover, install & even update the sublime plugins .</p>
<p>To install Sublime package control, Start sublime text editor & press <strong>Ctrl + `</strong> and type the following commands :</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">urllib2</span><span class="o">,</span><span class="nn">os</span><span class="p">;</span> <span class="n">pf</span><span class="o">=</span><span class="s1">'Package Control.sublime-package'</span><span class="p">;</span> <span class="n">ipp</span><span class="o">=</span><span class="n">sublime</span><span class="o">.</span><span class="n">installed_packages_path</span><span class="p">();</span> <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">ipp</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">ipp</span><span class="p">)</span> <span class="k">else</span> <span class="kc">None</span><span class="p">;</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">install_opener</span><span class="p">(</span><span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">urllib2</span><span class="o">.</span><span class="n">ProxyHandler</span><span class="p">()));</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ipp</span><span class="p">,</span><span class="n">pf</span><span class="p">),</span><span class="s1">'wb'</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">'http://sublime.wbond.net/'</span><span class="o">+</span><span class="n">pf</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span><span class="s1">'%20'</span><span class="p">))</span><span class="o">.</span><span class="n">read</span><span class="p">());</span> <span class="nb">print</span> <span class="s1">'Please restart Sublime Text to finish installation'</span>
</code></pre></div>
<p>and press 'Enter' . Something like below shown image will appear.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/sublimearduino.png" title="Package control installed in Sublime text 2"><img alt="sublimearduino" src="//thegeekyway.com/uploads/images/2013/12/sublimearduino.png" title="Package control installed in Sublime text 2"></a></p>
</div>
<p>Restart the Sublime Text editor.</p>
<p><strong> Step 4 : Installing Stino</strong></p>
<p>Stino is the plugin for Sublime text editor written by Robot-Will, you can view the code of Stino from <a href="https://github.com/Robot-Will/Stino" title="Stino Source code">Github</a></p>
<p>Now, Open Sublime text editor & go to <strong>Preference -> Package Control</strong> and click on <strong>Install Packages.</strong></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/package-control.png" title="Selecting 'Install Package' in Package control"><img alt="package control" src="//thegeekyway.com/uploads/images/2013/12/package-control.png"></a></p>
</div>
<p>Wait for while till the repository is loading, once repository is loaded, you'll be able to access all the available plugins & install it. Search for 'Arduino IDE' & press 'Enter'. This will install 'Stino' in sublime text editor.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/sublime-package-stino.png" title="Installing Stino"><img alt="sublime package stino" src="//thegeekyway.com/uploads/images/2013/12/sublime-package-stino.png" title="Installing Stino"></a></p>
</div>
<p><strong>Step 5 : Configuring Stino to make Sublime text editor to work like Arduino IDE.</strong></p>
<p>Now open Sublime text edito, in their one more tab will be available now, called 'Arduino'. Click on it & go to Preferences -> Select Arduino Application Folder. Then select the path where you've extracted arduino.tgz (In this we extracted in /home folder, so select home while declaring path to Stino)</p>
<p>If you've extracted arduino.tgz the steps will be like : <strong>Preferences -> Select Arduino Application Folder -> / -> home -> your_username -> arduino-1.0.5</strong> and you'll get the output as given below :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/sublime-linked-arduino.png" title="Selecting Arduino application folder"><img alt="sublime linked arduino" src="//thegeekyway.com/uploads/images/2013/12/sublime-linked-arduino.png" title="Selecting Arduino application folder from sublime"></a></p>
</div>
<p>That's all. Now, you can select your arduino board in which you're going to upload sketches from <strong>Arduino -> Arduino AVR boards. </strong> Use <strong>'Verify/Compile'</strong> option to compile your arduino sketch & <strong>'Upload'</strong> option to upload the sketch into arduino connected to your PC.</p>
<blockquote>
<p>Honestly, Stino + Sublime text is so robust that i can't explain in words how much i am enjoying coding in Arduino.</p>
</blockquote>
<p>I hope this article may help you to find an alternative to Arduino IDE & enjoy editing & uploading sketches in Arduino.</p>How To Install Sublime Text Editor In GNU/Linux ?2013-12-11T07:25:00+05:302013-12-11T07:25:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-11:/install-sublime-text-editor-gnulinux/<p>Sublime Text Editor is one of the best cross-platform editor out there for editing source code. It is capable of syntax highlighting for wide range of file extensions like Python, C, C++, Java, HTML, JS, PHP, LaTeX & even Arduino. Sublime Text Editor is not opensource & not an free editor, but …</p><p>Sublime Text Editor is one of the best cross-platform editor out there for editing source code. It is capable of syntax highlighting for wide range of file extensions like Python, C, C++, Java, HTML, JS, PHP, LaTeX & even Arduino. Sublime Text Editor is not opensource & not an free editor, but in last update the expiry date has been removed by the developers, so you can use Sublime text as there is no enforce time limit for evaluation.</p>
<p><strong>Sublime Text Editor can be installed manually following the steps below :</strong></p>
<ul>
<li>Download correct bit version of Sublime Text 2 from <a href="http://www.sublimetext.com/2" title="Sublime Text Download"><strong>Official Website.</strong></a></li>
</ul>
<!-- -->
<ul>
<li>Now, extract the downloaded Sublime text 2 .tar.bz2 in <strong>/home</strong> directory by simply right clicking on it.</li>
</ul>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Sublime_text1.png"><img alt="Sublime_text1" src="//thegeekyway.com/uploads/images/2013/12/Sublime_text1.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Sublime-text-2.png"><img alt="Sublime text 2" src="//thegeekyway.com/uploads/images/2013/12/Sublime-text-2.png"></a></p>
</div>
<ul>
<li>Now move the extracted folder 'Sublime Text 2' to the proper location. Open Terminal/Konsole and type :</li>
</ul>
<div class="highlight"><pre><span></span><code>sudo mv Sublime Text 2 /opt/
</code></pre></div>
<p>The extracted folder will be moved in /opt directory of your system.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Sublime-text-3.png"><img alt="Sublime text 3" src="//thegeekyway.com/uploads/images/2013/12/Sublime-text-3.png"></a></p>
</div>
<ul>
<li>Now, we have to add following command for symbolic link. Open terminal & type :</li>
</ul>
<div class="highlight"><pre><span></span><code>sudo ln -s /opt/Sublime Text 2/sublime_text /usr/bin/sublime
</code></pre></div>
<ul>
<li>Now, to add application icon in launcher. In terminal type :</li>
</ul>
<div class="highlight"><pre><span></span><code>sudo gedit /usr/share/applications/sublime.desktop
</code></pre></div>
<p>(Replace gedit by any other editor like kate, vim or even sublime) and copy the following lines into it & hit on 'Save' button.</p>
<div class="highlight"><pre><span></span><code><span class="k">[Desktop Entry]</span>
<span class="na">Version</span><span class="o">=</span><span class="s">2.0</span>
<span class="na">Name</span><span class="o">=</span><span class="s">Sublime Text 2</span>
<span class="c1"># Only KDE 4 seems to use GenericName, so we reuse the KDE strings.</span>
<span class="c1"># From Ubuntu's language-pack-kde-XX-base packages, version 9.04-20090413.</span>
<span class="na">GenericName</span><span class="o">=</span><span class="s">Text Editor</span>
<span class="na">Exec</span><span class="o">=</span><span class="s">sublime</span>
<span class="na">Terminal</span><span class="o">=</span><span class="s">false</span>
<span class="na">Icon</span><span class="o">=</span><span class="s">/opt/Sublime Text 2/Icon/48x48/sublime_text.png</span>
<span class="na">Type</span><span class="o">=</span><span class="s">Application</span>
<span class="na">Categories</span><span class="o">=</span><span class="s">TextEditor</span><span class="c1">;IDE;Development</span>
<span class="na">X-Ayatana-Desktop-Shortcuts</span><span class="o">=</span><span class="s">NewWindow</span>
<span class="k">[NewWindow Shortcut Group]</span>
<span class="na">Name</span><span class="o">=</span><span class="s">New Window</span>
<span class="na">Exec</span><span class="o">=</span><span class="s">sublime -n</span>
<span class="na">TargetEnvironment</span><span class="o">=</span><span class="s">Unity</span>
</code></pre></div>
<p>Login & out & you'll notice launcher icon of sublime text in Menu.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/sublime-text-4.png"><img alt="sublime text 4" src="//thegeekyway.com/uploads/images/2013/12/sublime-text-4.png"></a></p>
</div>
<blockquote>
<p>I have tested same .desktop configuration in openSUSE Gnome & it shows up in launcher too, so no need to modify if you're using other desktop environment. Didn't tested in KDE though.</p>
</blockquote>
<p>Happy coding.</p>How To Install Any Application From Source Code ?2013-12-10T10:44:00+05:302013-12-10T10:44:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-10:/install-application-source-code-2/<style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Most of the applications are available in the official repository of your Linux distribution. However, there are times when some of the applications you're installing haven't been updated in the repository & you want to get the latest version of that application.Learning how to …</p><style type="text/css"><!--<br />
P { margin-bottom: 0.08in; }<br />
--></style>
<p>Most of the applications are available in the official repository of your Linux distribution. However, there are times when some of the applications you're installing haven't been updated in the repository & you want to get the latest version of that application.Learning how to install application from the source code comes handy during this time.</p>
<p><strong>There are 6 steps to follow :</strong></p>
<ol>
<li><strong> Obtaining source code</strong></li>
<li><strong> Unzipping source code</strong></li>
<li><strong> Configuring source code</strong></li>
<li><strong> Resolving dependencies (if any dependency is missing)</strong></li>
<li><strong> Compiling the source code</strong></li>
<li><strong> Installing the binaries</strong></li>
</ol>
<p><strong>1) Obtaining source code :</strong></p>
<p>Source code of the opensource application can be obtained from Github using git if the source is uploaded in Github, or you can even visit the official website of the application & download it's source code. In most case, you'll find source of opensource application from the official website, easily.</p>
<p><strong>2) Unzipping source code :</strong></p>
<p>The downloaded source code will be in <em>.tar.bz2</em> or <em>.tar.gz</em> ,you need to extract the files within the tar ball & place it anywhere in the <em>/home</em> directory. Note that, it is not necessary to put the extracted source code in <em>/home</em> directory only. But, <em>/home</em> directory is usually selected by most of the user as it is convenient to access.</p>
<p><strong>3) Configuring the source code :</strong></p>
<p>Since you've downloaded the source code & extracted it in /home directory; Use <strong>cd</strong> (change directory) command to enter the directory where the source code is available.</p>
<p>For Example :</p>
<div class="highlight"><pre><span></span><code>cd /home/user_name/extracted_sourcecode_folder
</code></pre></div>
<p>Now, before configuring the source code let's understand what is it.</p>
<blockquote>
<p><strong>'Configure'</strong> is the title given to the configuration script which is usually shipped in package alongwith the source code. The configuration script makes sure that all the required tools for the compilation is installed within the system or not. If the dependency is missing, the configuration script will give you error & you won't be able to compile the software unless all the missing dependencies are not met.</p>
</blockquote>
<p>You can execute the configuration script using following command (Make sure you're in root) :</p>
<div class="highlight"><pre><span></span><code>./configure
</code></pre></div>
<blockquote>
<p><em>All source codes are </em><em>not</em><em> shipped with configuration script, In that case you've to access documentation of that specific application & manually check the required dependencies & whether all the dependencies are met or not.</em></p>
</blockquote>
<p><strong>4) Resolving dependencies :</strong></p>
<p>After process of 'configuring', if any dependency is missing the compilation won't proceed. So, if the configuration script pops the dependency error, then install the missing dependency & execute the configuration script again ( i.e ./configure). After configure script is successfully executed, it will generate the <strong>'Make'</strong> file which will be used for the compilation of the source code.</p>
<p>Configuration & resolving dependencies are most important part of entire process as you've to match all the required dependencies, after that everything is cake walk.</p>
<p>Now if all the dependencies are met, you can proceed the next step.</p>
<p><strong>5) Compiling the source code:</strong></p>
<p>Now open terminal & type</p>
<div class="highlight"><pre><span></span><code>make
</code></pre></div>
<p>This command will compile the software & all the binaries are generated. The duration taken by the system for compilation process depends on the size of the program & processing speed of your computer hardware. Now, we have to install the generated binaries created after compilation.</p>
<p><strong>6) Installing the binaries :</strong></p>
<p>If no error is invoked during the compilation of the software, open terminal/konsole & type :</p>
<div class="highlight"><pre><span></span><code>make install
</code></pre></div>
<p>This command will install all the required binaries in the system.</p>
<hr>
<p><strong>Example for installing an application from the source code :</strong></p>
<p>Following is the short example of the entire process of installing application from the source code. In this example, I will be installing <a href="http://satya164.deviantart.com/art/Gnome-Shell-Elegance-Colors-305966388"><strong>'elegance-colors'</strong></a> in my system.</p>
<p><strong>Step 1 :</strong></p>
<p>I have downloaded the source code of 'elegance-colors' from <a href="https://github.com/satya164/elegance-colors" title="Elegance Colors"><strong>HERE</strong></a></p>
<p><strong>Step 2 :</strong></p>
<p>Downloaded source code is extracted in /home directory</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Home_012.png" title="Home directory where source code is placed"><img alt="Home_012" src="//thegeekyway.com/uploads/images/2013/12/Home_012.png" title="Home directory"></a></p>
</div>
<p><strong>Step 3 :</strong></p>
<p>I have to configure manually, since configuration script is not available for this opensource application.</p>
<p><strong>Step 4 :</strong></p>
<p>I resolved all the dependencies that were missing after checking the <a href="https://github.com/satya164/elegance-colors/blob/master/README.md"><strong>documentation</strong></a>. and proceeded to next step.</p>
<p><strong>Step 5 :</strong></p>
<p>Compiled the source code by typing</p>
<div class="highlight"><pre><span></span><code>make
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/abhishek@linux-f3qk-home-abhishek-elegance-colors_010.png" title="'make' command - Generating binaries"><img alt="abhishek\@linux-f3qk:-home-abhishek-elegance-colors_010" src="//thegeekyway.com/uploads/images/2013/12/abhishek@linux-f3qk-home-abhishek-elegance-colors_010.png"></a></p>
</div>
<p><strong>Step 6 :</strong></p>
<p>Installed the compiled source code by typing</p>
<div class="highlight"><pre><span></span><code>make install
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/abhishek@linux-f3qk-home-abhishek-elegance-colors_011.png" title="'make install' command - Installing generated binaries"><img alt="abhishek\@linux-f3qk:-home-abhishek-elegance-colors_011" src="//thegeekyway.com/uploads/images/2013/12/abhishek@linux-f3qk-home-abhishek-elegance-colors_011.png"></a></p>
</div>
<p>and successfully installed the application from the source code.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Screenshot-from-2013-12-10-111926.png" title="Installed application can be accessed from launcher"><img alt="Screenshot from 2013-12-10 11:19:26" src="//thegeekyway.com/uploads/images/2013/12/Screenshot-from-2013-12-10-111926.png"></a></p>
</div>
<p>This tutorial is containing the basics & simply gives hints on how you can compile any software from source code. There are many reasons by which error in configuring & compilation can occur & leaves you headbanging, like missing of shared libraries, no configuration script, unable to install dependencies ,etc. But somehow there's always a way to rectify the errors, if you know the basics.</p>Learning about Variables in C2013-12-09T22:30:00+05:302013-12-09T22:30:00+05:30sanyam_khuranatag:thegeekyway.com,2013-12-09:/variables-in-c/<p>Welcome to your second tutorial on the C language. In this tutorial, we'll be discussing about variables, their applications, their necessity both in general terms of programming and for C language as well. Variables in C are really very important. These are the fundamentals and you need to understand how …</p><p>Welcome to your second tutorial on the C language. In this tutorial, we'll be discussing about variables, their applications, their necessity both in general terms of programming and for C language as well. Variables in C are really very important. These are the fundamentals and you need to understand how variables in C are used and data in them can be manipulated.</p>
<p>Variables are where you store your data, you can assume them to be pretty much like containers (reserved memory locations) where you can store your things ( data ). In C or any programming language, it is essential that you define that variable is of what kind, ie it's data type.</p>
<p>The data types can be divided into two broad categories namely Fundamental Data types and Derived data types. Now we'll look in detail about both of these.</p>
<p><strong>Fundamental Data Types</strong></p>
<p>These are the data types which are built-in and not derived, that's why the word “Fundamental” has been used. There are five basic kind of Data types :-</p>
<ul>
<li>int – for integer data types</li>
<li>float – for floating point numbers (those with decimals)</li>
<li>char – for character type</li>
<li>double- same as integer type but include float also and is capable of holding more numbers (range is very large)</li>
<li>void – this stands for null value</li>
</ul>
<p><strong>Derived Data Types</strong></p>
<p>These are the data types derived from fundamental data types. Some of the derived data types are :</p>
<ul>
<li>Arrays</li>
<li>Pointers</li>
<li>Enumeration</li>
<li>Structures</li>
</ul>
<p>etc.</p>
<p><strong>Defining a Variable</strong></p>
<p>The following syntax should be used while defining variable in C :</p>
<div class="highlight"><pre><span></span><code><span class="n">data_type</span><span class="w"> </span><span class="n">variable_name</span><span class="p">;</span>
</code></pre></div>
<p>Here, data_type is the type of data which variable shall hold, and variable_name is any legal name you may give to the variable to access the information stored in it.</p>
<p>Now, we'll define a variable of type integer.</p>
<div class="highlight"><pre><span></span><code>int sam;
</code></pre></div>
<p>Here, we have defined a variable named sam which is capable of holding any integer type value.<br>
Now, we'll assign a value to this variable with assignment operator (=)</p>
<div class="highlight"><pre><span></span><code>sam = 10;
</code></pre></div>
<p>Now, our variable sam contains value 10.</p>
<p>Now, we'll see it in the entire program and output the value of variable.</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="nv">int</span><span class="w"> </span><span class="nv">sam</span><span class="c1">;</span>
<span class="nv">sam</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="c1">;</span>
<span class="nv">printf</span><span class="ss">(</span>“<span class="nv">Value</span><span class="w"> </span><span class="nv">of</span><span class="w"> </span><span class="nv">sam</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="o">%</span><span class="nv">d</span>”,<span class="w"> </span><span class="nv">sam</span><span class="ss">)</span><span class="c1">;</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="c1">;</span>
}
</code></pre></div>
<p>For the above code, we'll get the following output :</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/2.png" title="Output of variable in C tutorial code"><img alt="Output of variable in C tutorial code" src="//thegeekyway.com/uploads/images/2013/12/2.png"></a></p>
</div>
<p>(Note: Program are compiled in Turbo C with DosBox instead of gcc)</p>
<p>Note we've used %d , this just means that we want to print an integer value. In C, we need to tell the compiler what type of value is coming, so we need to have this %d for integer type value. Similar to %d, we use the following placeholders to instruct the compiler of upcoming data type :</p>
<p>\%d – integer (int)<br>
%c – character (char)<br>
%s – strings</p>
<p>\%f - float<br>
etc.</p>
<p>Observe, that in printf() function, we have used , and the variable name, and when this program is compiled and executed, we get the output , such that it prints Value of sam is 10 , the %d is now replaced by the value stored in our variable sam. This is because, %d was an instruction for the compiler so that it knows what type of value is coming in, so wherever you place this %d, and output an integer value, it would be replaced by value of that variable.</p>
<p>For example, see the following code snippet :</p>
<div class="highlight"><pre><span></span><code><span class="c1">#include<stdio.h></span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="nb nb-Type">int</span><span class="w"> </span><span class="n">sam</span><span class="p">;</span>
<span class="n">sam</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="err">“</span><span class="w"> </span><span class="n">Value</span><span class="w"> </span><span class="n">stored</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="o">%</span><span class="n">d</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="n">sam</span><span class="err">”</span><span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="n">sam</span><span class="p">);</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Here is the output for the above mentioned code snippet</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/1.png" title="Outputing with variables"><img alt="Output of variable in C tutorial code" src="//thegeekyway.com/uploads/images/2013/12/1.png"></a></p>
</div>
<p>(Note: Program are compiled in Turbo C with DosBox instead of gcc)</p>
<p>Note: Return type of our main function is of int type so we are returning value 0, this can be any value, but since we don't need to return anything, we generally return 0.</p>
<p>We'll carry this topic in the next tutorial as well, till then, you can try the codes from this tutorial and play with the code.</p>Understanding init.d Directory In Linux Filesystem.2013-12-07T13:08:00+05:302013-12-07T13:08:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-07:/whats-important-init-d-directory-linux/<p><strong><em>init.d</em></strong> is the sub-directory of <strong><em>/etc</em></strong> directory in Linux file system. init.d basically contains the bunch of start/stop scripts which are used to control (start,stop,reload,restart) the daemon while the system is running or during boot. If you look at <strong><em>/etc/init.d</em></strong> then you …</p><p><strong><em>init.d</em></strong> is the sub-directory of <strong><em>/etc</em></strong> directory in Linux file system. init.d basically contains the bunch of start/stop scripts which are used to control (start,stop,reload,restart) the daemon while the system is running or during boot. If you look at <strong><em>/etc/init.d</em></strong> then you will notice all the scripts for different services of your system.</p>
<p>The scripts within the directory varies as per the applications installed in your system. In server system you'll find many network related scripts while in desktop there will be the only basic ones like 'networking'.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/init_main_featureimage.png"><img alt="init_main_featureimage" src="//thegeekyway.com/uploads/images/2013/12/init_main_featureimage.png"></a></p>
</div>
<p>There are two types of scripts : K scripts & S scripts.</p>
<p>K scripts are known <strong>'kill'</strong> scripts while S scripts are known as <strong>'startup'</strong> scripts. Kill scripts always run before startup scripts. The configuration file (.conf) of these scripts are located under <strong><em>/etc/init</em></strong> & the scripts that are used as defaults are located under <strong><em>/etc/default</em></strong>.</p>
<!--more-->
<p>Within <em>/etc/init.d</em> you'll find the script <strong><em>rc.local</em></strong> ,this script is automatically executed after all primarily priortized scripts are executed. Which means first K scripts are executed, following by S scripts, then all other remaining init level scripts (if there's any) & finally rc.local script.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/init_pic2.png"><img alt="init_pic2" src="//thegeekyway.com/uploads/images/2013/12/init_pic2.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/init_pic3.png"><img alt="init_pic3" src="//thegeekyway.com/uploads/images/2013/12/init_pic3.png"></a></p>
</div>
<p>Now to control any script, you'll need the superuser (sudo/su) permission. You can manually control the scripts using console/terminal. The syntax for controlling the script is like :</p>
<div class="highlight"><pre><span></span><code># /etc/init.d/<script> <option>
</code></pre></div>
<p>Where \<script> stands for the name of the script located under /etc/init.d which you wanna control, and \<option> can be replaced by the control commands like : start,stop,reload,restart,force-reload.</p>
<p>Example :</p>
<div class="highlight"><pre><span></span><code><span class="n">sudo</span><span class="w"> </span><span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">init</span><span class="o">.</span><span class="n">d</span><span class="o">/</span><span class="n">pulseaudio</span><span class="w"> </span><span class="n">reload</span>
</code></pre></div>
<p>I hope this helps to understand /etc/init.d directory. This was basic understanding of init.d directory, there's more into it which will be covered soon.</p>C# Tutorial : Compiling C# Programmes using Notepad2013-12-05T17:13:00+05:302013-12-05T17:13:00+05:30Deep_Singhtag:thegeekyway.com,2013-12-05:/csharp-tutorial-compiling-csharp-programmes-using-notepad/<h2>About C#</h2>
<p>C# is a multi-paradigm object oriented language developed by Mircrosoft in the year 2000. C# has its major implementations in dotNet framework, Mono, DotGNU. In this tutorial we will print a simple line "TheGeekyWay.com" using a notepad and C# command line compiler.</p>
<blockquote>
<p>In order to continue with …</p></blockquote><h2>About C#</h2>
<p>C# is a multi-paradigm object oriented language developed by Mircrosoft in the year 2000. C# has its major implementations in dotNet framework, Mono, DotGNU. In this tutorial we will print a simple line "TheGeekyWay.com" using a notepad and C# command line compiler.</p>
<blockquote>
<p>In order to continue with the C# tutorial you will need the latest version of .NET Framework SDK installed on your machine. You can download latest version of .NET Framework <a href="http://www.microsoft.com/en-in/download/details.aspx?id=19988">here</a>.</p>
</blockquote>
<p>First of all you need to create a folder where you can save your C# programme, you can also save the file on Desktop but its always a good habit to keep your files organized, so I am creating a folder named TheGeekyWay in the C: drive.</p>
<!--more-->
<blockquote>
<p><strong>Remember :</strong> C# is a case sensitive language. So everything should be same while writing the below code, any syntactical changes will bring down an error in compiling.</p>
</blockquote>
<p>So here is the following C# code (don't worry the code will be explained in detail in next tutorial, for now we just focus on how to compile and run a c# code in command prompt)</p>
<div class="highlight"><pre><span></span><code>public class TheGeekyWay
{
public static void Main()
{
System.Console.WriteLine("Welcome to TheGeekyWay.com");
System.Console.ReadLine();
}
}
</code></pre></div>
<p>Next step is to go to start button and type cmd and run as administrator. The reason using cmd is that many .NET tools can be used or scripted using DOS batch files.</p>
<p>Now in cmd type the following lines in cmd and reach to the directory of Framework. The reason for showing the directory is to know the recent version of C# compiler.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/CTUT1.1.png" title="C# Tutorial : To run C# programme using Notepad (Image 1)"><img alt="C# Tutorial : To run C# programme using Notepad Image 1" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.1.png"></a></p>
</div>
<p>now next step is to go in the directory of your latest .NET version, for me its v4.0.30319, so we use the following command line to go in the latest .NET folder directory.</p>
<div class="highlight"><pre><span></span><code>cd v4.0.30319
</code></pre></div>
<p>you can also visit the following code to view all the help related to C# compiler, remember to type the code in v4.0.30319 directory (or whatever your latest version of .NET compiler is).</p>
<div class="highlight"><pre><span></span><code>csc help
</code></pre></div>
<p>now the main part of the tutorial, the compilation code, here is the compilation code to compile your c# code.</p>
<div style="text-align:center">
<p><img alt="C#TUT1.2" class="alignnone size-medium wp-image-302" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.2.png"></p>
</div>
<p>confused ! don't worry, here is snapshot of the complete code using cmd.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/CTUT1.3.png" title="C# Tutorial : To run C# programme using Notepad (Image 2)"><img alt="C#" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.3.png" title="C# Tutorial : To run C# programme using Notepad"></a></p>
</div>
<p>here is the complete explanation of the C# code</p>
<blockquote>
<ul>
<li><strong>csc :</strong> csc stands for <strong>C</strong> <strong>S</strong>harp <strong>C</strong>ompiler.</li>
<li><strong>/t:exe :</strong> It means the extension of your output, generally "exe" is used.</li>
<li><strong>\<Your output destination path>\<Your file name> :</strong> Set the destination path and the file name where you want to save the file.</li>
<li><strong>\<Your Source Code path> :</strong> Set the path where your source code is saved.</li>
</ul>
</blockquote>
<p>and here is the output</p>
<div style="text-align:center">
<p><img alt="C#TUT1.4" src="//thegeekyway.com/uploads/images/2013/12/CTUT1.4.png" title="C# Tutorial : To run C# programme using Notepad " "C# Tutorial : To run C# programme using Notepad (Image 3)"></p>
</div>
<p>There can be various problems that you can arrive into, since C# is a case sensitive language, so did you typed the code exactly same as mentioned in the box, sometimes there can be permissions problem, sometimes there can be a problem in the compiler line.</p>
<p>That's it for now folks ! I hope you enjoyed the article on compiling C# Programmes using Notepad and command line. If any problem arises in the compilation or any sort of error comes during compilation, you can post it as a comment below and TheGeekyWay team will try to solve it ASAP.</p>
<h2>You can click the following social icons and share this article with your friends.</h2>
<hr>How To Display Raspberry Pi On VGA Monitor/TV ?2013-12-04T23:43:00+05:302013-12-04T23:43:00+05:30Abhishek_Rawaltag:thegeekyway.com,2013-12-04:/connect-raspberry-pi-vga-monitortv/<p>Raspberry Pi, A \$25 ARM GNU/Linux box comes with RCA & HDMI port. Connecting Raspberry Pi with HDMI supported TV/monitor is cake walk. But, unfortunately some of us still have monitors with only VGA connectors. Raspberry Pi didn't included VGA connectors keeping power consumption of the board in mind …</p><p>Raspberry Pi, A \$25 ARM GNU/Linux box comes with RCA & HDMI port. Connecting Raspberry Pi with HDMI supported TV/monitor is cake walk. But, unfortunately some of us still have monitors with only VGA connectors. Raspberry Pi didn't included VGA connectors keeping power consumption of the board in mind. Fortunately, there are plenty of converters available in the online stores which lets you convert HDMI(Digital) signal to VGA(Analog) signal.</p>
<p>Following are the detailed steps which will help you to connect RPi with VGA monitors & display RPi on VGA monitors without any problem.</p>
<p><strong>1) HDMI to VGA Converter Adapter :</strong></p>
<p>You have to purchase power free HDMI to VGA adapter. Here's the link of two HDMI\<->VGA adapters which have been tested by me that just works.</p>
<!--more-->
<p>i) <a href="https://www.modmypi.com/pi-vew-raspberry-pi-hdmi-to-vga-converter">ModMyPi | PiView</a></p>
<p>ii) <a href="https://www.crazypi.com/RASPBERRY-PI-HDMI-TO-VGA">CrazyPi | PiEyes</a></p>
<p>Note that these adapters doesn't support audio from HDMI port, so external speakers are required to connect onboard. Since, I don't use RPi for entertainment purpose, I never cared much about audio signals.</p>
<p><strong>2) Editing Config.txt located in Raspberry Pi distro :</strong></p>
<p>Make SDHC bootable by installing RPi distribution into it. After installing RPi distribution in SDHC, insert the SD in computer & open <em>config.txt</em> which is located under /boot directory.There will be set of lines for the different functions. We have to edit config.txt file inorder to obtain picture on VGA monitor.</p>
<p>Uncomment following four lines in config.txt by removing '#' located at start of the line. (check Images below)</p>
<div class="highlight"><pre><span></span><code>hdmi_force_hotplug=1
hdmi_drive=2
hdmi_group=1
hdmi_mode=1
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Image.png"><img alt="Image" src="//thegeekyway.com/uploads/images/2013/12/Image.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/Image2.png"><img alt="Image2" src="//thegeekyway.com/uploads/images/2013/12/Image2.png"></a></p>
</div>
<p><strong>hdmi_force_hotplug=1</strong> pretends that HDMI device is always attached</p>
<p><strong>hdmi_drive=2</strong> forces monitor to HDMI mode so that sound signals are carried from HDMI cables.</p>
<p><strong>hdmi_group</strong> specifies whether monitor is DMT type (Computers) or CEA type (TV)</p>
<p><strong>hdmi_mode</strong> specifies the resolution of monitor.</p>
<p><strong>For hdmi_group value selection :</strong></p>
<p>If you're using output as Computer monitor then replace value '1' with '2', so the new config will be like :</p>
<div class="highlight"><pre><span></span><code>hdmi_group=2
</code></pre></div>
<p>(Select value 1 for TV, Select value 2 for monitor)</p>
<p><strong>For hdmi_mode value selection :</strong></p>
<p>Now open <a href="http://elinux.org/RPiconfig#Video_mode_options">eLinux RPi config</a> scroll down, there in hdmi_mode two tables are given, select the correct resolution as per your monitor. (Table1 if you're using TV & Table2 if you're using Monitor)<br>
Since my monitor's resolution is 1366x768 px, hdmi_mode=39 fits me the best.So, the modified config.txt will be like.</p>
<div class="highlight"><pre><span></span><code>hdmi_mode=39
</code></pre></div>
<p>So, overall my uncommented lines will look something like :</p>
<div class="highlight"><pre><span></span><code>hdmi_force_hotplug=1
hdmi_drive=2
hdmi_group=2
hdmi_mode=39
</code></pre></div>
<p>Now, save the edited config.txt</p>
<p><strong>3) Connection of RPi with HDMI\<->VGA adapter.</strong></p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/12/HDMI-VGA.jpg"><img alt="HDMI-VGA" src="//thegeekyway.com/uploads/images/2013/12/HDMI-VGA.jpg"></a></p>
</div>
<p>Happy hacking.</p>Privacy Policy2013-11-29T19:19:00+05:302013-11-29T19:19:00+05:30sanyam_khuranatag:thegeekyway.com,2013-11-29:/privacy-policy/<p>This Privacy Policy governs the manner in which TheGeekyWay collects, uses, maintains and discloses information collected from users (each, a "User") of the <a href="http://www.TheGeekyWay.com">http://www.TheGeekyWay.com</a> website ("Site"). This privacy policy applies to the Site and all products and services offered by TheGeekyWay.</p>
<p><strong>Personal identification information</strong></p>
<p>We may collect …</p><p>This Privacy Policy governs the manner in which TheGeekyWay collects, uses, maintains and discloses information collected from users (each, a "User") of the <a href="http://www.TheGeekyWay.com">http://www.TheGeekyWay.com</a> website ("Site"). This privacy policy applies to the Site and all products and services offered by TheGeekyWay.</p>
<p><strong>Personal identification information</strong></p>
<p>We may collect personal identification information from Users in a variety of ways, including, but not limited to, when Users visit our site, subscribe to the newsletter, and in connection with other activities, services, features or resources we make available on our Site. Users may be asked for, as appropriate, name, email address, mailing address. Users may, however, visit our Site anonymously. We will collect personal identification information from Users only if they voluntarily submit such information to us. Users can always refuse to supply personally identification information, except that it may prevent them from engaging in certain Site related activities.</p>
<p><strong>Non-personal identification information</strong></p>
<p>We may collect non-personal identification information about Users whenever they interact with our Site. Non-personal identification information may include the browser name, the type of computer and technical information about Users means of connection to our Site, such as the operating system and the Internet service providers utilized and other similar information.</p>
<p><strong>Web browser cookies</strong></p>
<p>Our Site may use "cookies" to enhance User experience. User's web browser places cookies on their hard drive for record-keeping purposes and sometimes to track information about them. User may choose to set their web browser to refuse cookies, or to alert you when cookies are being sent. If they do so, note that some parts of the Site may not function properly.</p>
<p><strong>How we use collected information</strong></p>
<p>TheGeekyWay may collect and use Users personal information for the following purposes:</p>
<ul>
<li><em>- To improve customer service</em><br>
Information you provide helps us respond to your customer service requests and support needs more efficiently.</li>
<li><em>- To personalize user experience</em><br>
We may use information in the aggregate to understand how our Users as a group use the services and resources provided on our Site.</li>
<li><em>- To improve our Site</em><br>
We may use feedback you provide to improve our products and services.</li>
<li><em>- To send periodic emails</em><br>
We may use the email address to respond to their inquiries, questions, and/or other requests. If User decides to opt-in to our mailing list, they will receive emails that may include company news, updates, related product or service information, etc. If at any time the User would like to unsubscribe from receiving future emails, we include detailed unsubscribe instructions at the bottom of each email or User may contact us via our Site.</li>
</ul>
<p><strong>How we protect your information</strong></p>
<p>We adopt appropriate data collection, storage and processing practices and security measures to protect against unauthorized access, alteration, disclosure or destruction of your personal information, username, password, transaction information and data stored on our Site.</p>
<p><strong>Sharing your personal information</strong></p>
<p>We do not sell, trade, or rent Users personal identification information to others. We may share generic aggregated demographic information not linked to any personal identification information regarding visitors and users with our business partners, trusted affiliates and advertisers for the purposes outlined above.We may use third party service providers to help us operate our business and the Site or administer activities on our behalf, such as sending out newsletters or surveys. We may share your information with these third parties for those limited purposes provided that you have given us your permission.</p>
<p><strong>Third party websites</strong></p>
<p>Users may find advertising or other content on our Site that link to the sites and services of our partners, suppliers, advertisers, sponsors, licensors and other third parties. We do not control the content or links that appear on these sites and are not responsible for the practices employed by websites linked to or from our Site. In addition, these sites or services, including their content and links, may be constantly changing. These sites and services may have their own privacy policies and customer service policies. Browsing and interaction on any other website, including websites which have a link to our Site, is subject to that website's own terms and policies.</p>
<p><strong>Changes to this privacy policy</strong></p>
<p>TheGeekyWay has the discretion to update this privacy policy at any time. When we do, we will revise the updated date at the bottom of this page. We encourage Users to frequently check this page for any changes to stay informed about how we are helping to protect the personal information we collect. You acknowledge and agree that it is your responsibility to review this privacy policy periodically and become aware of modifications.</p>
<p><strong>Your acceptance of these terms</strong></p>
<p>By using this Site, you signify your acceptance of this policy. If you do not agree to this policy, please do not use our Site. Your continued use of the Site following the posting of changes to this policy will be deemed your acceptance of those changes.</p>
<p><strong>Contacting us</strong></p>
<p>If you have any questions about this Privacy Policy, the practices of this site, or your dealings with this site, please contact us at:<br>
<a href="http://www.TheGeekyWay.com">TheGeekyWay</a><br>
<a href="http://www.TheGeekyWay.com">http://www.TheGeekyWay.com</a><br>
admin\@thegeekyway.com</p>
<p>This document was last updated on November 29, 2013</p>Getting Started With C language2013-11-19T13:26:00+05:302013-11-19T13:26:00+05:30sanyam_khuranatag:thegeekyway.com,2013-11-19:/getting-started-c-language/<p><strong>Introduction to C language</strong></p>
<p>C language is a middle level language developed by Dennis Ritchie in 1969 and the most secure OS "UNIX" is also coded in C language. It has just turned the world of Coding giving new level to programming, and introducing us to what is called "Structured …</p><p><strong>Introduction to C language</strong></p>
<p>C language is a middle level language developed by Dennis Ritchie in 1969 and the most secure OS "UNIX" is also coded in C language. It has just turned the world of Coding giving new level to programming, and introducing us to what is called "Structured Programming".</p>
<p>It is very important to note here that C is a Case Sensitive language, what this means is, TgW , tGw, TGw are all different. Don't worry if it's sound confusing, you'll be able to understand it better once we begin to learn about Variables.</p>
<p>Wait! before we start, the question arises :-</p>
<p><strong>Why learn C language?</strong></p>
<p>Well, learning C language would improve your logical skills, and many programming languages are directly or indirectly derived from C. This means, if you learn C language, you can learn other programming languages like C++, Java, JavaScript, Python, PHP, Perl and many others, much faster and it would be simple for you.</p>
<!--more-->
<p><strong>Pre Requisites for this and the coming Tutorials :-</strong></p>
<p>To compile the C language programs, you need something called compiler. It doesn't matter if you're using Linux, Windows, or Mac, you'll surely get a C compiler. If you're Windows user, get the best compiler from the start ie gcc compiler. I wouldn't recommend any other like Turbo C etc. they are not good when compared to gcc. Download gcc compiler from here :-<br>
http://gcc.gnu.org/<br>
If you're on Linux you'll probably already have gcc compiler installed. To check if you have gcc compiler already installed please type the following command in the terminal</p>
<div class="highlight"><pre><span></span><code>$<span class="w"> </span>g++<span class="w"> </span>-v
</code></pre></div>
<p>If you've gcc installed, you'll get something like this, as in my terminal window.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2013/11/C_Tut1.1.png" title="checking for gcc compiler in linux"><img alt="checking for gcc compiler in linux" src="//thegeekyway.com/uploads/images/2013/11/C_Tut1.1.png"></a></p>
</div>
<p>Much of theory, isn't it? Ok, let's get started with some code.</p>
<p><strong>First C Program</strong></p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
<span class="nv">int</span><span class="w"> </span><span class="nv">main</span><span class="ss">()</span>
{
<span class="nv">printf</span><span class="ss">(</span><span class="s2">"Hello World"</span><span class="ss">)</span><span class="c1">;</span>
}
</code></pre></div>
<p>Just compile it in your gcc compiler and you'll get Hello World printed in the output console.</p>
<p>If you need help in compilation and execution, wait for one more tutorial.</p>
<p>Now, we'll just break down each and every part of the program and analyse it.<br>
``</p>
<div class="highlight"><pre><span></span><code>#<span class="k">include</span><span class="o"><</span><span class="nv">stdio</span>.<span class="nv">h</span><span class="o">></span>
</code></pre></div>
<p>``In this command, we ask the compiler to include a file (known as header file) which contains some pre defined fuctions which are needed to run the program.<br>
We, request to add file stdio which is Standard Input/Output console file, as we need to display the print on the output console.<br>
"#" (hash/pound) symbol here represents the pre-processor directive ie command started with a # sign goes to the pre-processor, means these statements are carried out first, before the program goes for compilation.</p>
<div class="highlight"><pre><span></span><code>main()
</code></pre></div>
<p>This is a function in programming language, they are just meant to divide the program into modules. The main function of any program is of uttermost importance, as the program execution starts from main function.</p>
<div class="highlight"><pre><span></span><code>printf()
</code></pre></div>
<p>This is also a function, it is the standard output function defined under stdio header file. Whatever we give in the paranthesis between the quotes is printed on the screen.</p>
<p>Note, that we have used int before main function , this means that this function would return a value of int type, and that's the reason we have given return 0 at the end.</p>
<p>Also note, that each statement in C would terminate with a semicolon. If you forget to put a semicolon, compiler would report an error.</p>
<p>You can now continue to <a href="http://www.thegeekyway.com/variables-in-c/" title="Learn about variables in C">Learn about variables in C</a></p>About Us2013-11-16T05:48:00+05:302013-11-16T05:48:00+05:30sanyam_khuranatag:thegeekyway.com,2013-11-16:/about-us/<p>Welcome to the About Us page, finally you want to know us, and it matters a lot to us. We are a team of Tech Enthusiasts, Coders and Open Source lovers.</p>
<p>This blog is an effort to bring people closer to the world of Technology and make them comfortable with …</p><p>Welcome to the About Us page, finally you want to know us, and it matters a lot to us. We are a team of Tech Enthusiasts, Coders and Open Source lovers.</p>
<p>This blog is an effort to bring people closer to the world of Technology and make them comfortable with its various aspects.</p>
<h5>Here are our current Authors :</h5>
<p><a href="http://www.thegeekyway.com/author/Sanyam_Khurana/" title="View all posts by Sanyam Khurana"><strong>Sanyam Khurana</strong></a></p>
<p>Sanyam graduated from Georgia Tech, Atlanta, US with a Master's degree in Computer Science. He goes by "CuriousLearner" all over the web & has spoken at many international conferences. He likes to contribute to FOSS. He is listed as an individual contributor of the Django project, bug-triager for CPython & has his name listed in the credits section of every browser ever released by Mozilla. In his free time, he likes to improve his guitar skills or binge-watch series. You can find him on <a href="https://twitter.com/ErSanyamKhurana">Twitter</a>.</p>
<h5>Our Ex-Contributors:</h5>
<p><a href="http://www.thegeekyway.com/author/anoop/" title="View all post by Anoop"><strong>Anoop Kumar</strong></a></p>
<p>A java professional - have keen interest in open source programming. Likes to share knowledge about java, jQuery and linux stuffs. <a href="https://plus.google.com/+AnoopKumar" title="Anoop Kumar on Google+">Google+</a>, <a href="https://www.facebook.com/ianoopkumar" title="Anoop Kumar on Facebook">Facebook</a>, <a href="https://twitter.com/theanoopk" title="Anoop Kumar on Twitter">Twitter</a></p>
<p><a href="http://www.thegeekyway.com/author/Peeyush_gupta/"><strong>Peeyush Gupta</strong></a></p>
<p>Peeyush is an open-source enthusiast who is in love with Python! He is a virtualization professional and can be found at <a href="https://www.facebook.com/guptapeeyush">Facebook</a>, <a href="https://plus.google.com/u/0/+PeeyushGupta91/posts">Google+</a>.</p>
<p><a href="http://www.thegeekyway.com/author/abhishek_rawal/" title="View all posts by Abhishek Rawal"><strong>Abhishek Rawal</strong></a></p>
<p>An Opensource evangelist,Electronics Engineering student & an aspiring Embedded Systems Engineer who likes tweaking open hardware like Arduino & Raspberry Pi, passionate about processors, likes writing articles on Opensource & Linux related software and hardware, knows little bit of C & bash scripting & dual-boots openSUSE and Ubuntu on his desktop. <a href="https://plus.google.com/+AbhishekRawal" title="Add him in circle">Google+</a> ,<a href="https://twitter.com/OpenSourceMoFo" title="Follow him on Twitter">Twitter</a></p>
<p><strong>
<a href="http://www.thegeekyway.com/author/Deep_Singh/" title="View all post by Deep">Deep Singh</a></strong></p>
<p>A tech enthusiastic, prefinal year student of GGSIPU university, having keen interest in dot.net and database administration. You can find me on <a href="https://plus.google.com/112233042255615343235/posts">Google+</a> , <a href="https://www.facebook.com/DPSC.2007">Facebook</a> and <a href="https://twitter.com/DPSC_">Twitter</a></p>
<p><a href="http://www.thegeekyway.com/author/gagan93/" title="View all posts by Gagandeep Singh"><strong>Gagandeep Singh</strong></a></p>
<p>Pre-final year student of GTBIT, GGSIPU, New Delhi. Interested in Programming languages like C, C++,Java and now started with Android. You can find him on <a href="https://plus.google.com/115019687639113613369/posts">Google+</a> and <a href="https://www.fb.com/gagan93">Facebook</a></p>