Tuesday, February 28, 2017

Count Server Socket

Definition: Program to demonstrate use of Page views count on server.
Count server program will increment page view counter for every client request.

Count Server Socket Program

import java.io.*;
import java.net.*;

public class CountServer {

    public static void main(String[] args) throws IOException {

        ServerSocket ss = new ServerSocket(1234);
        Socket cs = null;
        System.out.println("Waiting for connection.....");
        int count=0;
        while(true){
            cs = ss.accept();          
            count++;
            System.out.println("Total visitors:"+count);
            cs.close();
        }
        //ss.close();
    }
}

Output on Server screen

Waiting for connection.....
Total visitors:1

//Validations and exception handling to be added.

Count Client Socket Program

/* This program will just create socket with server. Note: try-catch clauses are not added to keep logic simple…*/

import java.io.*;
import java.net.*;

public class CountClient
{
   public static void main(String[] args) throws IOException
   {
         Socket s1 = new Socket("localhost", 1234);
         System.out.println("Hello");
         s1.close();
   }
}

Output on Client screen

Hello

Monday, February 27, 2017

Chat Server Socket

Following two socket programs demonstrate basic Chat application. You need to add additional functionalities and exception handling according to your needs.

Chat Server Socket Program

import java.io.*;
import java.net.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        ServerSocket ss = new ServerSocket(1234);
        Socket cs = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Waiting for connection.....");
        cs = ss.accept();      
        BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        PrintWriter out = new PrintWriter(cs.getOutputStream(), true);
        String inputLine, serverInput;
        while(true){
            inputLine = in.readLine();
            System.out.println("Client: " + inputLine);
            System.out.print("Server:");
            serverInput = br.readLine();
            out.println(serverInput);
        }
        out.close();
        in.close();
        cs.close();
        ss.close();
    }
}
//You may add exit condition in while loop of server socket.

Output on server screen:

Waiting for connection.....
Client: hello
Server:hello client
Client: how are you?
Server:I am fine...what about you?


Chat Client Socket Program

import java.io.*;
import java.net.*;

public class ChatClient {
    public static void main(String[] args) throws IOException
   {
         Socket s1 = new Socket("localhost", 1234);
         DataInputStream is = new DataInputStream(s1.getInputStream());
        // String request = "Hello";
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
         System.out.print("Client:");
         String request = br.readLine();
         while(!(request.contentEquals("bye"))){
            out.println(request);
            String reply = is.readLine();
            System.out.println("Server:" + reply);
            System.out.print("Client:");
            request = br.readLine();
         }
         out.close();                   
         is.close();
         s1.close();
   }
}

Output on Client Screen:

Client:hello
Server:hello client
Client:how are you?
Server:I am fine...what about you?
Client:bye

Echo Client Server Socket

Following two programs are written in JAVA. You have to run Server program first, and then run client program. Server program will reply the message sent by client.


Simple Echo Server Program

import java.io.*;
import java.net.*;

public class SimpleEchoServer1 {
 public static void main(String[] args) throws IOException    {

    ServerSocket ss = new ServerSocket(123);
    Socket cs = null;     
    System.out.println ("Waiting for connection.....");
   
    cs = ss.accept();     
    System.out.println ("Waiting for input.....");

    BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
    PrintWriter out = new PrintWriter(cs.getOutputStream(),true);

    String inputLine = in.readLine();
    out.println(inputLine);
    System.out.println ("Reply sent...");

    out.close();    
    in.close();    
    cs.close();    
    ss.close();
   }
}

Output on server:

Waiting for connection.....
Waiting for input.....
Reply sent...


Simple Echo Client Program

import java.io.*;
import java.net.*;

public class SimpleEchoClient1 {
   public static void main(String[] args) throws IOException   {
         Socket s1 = new Socket("localhost", 123);
         DataInputStream is = new DataInputStream(s1.getInputStream());
         System.out.print("Enter your string:");
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String request = br.readLine();
    
         PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
         out.println(request);       
         String reply = is.readLine();
         System.out.println("Echo from Server:" + reply);
         
         out.close();           
      is.close();         
      s1.close();
   } 
}

Output on Client Screen:

Enter your string:hello
Echo from Server:hello



Saturday, February 25, 2017

RSA Decryption

#include<stdio.h>
int main()
{
long int n=187, d=23, m, c=11;
int i;
//Assuming input - Cipher text c=11.

printf("Sample Data:d=23, n=187, c=11\n");

m = 1;

for(i=0; i<d; i++){
m = m * c%n;
}
m = m%n;

printf("Plain Text of %i = %i",c,m);
return 0;
}

Output of program:

Sample Data:d=23, n=187, c=11
Plain Text of 11 = 88

RSA Encryption

Sample values taken for execution of RSA encryption are as under:

      e=7, d=23, n=187 and m=88

#include<stdio.h>
int main()
{
  long int e=7,n=187,d=23, m=88, c;
  int i;
  //you may read e,n,d and m from user.

  printf("Sample Data:e=7, d=23, n=187, m=88\n");

  c=1;
  for(i=0; i<e; i++)
{
    c = c*m%n;
  }
  c = c%n;
  printf("Cipher Text of %i = %i \n",m, c);

  return 0;
}

Output of program:

Sample Data:e=7, d=23, n=187, m=88
Cipher Text of 88 = 11



Monday, February 20, 2017

Random between 0 to 100.

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i;
   time_t my_sys_time;
   //To set random number generator as per system time 
   srand((unsigned) time(&my_sys_time));

   //To print 10 random numbers from 0 to 100
   for( i = 0 ; i < 10 ; i++ ) 
   {
      printf("%d ", rand()%100);
   }
   return(0);
}
Output of program
16 45 14 21 60 18 65 3 41 34

Thursday, February 16, 2017

Greatest Common Divisor(GCD) of given 2 numbers

Write a C program to find Greatest Common Divisor (GCD) of given 2 numbers.
GCD is also known as Greatest Common Factor (GCF)
GCD is also known as Highest Common Factor (HCF)

#include <stdio.h>
int main()
{
  int num1, num2, i, j, temp, gcd;

  printf("Enter number 1:");
  scanf("%d", &i);

  printf("Enter number 2:");
  scanf("%d", &j);

  num1 = i;
  num2 = j;

  while (num2 != 0)
  {
    temp = num2;
    num2 = num1 % num2;
    num1 = temp;
  }

  gcd = num1;
  printf("Greatest Common Divisor(GCD) of %d and %d is %d.\n", i,j,gcd);
  return 0;
}

Output of program

Enter number 1:20
Enter number 2:30
Greatest Common Divisor(GCD) of 20 and 30 is 10.