Kontera

Sunday, November 25, 2012

TCP Client Server Implementation in C

 TCPServer.c

#include "sys/types.h"
#include
#include
#include
#include
void main(){
 
  int sfd,clisize,cfd,port;
  struct sockaddr_in server,client;
  char msg1[50]="",msg2[50]="";
  sfd = socket(AF_INET,SOCK_STREAM,0);
  if(sfd==-1){
    printf("\nCannot Create Socket");
  }
  printf("\nEnter the port address:");
  scanf("%d",&port);
  server.sin_family = AF_INET;
  server.sin_port = port;
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  if(bind(sfd,(struct sockaddr *)&server,sizeof(server))!=0){
    printf("\nCannot Bind");
  }
  if(listen(sfd,0)!=0){
    printf("\nError In Listening");
  }
  printf("\nServer Ready and Waiting for Client.............\n");
  clisize = sizeof(client);
  cfd = accept(sfd,(struct sockaddr *)&client,&clisize);
  if(cfd==-1){
    printf("\nCannot Accept client Socket");
  }
  for(;;){
  recv(cfd,msg1,sizeof(msg1),0);
  printf("\nClient:%s",msg1);
  printf("\nServer :");
  scanf("%s",msg2);
  send(cfd,msg2,sizeof(msg2),0);
   if(strcmp(msg2,"end")==0)
    break;
  }
  close(sfd);
  close(cfd);
}


TCPClient.c

#include
#include
#include
#include
#include
void main(){
  struct sockaddr_in server,client;
  int sfd,port;
  char msg1[50]="",msg2[50]="";
  sfd = socket(AF_INET,SOCK_STREAM,0);
  if(sfd ==-1){
    printf("\nCannot Create socket");
  }
  printf("\nEnter the port address:");
  scanf("%d",&port);
  server.sin_family = AF_INET;
  server.sin_port = port;
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  printf("\nClient Ready");
  if(connect(sfd,(struct sockaddr *)&server,sizeof(server))!=0){
    printf("\nCannot Connect");
  }
  for(;;){
  printf("\nClient:");
  scanf("%s",msg1);
  send(sfd,msg1,sizeof(msg1),0);
  recv(sfd,msg2,sizeof(msg2),0);
  printf("\nServer:%s",msg2);
  if(strcmp(msg2,"end")==0)
    break;
 
 
  }
  close(sfd);
}


OUPUT
sarju@krishnakripa:~$ gcc TCPServer.c -o server
sarju@krishnakripa:~$ ./server

Enter the port address:2002

Server Ready and Waiting for Client.............

Client:hi
Server :hi

Client:end
Server :end
sarju@krishnakripa:~$

sarju@krishnakripa:~$ gcc TCPClient.c -o client
sarju@krishnakripa:~$ ./client

Enter the port address:2002

Client Ready
Client:hi
 
Server:hi
Client:end

Server:endsarju@krishnakripa:~$



Monday, November 19, 2012

Data Structures Notes

Saturday, November 17, 2012

Qick Sort Using Java

/* Implementation of QUICK SORT ALGORITHM using JAVA
Date:         18/11/2012
Author :     Sarju S */

import java.util.*;
import java.io.*;

public class QuickSort{
    public static void main(String[] args) {
        try{
        System.out.println("\nEnter the total number of element to sort:");
        DataInputStream din  = new DataInputStream(System.in);
        din = new DataInputStream(System.in);
        int totalNumbers = Integer.parseInt(din.readLine());
        int array[] = new int[totalNumbers];
        System.out.println("Enter the elements: ");
        for(int i=0;i            array[i] = Integer.parseInt(din.readLine());
        }
        recursiveQuickSort(array, 0, array.length-1);
        System.out.println("The following array should be sorted: ");
        printList(array);
        System.exit(0);
        }
        catch(Exception e){
        }
    }

    public static void recursiveQuickSort(int[] list, int first, int last) {
        if(first < last)
        {
            int p = partition(list, first, last);
            printList(list);
            recursiveQuickSort(list, first, p-1);
            recursiveQuickSort(list, p+1, last);
        }
    }

    public static int partition(int[] list, int first, int last) {
        int p = first;
        for(int n = p+1; n <= last; n++)
            if(list[n] < list[p])
            {
                swap(list, n, p+1);
                swap(list, p, p+1);
                p++;
            }
        return p;
    }

    private static void swap(int[] list, int index1, int index2) {
        int temp = list[index1];
        list[index1] = list[index2];
        list[index2] = temp;
    }

    protected static void printList(int[] list) {
        for(int n = 0; n < list.length; n++)
            System.out.print(list[n]+" ");
        System.out.println();
    }
}

Tries


  •  The standard Trie for a set of strings S is an ordered tree such that:
    •   Each node but the root is labeled with a character 
    • The children of a node are alphabetically ordered 
    • The paths from the external nodes to the root yield the strings of S
  •   Example: standard Trie for the set of strings


Java Code:

/* Implementation of Trie using JAVA
Date:         18/11/2012
Author :     Sarju S
*/
import java.io.*;
class TrieNode
{
    char letter;
    TrieNode[] links;
    boolean fullWord;
  
    TrieNode(char letter, boolean fullWord)
    {
        this.letter = letter;
        links = new TrieNode[26];//For storing 26 English Alphabets
        this.fullWord = fullWord;
    }
}
public class Trie
{   
    /*Function for creating Empty Trie*/
    static TrieNode createTree()
    {
        return(new TrieNode('\0', false));
    }
   /*Function for insertion into Trie*/
    static void insertWord(TrieNode root, String word)
    {
        int offset = 97;//ASCII Value of 'a'
        int l = word.length();
        char[] letters = word.toCharArray();
        TrieNode curNode = root;
      
        for (int i = 0; i < l; i++)
        {
            if (curNode.links[letters[i]-offset] == null)
                curNode.links[letters[i]-offset] = new TrieNode(letters[i], i == l-1 ? true : false);
            curNode = curNode.links[letters[i]-offset];
        }
    }
     /*Function for search a particular word from Trie*/
    static boolean find(TrieNode root, String word)
    {
        char[] letters = word.toCharArray();
        int l = letters.length;
        int offset = 97;//ASCII Value of 'a'
        TrieNode curNode = root;
      
        int i;
        for (i = 0; i < l; i++)
        {
            if (curNode == null)
                return false;
            curNode = curNode.links[letters[i]-offset];
        }
      
        if (i == l && curNode == null)
            return false;
      
        if (curNode != null && !curNode.fullWord)
            return false;
      
        return true;
    }
    /*Function for Printing Trie*/
    static void printTree(TrieNode root, int level, char[] branch)
    {
        if (root == null)
            return;
      
        for (int i = 0; i < root.links.length; i++)
        {
            branch[level] = root.letter;
            printTree(root.links[i], level+1, branch);  
        }
      
        if (root.fullWord)
        {
            for (int j = 1; j <= level; j++)
                System.out.print(branch[j]);
            System.out.println();
        }
    }
  
    public static void main(String[] args)
    {
        TrieNode tree = createTree();
           String words,searchWord;
        int choice;
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        try{
            do{
                System.out.print("\nMENU\n1:Insert\n2.Search\n3.Print\n4.Exit\n");
                System.out.println("Enter Your Choice:");
                choice = Integer.parseInt(in.readLine());
                switch(choice){
                    case 1:    System.out.println("Enter the word to insert :");
                            words = in.readLine();
                            insertWord(tree, words);
                            break;
                    case 2:    System.out.println("Enter the word to search :");
                            searchWord = in.readLine();
                             if (find(tree, searchWord))
                                System.out.println("The word was found");
                             else
                                System.out.println("The word was NOT found");
                            break;
                    case 3:    char[] branch = new char[50];
                            System.out.println("The Trie");
                            printTree(tree, 0, branch);
                    }
                }while(choice<4 br="br">            }
        catch(Exception e){
        }
       
    }
}

 OUPUT

C:\Users\Sarju>javac trie.java

C:\Users\Sarju>java Trie

MENU
1:Insert
2.Search
3.Print
4.Exit
Enter Your Choice:
1
Enter the word to insert :
sss

MENU
1:Insert
2.Search
3.Print
4.Exit
Enter Your Choice:
1
Enter the word to insert :
www

MENU
1:Insert
2.Search
3.Print
4.Exit
Enter Your Choice:
3
The Trie
sss
www

MENU
1:Insert
2.Search
3.Print
4.Exit
Enter Your Choice:
2
Enter the word to search :
kkk
The word was NOT found

MENU
1:Insert
2.Search
3.Print
4.Exit
Enter Your Choice:4

C:\Users\Sarju>

                For More Details : Click Here