Wednesday, November 7, 2012

Web Server in C

I implemented a web server in C language using only the standard libraries and thought it would be useful for you guys if I share the code.

The server runs on Linux and includes features like handling HTTP GET request, handling content types(txt, html, jpg, zip. rar, pdf, php etc.), sending proper HTTP error codes, serving the files from a web root, change in web root in a config file, zero copy optimization using sendfile method and php file handling. A port number should be provided as a command line argument.

After the server is up and running you can request for files using a web browser like Firefox.

For an example assume port number is "9000" and if you want to request a file called "test.php" which is in the webroot, use
http://localhost:9000/test.php 

WebServer.c
/*
 * WebServer.c
 *
 *  Created on: Nov 3, 2012
 *      Author: pavithra
 *
 * A web server in C language using only the standard libraries.
 * The port number is passed as an argument.
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>

#define EOL "\r\n"
#define EOL_SIZE 2

typedef struct {
 char *ext;
 char *mediatype;
} extn;

//Possible media types
extn extensions[] ={
 {"gif", "image/gif" },
 {"txt", "text/plain" },
 {"jpg", "image/jpg" },
 {"jpeg","image/jpeg"},
 {"png", "image/png" },
 {"ico", "image/ico" },
 {"zip", "image/zip" },
 {"gz",  "image/gz"  },
 {"tar", "image/tar" },
 {"htm", "text/html" },
 {"html","text/html" },
 {"php", "text/html" },
 {"pdf","application/pdf"},
 {"zip","application/octet-stream"},
 {"rar","application/octet-stream"},
 {0,0} };

/*
 A helper function
 */
void error(const char *msg) {
 perror(msg);
 exit(1);
}

/*
 A helper function
 */
int get_file_size(int fd) {
 struct stat stat_struct;
 if (fstat(fd, &stat_struct) == -1)
  return (1);
 return (int) stat_struct.st_size;
}

/*
 A helper function
 */
void send_new(int fd, char *msg) {
 int len = strlen(msg);
 if (send(fd, msg, len, 0) == -1) {
  printf("Error in send\n");
 }
}

/*
 This function recieves the buffer
 until an "End of line(EOL)" byte is recieved
 */
int recv_new(int fd, char *buffer) {
 char *p = buffer; // Use of a pointer to the buffer rather than dealing with the buffer directly
 int eol_matched = 0; // Use to check whether the recieved byte is matched with the buffer byte or not
 while (recv(fd, p, 1, 0) != 0) // Start receiving 1 byte at a time
 {
  if (*p == EOL[eol_matched]) // if the byte matches with the first eol byte that is '\r'
    {
   ++eol_matched;
   if (eol_matched == EOL_SIZE) // if both the bytes matches with the EOL
   {
    *(p + 1 - EOL_SIZE) = '\0'; // End the string
    return (strlen(buffer)); // Return the bytes recieved
   }
  } else {
   eol_matched = 0;
  }
  p++; // Increment the pointer to receive next byte
 }
 return (0);
}

/*
 A helper function: Returns the
 web root location.
 */
char* webroot() {
 // open the file "conf" for reading
 FILE *in = fopen("conf", "rt");
 // read the first line from the file
 char buff[1000];
 fgets(buff, 1000, in);
 // close the stream
 fclose(in);
 char* nl_ptr = strrchr(buff, '\n');
 if (nl_ptr != NULL)
  *nl_ptr = '\0';
 return strdup(buff);
}

/*
 Handles php requests
 */
void php_cgi(char* script_path, int fd) {
 send_new(fd, "HTTP/1.1 200 OK\n Server: Web Server in C\n Connection: close\n");
 dup2(fd, STDOUT_FILENO);
 char script[500];
 strcpy(script, "SCRIPT_FILENAME=");
 strcat(script, script_path);
 putenv("GATEWAY_INTERFACE=CGI/1.1");
 putenv(script);
 putenv("QUERY_STRING=");
 putenv("REQUEST_METHOD=GET");
 putenv("REDIRECT_STATUS=true");
 putenv("SERVER_PROTOCOL=HTTP/1.1");
 putenv("REMOTE_HOST=127.0.0.1");
 execl("/usr/bin/php-cgi", "php-cgi", NULL);
}

/*
 This function parses the HTTP requests,
 arrange resource locations,
 check for supported media types,
 serves files in a web root,
 sends the HTTP error codes.
 */
int connection(int fd) {
 char request[500], resource[500], *ptr;
 int fd1, length;
 if (recv_new(fd, request) == 0) {
  printf("Recieve Failed\n");
 }
 printf("%s\n", request);
 // Check for a valid browser request
 ptr = strstr(request, " HTTP/");
 if (ptr == NULL) {
  printf("NOT HTTP !\n");
 } else {
  *ptr = 0;
  ptr = NULL;

  if (strncmp(request, "GET ", 4) == 0) {
   ptr = request + 4;
  }
  if (ptr == NULL) {
   printf("Unknown Request ! \n");
  } else {
   if (ptr[strlen(ptr) - 1] == '/') {
    strcat(ptr, "index.html");
   }
   strcpy(resource, webroot());
   strcat(resource, ptr);
   char* s = strchr(ptr, '.');
   int i;
   for (i = 0; extensions[i].ext != NULL; i++) {
    if (strcmp(s + 1, extensions[i].ext) == 0) {
     fd1 = open(resource, O_RDONLY, 0);
     printf("Opening \"%s\"\n", resource);
     if (fd1 == -1) {
      printf("404 File not found Error\n");
      send_new(fd, "HTTP/1.1 404 Not Found\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      send_new(fd, "<html><head><title>404 Not Found</head></title>");
      send_new(fd, "<body><p>404 Not Found: The requested resource could not be found!</p></body></html>");
      //Handling php requests
     } else if (strcmp(extensions[i].ext, "php") == 0) {
      php_cgi(resource, fd);
      sleep(1);
      close(fd);
      exit(1);
     } else {
      printf("200 OK, Content-Type: %s\n\n",
        extensions[i].mediatype);
      send_new(fd, "HTTP/1.1 200 OK\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      if (ptr == request + 4) // if it is a GET request
        {
       if ((length = get_file_size(fd1)) == -1)
        printf("Error in getting size !\n");
       size_t total_bytes_sent = 0;
       ssize_t bytes_sent;
       while (total_bytes_sent < length) {
        //Zero copy optimization
        if ((bytes_sent = sendfile(fd, fd1, 0,
          length - total_bytes_sent)) <= 0) {
         if (errno == EINTR || errno == EAGAIN) {
          continue;
         }
         perror("sendfile");
         return -1;
        }
        total_bytes_sent += bytes_sent;
       }

      }
     }
     break;
    }
    int size = sizeof(extensions) / sizeof(extensions[0]);
    if (i == size - 2) {
     printf("415 Unsupported Media Type\n");
     send_new(fd, "HTTP/1.1 415 Unsupported Media Type\r\n");
     send_new(fd, "Server : Web Server in C\r\n\r\n");
     send_new(fd, "<html><head><title>415 Unsupported Media Type</head></title>");
     send_new(fd, "<body><p>415 Unsupported Media Type!</p></body></html>");
    }
   }

   close(fd);
  }
 }
 shutdown(fd, SHUT_RDWR);
}

int main(int argc, char *argv[]) {
 int sockfd, newsockfd, portno, pid;
 socklen_t clilen;
 struct sockaddr_in serv_addr, cli_addr;

 if (argc < 2) {
  fprintf(stderr, "ERROR, no port provided\n");
  exit(1);
 }
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 bzero((char *) &serv_addr, sizeof(serv_addr));
 portno = atoi(argv[1]);
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = INADDR_ANY;
 serv_addr.sin_port = htons(portno);
 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  error("ERROR on binding");
 listen(sockfd, 5);
 clilen = sizeof(cli_addr);
 /*
  Server runs forever, forking off a separate
  process for each connection.
  */
 while (1) {
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  if (newsockfd < 0)
   error("ERROR on accept");
  pid = fork();
  if (pid < 0)
   error("ERROR on fork");
  if (pid == 0) {
   close(sockfd);
   connection(newsockfd);
   exit(0);
  } else
   close(newsockfd);
 } /* end of while */
 close(sockfd);
 return 0; /* we never get here */
}
Make sure you have installed php and there's exist a "conf" file consisted of the webroot before running this.

Enjoy!

Monday, November 5, 2012

NS-2 Simulation

Installing NS2 on Ubuntu 11.04
sudo apt-get install ns2 nam
Here I have used NS-2 to simulate a network with the topology as shown below.


I have written tcl scripts to create the above topology and to show,
  1. When two TCP flows compete for the bandwidth they fairly share the bandwidth
  2. When a TCP flow and a UDP flow compete for the bandwidth there is no fair sharing
Also I have plotted the throughput at the receivers against time using xgraph.

Installing xgraph on Ubuntu 11.04
sudo apt-get install xgraph
To start the simulation
ns XXX.tcl
How two TCP flows compete for the bandwidth

tcp_tcp.tcl

This script simulates two TCP flows; one between n0 and n4 and one between n1 and n5. Also the bandwidth of L2 has selected in such a way that it becomes the bottleneck link and the "record" procedure has used to measure the throughput at the receivers. .
#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the trace files outX.tr for Xgraph and out.nam for nam
set f0 [open out_tcp0.tr w]
set f1 [open out_tcp1.tr w]

#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
 global ns nf f0 f1
 $ns flush-trace
#Close the NAM trace file
 close $nf
#Close the output files
 close $f0
 close $f1
#Execute xgraph to display the results
 exec xgraph out_tcp0.tr out_tcp1.tr -geometry 600x400 &
#Execute NAM on the trace file
 exec nam out.nam &
 exit 0
}

#Create five nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail

#Set Queue Size of link (n2-n3) to 20
$ns queue-limit $n2 $n3 20

#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#record procedure
proc record {} {
 global sink sink1 f0 f1
#Get an instance of the simulator
 set ns [Simulator instance]

#Set the time after which the procedure should be called again
 set time 0.5

#How many bytes have been received by the traffic sinks?
 set bw0 [$sink set bytes_]
 set bw1 [$sink1 set bytes_]

#Get the current time
 set now [$ns now]

#Calculate the bandwidth (in MBit/s) and write it to the files
 puts $f0 "$now [expr $bw0/$time*8/1000000]"
 puts $f1 "$now [expr $bw1/$time*8/1000000]"

#Reset the bytes_ values on the traffic sinks
 $sink set bytes_ 0
 $sink1 set bytes_ 0

#Re-schedule the procedure
 $ns at [expr $now+$time] "record"
}

#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#Setup a TCP connection
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 2

#Setup a FTP over TCP connection
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP

#Start logging the received bandwidth
$ns at 0.0 "record"

#Schedule events for the FTP agents
$ns at 0.1 "$ftp start"
$ns at 0.8 "$ftp1 start"
$ns at 4.0 "$ftp1 stop"
$ns at 4.8 "$ftp stop"

#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run

Graph


Above graph visualization clearly shows how two TCP flows fairly share the bandwidth.

How a TCP flow and a UDP flow compete for the bandwidth

tcp_udp.tcl

This script simulates one TCP flow (between n0 and n4) and one UDP flow (between n1 and n5). Again the bandwidth of L2 has selected in such a way that it becomes the bottleneck link and the "record" procedure has used to measure the throughput at the receivers.
#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the trace files outX.tr for Xgraph and out.nam for nam
set f0 [open out_tcp.tr w]
set f1 [open out_udp.tr w]

#Open the NAM trace file
set nf [open out_udptcp.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
 global ns nf f0 f1
 $ns flush-trace
#Close the NAM trace file
 close $nf
#Close the output files
 close $f0
 close $f1
#Execute xgraph to display the results
 exec xgraph out_tcp.tr out_udp.tr -geometry 600x400 &
#Execute NAM on the trace file
 exec nam out_udptcp.nam &
 exit 0
}

#Create five nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail

#Set Queue Size of link (n2-n3) to 20
$ns queue-limit $n2 $n3 20

#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#record procedure
proc record {} {
 global sink sink1 f0 f1
#Get an instance of the simulator
 set ns [Simulator instance]

#Set the time after which the procedure should be called again
 set time 0.5

#How many bytes have been received by the traffic sinks?
 set bw0 [$sink set bytes_]
 set bw1 [$sink1 set bytes_]

#Get the current time
 set now [$ns now]

#Calculate the bandwidth (in MBit/s) and write it to the files
 puts $f0 "$now [expr $bw0/$time*8/1000000]"
 puts $f1 "$now [expr $bw1/$time*8/1000000]"

#Reset the bytes_ values on the traffic sinks
 $sink set bytes_ 0
 $sink1 set bytes_ 0

#Re-schedule the procedure
 $ns at [expr $now+$time] "record"
}

#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set sink1 [new Agent/LossMonitor]
$ns attach-agent $n5 $sink1
$ns connect $udp $sink1
$udp set fid_ 2

#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 2mb
$cbr set random_ false

#Start logging the received bandwidth
$ns at 0.0 "record"

#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 0.8 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.8 "$cbr stop"

#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run
Graph


Above graph visualization clearly shows, when it comes to a TCP flow and a UDP flow there is no fair sharing the bandwidth, UDP gets the most of it.

Tuesday, August 21, 2012

How to deploy a war file using GlassFish

Before trying this out make sure you have installed GlassFish in your machine. Installation is just easy as downloading the zip archive from here and unzipping it to a desired location.

Creating a new domain

Open up a terminal, change directory to the GlassFish installation directory and run following.
bin/asadmin

This will enable you to use asadmin tool. Now execute the following command to create a new domain. after running this command you will probably have to give admin password and master password.
create-domain --adminport 5000 --profile developer --user admin domain2

Now open up another terminal and change directory to [glassfish-installation]/domains/ and you will see the newly created domain2 has appeared there.


Open up a browser and go to http://localhost:5000/. This will bring you the GlassFish GUI admin console. This is one place you can deploy your war file. But in this post I'm not much focusing on it, instead I will show you how to do this using the command line. But GUI lovers, for any consolation I have put some screenshots which you can follow if you prefer the GUI way.


In case you want to delete a domain use the following command.
delete-domain domain2

Starting the domain

To start domain2 run following command.
start-domain domain2

Deploying a war file

Use the following command to deploy your war file.
deploy --port 5000 --host localhost /home/pavithra/workspace/NewDemoService/WebServicesJaxWs/NewDemoService.war 

After deploying the war file I can access the WSDL file I want to access using the URL http://localhost:8080/NewDemoService/NewDemoService?WSDL


To change default 8080 (HTTP) port which specifies where the web application context roots are available for a Web browser to connect to, you can use the --instanceport parameter when creating the domain. See the following command.
create-domain --adminport 5000 --profile developer --user admin --instanceport 9000 domain2

Undeploying a war file

To undeploy NewDemoService.war file you need to use the following command. Note that here you don't have to use the full name but literal "NewDemoService".
undeploy --port 5000 --host localhost NewDemoService

Stopping a domain

To stop the domain "domain2" use the following command.
stop-domain domain2

After this if you try to deploy to this particular domain, it will complain.

Auto Deploy

To perform auto deploy, copy NewDemoService.war file in to [glassfish-installation-directory]/domains/domain2/autodeploy directory. If autodeploy succeeds, you will see NewDemoService.war_deployed has created. This will deploy your war file automatically.


Monday, August 20, 2012

Getting started with JAX-WS

JAX-WS stands for Java API for XML Web Services. It is a Java programming language API for creating web services and clients that communicate using XML. This post is a quick start for JAX-WS.

Prerequisites
GlassFish integrated with Eclipse.

Creating the JAX-WS Web Service
  1. In Eclipse create a Dynamic Web Project called "com.eviac.blog.jaxwsproj". Make GlassFish as the Target Runtime.
  2. Create a new class called "SampleWS" in the created project. This will be the implementation class of the web service.
    SampleWS.java
    package com.eviac.blog.jaxws.service;
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService
    public class SampleWS {
    
     @WebMethod
     public int sum(int a, int b) {
      return a + b;
     }
    
     @WebMethod
     public int multiply(int a, int b) {
      return a * b;
     }
    
    }
    
    
  3. Open a terminal and navigate to the root of the project directory. Create a directory called wsdl inside WebContent/WEB-INF/. Use the following command to create web service artifacts. Make sure your JAVA_ HOME is set properly or this command will not work. Also make sure to build the project before running this command or it will complain class not found.
    wsgen -classpath build/classes/ -wsdl -r WebContent/WEB-INF/wsdl -s src -d build/classes/ com.eviac.blog.jaxws.service.SampleWS
    
  4. Refresh the project to discover created artifacts. Open the created WSDL-file inside wsdl folder. Search for REPLACE_WITH_ACTUAL_URL and replace it with the web service URL: http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService, and save the file.
  5. Deploy the project in Glassfish by right-clicking the project, click Run As -> Run on Server and select the Glassfish server.
Creating the JAX-WS client
  1. Create a Java project in eclipse called "com.eviac.blog.jaxwsclientproj". Open up a new terminal and go to the project root. Use the following command to generate the classes you need to access the web service. Here you will need to use the URL of the WSDL file.
    wsimport -s src -d bin http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl
    
  2. Create a new class called "SampleWSClient" in the project.
    SampleWSClient.java
    package com.eviac.blog.jaxws.client;
    
    import javax.xml.ws.WebServiceRef;
    
    import com.eviac.blog.jaxws.service.SampleWS;
    import com.eviac.blog.jaxws.service.SampleWSService;
    
    public class SampleWSClient {
    
     @WebServiceRef(wsdlLocation = "http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl")
     private static SampleWSService Samplews;
    
     public static void main(String[] args) {
      SampleWSClient wsClient = new SampleWSClient();
      wsClient.run();
     }
    
     public void run() {
      Samplews = new SampleWSService();
      SampleWS port = Samplews.getSampleWSPort();
      System.out.println("multiplication Result= "+ port.multiply(10, 20));
      System.out.println("Addition Result= "+port.sum(10, 20));
     }
    
    }
    
  3. Right click on the project and click on Run As -> Java Application. This will result following.
    multiplication Result= 200
    Addition Result= 30
    

Sunday, August 19, 2012

Integrating GlassFish with Eclipse 3.7

This post will guide you to integrate GlassFish 3.0.1 with Eclipse 3.7.
  1. Download the zip archive for GlassFish from here.
  2. Unzip it using the following command.
    unzip glassfish-3.0.1.zip 
    
  3. Move GlassFish installation directory to a more suitable place.
    sudo mv glassfishv3 /opt
    
  4. Open Eclipse.
    Navigate to New -> Server and click on download additional server adapters link and install Oracle GlassFish server tools. Restart Eclipse, when the installation is finished.
    After restarting Eclipse, navigate to New -> server.
    Select GlassFish Server Open Source Edition 3 (Java EE 6) and click next.
    Select a working JRE environment, select the installation path of your Glassfish application server and click next.
    In this screen keep the default settings and click next.
    In this screen it lets you select existing projects to be added to the newly configured server adapter, just click Finish.
    After a successful configuration, server adapter will be appeared in the servers tab. Start the server by right-clicking on it.

Friday, August 17, 2012

Installing Tomcat 7 on Ubuntu 12.04

This post is about installing Apache Tomcat7 on Ubuntu 12.04. Before this installation make sure java is installed in your machine. You can refer this post if you haven't done that already.
  1. Download Tomcat 7 tar.gz binary distribution from here.
  2. Unpack it using following command.
    tar xvzf apache-tomcat-7.0.29.tar.gz 
    
  3. Move it to a more appropriate location using following command.
    sudo mv apache-tomcat-7.0.29/ /usr/share/tomcat7
    
  4. Open up /usr/share/tomcat7/bin/catalina.sh file using following command
    gedit /usr/share/tomcat7/bin/catalina.sh 
    
  5. Add following two lines in there after the first line.
    JAVA_HOME="/usr/lib/jvm/jdk-6u32"
    JRE_HOME="/usr/lib/jvm/jdk-6u32/jre"
    
  6. Open up /usr/share/tomcat7/conf/tomcat-users.xml and uncomment user and role entries there. Then add a manager-gui role and a user by adding following lines there.
    <role rolename="manager-gui"/>
    <user username="pavithra" password="tomcat" roles="manager-gui"/>
    
  7. Start the Tomcat server using following command.
    sudo /usr/share/tomcat7/bin/catalina.sh run
    
  8. Verify Tomcat installation using the following URL.
    http://127.0.0.1:8080/
    
  9. To login as the manager, use the following URL, provide the relevant username and password.
    http://127.0.0.1:8080/manager/html
    

Friday, August 10, 2012

Installing Maven 3.0.4 on Ubuntu 12.04

To install Apache Maven 3.0.4 on Ubuntu 12.04 use following steps
  1. Get Maven 3.0.4 binary distribution using following command
    wget http://www.gtlib.gatech.edu/pub/apache/maven/binaries/apache-maven-3.0.4-bin.tar.gz
    
  2. Unpack the binary distribution using following command
    tar -zxf apache-maven-3.0.4-bin.tar.gz
    
  3. Move uncompressed "apache-maven-3.0.4" directory to the /usr/local directory using this command
    sudo cp -R apache-maven-3.0.4 /usr/local
    
  4. Create a symbolic link to /usr/bin using following command
    sudo ln -s /usr/local/apache-maven-3.0.4/bin/mvn /usr/bin/mvn
    
  5. Verify for correct installation using following command
    mvn –version
    
    It should print following
    Apache Maven 3.0.4 (r1232337; 2012-01-17 14:14:56+0530)
    Maven home: /usr/local/apache-maven-3.0.4
    Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
    Java home: /usr/lib/jvm/jdk-6u32/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.2.0-27-generic", arch: "amd64", family: "unix"
    

Tuesday, August 7, 2012

Installing LaTeX on Ubuntu

LaTeX is a markup language for describing a document. It can also be defined as a document preparation system. LaTeX is mainly used to create technical or scientific articles, papers, reports, books or PhD thesis.

There are number of LaTeX distributions you can install on Ubuntu. One such distribution is TeX Live.
  1. To install Tex Live LaTeX distribution on ubuntu use the following command.
    sudo apt-get install texlive-full
    
  2. To edit LaTeX documents we need an editor. There are number of LaTeX editors you can find. I recommend Texmaker, a cross platform LaTeX editor. To install Texmaker on Ubuntu use following command.
    sudo apt-get install texmaker
    
  3. To open Texmaker on Ubuntu use following command.
    texmaker
    
  4. Now Let's create a simple document using Texmaker. Click on File -> New and insert following lines in the blank document.
    \documentclass{article}
    
    \begin{document}
    
      Hello world!
    
    \end{document}
    
  5. Now save the document as a 'tex' file by clicking File -> Save. Compile the document clicking the arrow Quick Build.
You can learn more about LaTeX by refferring this link.

Saturday, August 4, 2012

Installing Apache Thrift on Ubuntu

In a previous post I wrote how to install Apache thrift on Windows. In this post I will guide you how to install Apache Thrift on Ubuntu 12.04.
  1. Install dependencies using the following command
    sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
    
  2. Download tar.gz archive from the this link, extract it in your home directory using this command
    tar -xvzf thrift-0.8.0.tar.gz
    
  3. Change into the Thrift installation directory(the one extracted) and carry on the following command
    ./configure
    
  4. Run the following command within Thrift installation directory
    make
    
  5. After that carry on the following command
    sudo make install
    
  6. To verify Thrift has installed properly, use the following command
    thrift -version 
    

Friday, August 3, 2012

Installing Apache Cassandra on Ubuntu

Apache Cassandra is a distributed, extremely scalable, highly available and fault tolerant NoSQL database initiated by facebook, later open sourced as an apache project. Cassandra data model is inspired by Google Bigtable and it's distribution model is inspired by Amazon Dynamo. If you are interested to know more about Cassandra you can refer to the paper written by Facebook.

This post will guide you how to install Cassandra on Ubuntu 12.04.
  1. Install the new updates using following commands
    sudo apt-get update
    sudo apt-get upgrade
    
  2. open /etc/apt/sources.list using the following command
    sudo gedit /etc/apt/sources.list
    
    and add the following lines to it
    deb http://www.apache.org/dist/cassandra/debian 10x main
    deb-src http://www.apache.org/dist/cassandra/debian 10x main
    
  3. Run update again and you will get the following error. This means you need to add the PUBLIC_KEY. In next step you will understand how to add this PUBLIC_KEY.
    GPG error: http://www.apache.org unstable Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 4BD736A82B5C1B00
    
  4. Register and add a PUBLIC_KEY key and update again, note that you may need to change the key accordingly
    gpg --keyserver wwwkeys.pgp.net --recv-keys 4BD736A82B5C1B00
    sudo apt-key add ~/.gnupg/pubring.gpg
    sudo apt-get update
    
  5. Install Cassandra using the following command
    sudo apt-get install cassandra
    
  6. Start Cassandra server using the following command
    sudo cassandra -f
    
    After starting the Cassandra server you will see it has started listening for thrift clients.
    ....
    ....
    ....
    INFO 12:18:29,140 Listening for thrift clients...
    
  7. To stop Cassandra server process first find the Process ID for Cassandra and kill it.

    To find the process ID use following command
    ps auwx | grep cassandra
    
    Output will be something like this. According to that 3595 is the process ID for cassandra.
    root      3595  0.0  0.0  60048  1908 pts/0    S+   12:18   0:00 sudo cassandra -f
    
    To kill the process use the following command
    sudo kill <pid>
    
    After killing the process you will see Cassandra server has stopped listening to thrift clients.
    INFO 13:04:08,663 Stop listening to thrift clients
    INFO 13:04:08,666 Waiting for messaging service to quiesce
    INFO 13:04:08,667 MessagingService shutting down server thread.
    
  8. Use following command to start Cassandra as a service
    sudo /etc/init.d/cassandra start
    
  9. Use following command to stop Cassandra service
    sudo /etc/init.d/cassandra stop
    
Installation will create following directories. Uses of them are mentioned within the brackets.
  • /var/lib/cassandra (data directories)
  • /var/log/cassandra (log directory)
  • /var/run/cassandra (runtime files)
  • /usr/share/cassandra (environment settings)
  • /usr/share/cassandra/lib (JAR files)
  • /usr/bin (binary files)
  • /usr/sbin
  • /etc/cassandra (configuration files)
  • /etc/init.d (service startup script)
  • /etc/security/limits.d (cassandra user limits)
  • /etc/default
Installing JNA (Java Native Access) on Linux platforms can improve Cassandra memory usage. To install JNA, download jna.jar from here and add it to /usr/share/cassandra/lib directory.

If you get the following error while you try to start a Cassandra server, means that cassandra is already running in the background somewhere. You will need to kill the process that is running in the background first. You can probably use the above mentioned stop command to stop any Cassandra servers running background.
Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 7199; nested exception is: 
 java.net.BindException: Address already in use
Here I have done the packaged installation, alternatively you can install Cassandra binary tarball installation on Ubuntu. Use this link for that.

References

Official Package To Install On Debian(tm) (not a product of Debian(tm))

Installing Oracle java6 on Ubuntu

If you have already installed Ubuntu 12.04 you probably have realized that Sun java(oracle java) does not come prepacked with Ubuntu like it used to be , instead OpenJDK comes with it. Here is how you can install Oracle java on Ubuntu 12.04 manually.
  1. Download jdk-6u32-linux-x64.bin from this link. If you have used 32-bit Ubuntu installation, download jdk-6u32-linux-x32.bin instead.
  2. To make the downloaded bin file executable use the following command
    chmod +x jdk-6u32-linux-x64.bin
    
  3. To extract the bin file use the following command
    ./jdk-6u32-linux-x64.bin
    
  4. Using the following command create a folder called "jvm" inside /usr/lib if it is not already existing
    sudo mkdir /usr/lib/jvm
    
  5. Move the extracted folder into the newly created jvm folder
    sudo mv jdk1.6.0_32 /usr/lib/jvm/
    
  6. To install the Java source use following commands
    sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_32/bin/javac 1
    sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_32/bin/java 1
    sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_32/bin/javaws 1
    
  7. To make this default java
    sudo update-alternatives --config javac
    sudo update-alternatives --config java
    sudo update-alternatives --config javaws
    
  8. To make symlinks point to the new Java location use the following command
    ls -la /etc/alternatives/java*
    
  9. To verify Java has installed correctly use this command
    java -version
    
  10. To set JAVA_HOME variable and add to your PATH open up /etc/bash.bashrc file using following command
    sudo gedit /etc/bash.bashrc 
    
  11. Now add the following lines to it.
    JAVA_HOME=/usr/lib/jvm/jdk1.6.0_32
    export JAVA_HOME
    PATH=$PATH:$JAVA_HOME/bin
    export PATH 
    
  12. After doing that open up a new terminal and run following commands to verify JAVA_HOME has set correctly.
    $echo $JAVA_HOME
    Should output this ---> /usr/lib/jvm/jdk1.6.0_32
    $echo $PATH
    Should output this ---> [Other paths]:/usr/lib/jvm/jdk1.6.0_32/bin 
    

Thursday, July 12, 2012

JMS with ActiveMQ

JMS short for Java Message Service provides a mechanism for integrating applications in a loosely coupled, flexible manner. JMS delivers data asynchronously across applications on a store and forward basis. Applications communicate through MOM(Message Oriented Middleware) which acts as an intermediary without communicating directly.

JMS Architecture

Main components of JMS are:
  • JMS Provider: A messaging system that implements the JMS interfaces and provides administrative and control features
  • Clients: Java applications that send or receive JMS messages. A message sender is called the Producer, and the recipient is called a Consumer
  • Messages: Objects that communicate information between JMS clients
  • Administered objects: Preconfigured JMS objects created by an administrator for the use of clients.
There are several JMS providers available like Apache ActiveMQ and OpenMQ. Here I have used Apache ActiveMQ.

Installing and starting Apache ActiveMQ on windows
  1. Download ActiveMQ windows binary distribution
  2. Extract the it to a desired location
  3. Using the command prompt change the directory to the bin folder inside ActiveMQ installation folder and run the following command to start ActiveMQ
  4. activemq
After starting ActiveMQ you can visit the admin console using http://localhost:8161/admin/ and do the administrative tasks

JMS Messaging Models

JMS has two messaging models, point to point messaging model and publisher subscriber messaging model.

Point to point messaging model

Producer sends the message to a specified queue within JMS provider and the only one of the consumers who listening to that queue receives that message.

Image courtesy Oracle
Point to Point Model Example

Example 1 and example 2 are almost similar the only difference is example 1 creates queues within the program and the example 2 uses jndi.properties file for naming look ups and creating queues.

Example 1
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Producer {

 public Producer() throws JMSException, NamingException {

  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup("connectionFactory");
  Connection connection;

  // Getting JMS connection from the server and starting it
  connection = conFactory.createConnection();
  try {
   connection.start();

   // JMS messages are sent and received using a Session. We will
   // create here a non-transactional session object. If you want
   // to use transactions you should set the first parameter to 'true'
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   Destination destination = (Destination) jndi.lookup("MyQueue");

   // MessageProducer is used for sending messages (as opposed
   // to MessageConsumer which is used for receiving them)
   MessageProducer producer = session.createProducer(destination);

   // We will send a small text message saying 'Hello World!'
   TextMessage message = session.createTextMessage("Hello World!");

   // Here we are sending the message!
   producer.send(message);
   System.out.println("Sent message '" + message.getText() + "'");
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  try {
   BasicConfigurator.configure();
   new Producer();
  } catch (NamingException e) {
   e.printStackTrace();
  }

 }
}
package com.eviac.blog.jms;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.BasicConfigurator;

public class Consumer {
 // URL of the JMS server
 private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

 // Name of the queue we will receive messages from
 private static String subject = "MYQUEUE";

 public static void main(String[] args) throws JMSException {
  BasicConfigurator.configure();
  // Getting JMS connection from the server
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  // Creating session for seding messages
  Session session = connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);

  // Getting the queue
  Destination destination = session.createQueue(subject);

  // MessageConsumer is used for receiving (consuming) messages
  MessageConsumer consumer = session.createConsumer(destination);

  // Here we receive the message.
  // By default this call is blocking, which means it will wait
  // for a message to arrive on the queue.
  Message message = consumer.receive();

  // There are many types of Message and TextMessage
  // is just one of them. Producer sent us a TextMessage
  // so we must cast to it to get access to its .getText()
  // method.
  if (message instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) message;
   System.out.println("Received message '" + textMessage.getText()
     + "'");
  }
  connection.close();
 }
}
Example 2

jndi.properties
# START SNIPPET: jndi

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

# END SNIPPET: jndi
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Producer {

 public Producer() throws JMSException, NamingException {

  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup("connectionFactory");
  Connection connection;

  // Getting JMS connection from the server and starting it
  connection = conFactory.createConnection();
  try {
   connection.start();

   // JMS messages are sent and received using a Session. We will
   // create here a non-transactional session object. If you want
   // to use transactions you should set the first parameter to 'true'
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   Destination destination = (Destination) jndi.lookup("MyQueue");

   // MessageProducer is used for sending messages (as opposed
   // to MessageConsumer which is used for receiving them)
   MessageProducer producer = session.createProducer(destination);

   // We will send a small text message saying 'Hello World!'
   TextMessage message = session.createTextMessage("Hello World!");

   // Here we are sending the message!
   producer.send(message);
   System.out.println("Sent message '" + message.getText() + "'");
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  try {
   BasicConfigurator.configure();
   new Producer();
  } catch (NamingException e) {
   e.printStackTrace();
  }

 }
}
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Consumer {
 public Consumer() throws NamingException, JMSException {
  Connection connection;
  
  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup("connectionFactory");
  // Getting JMS connection from the server and starting it
  // ConnectionFactory connectionFactory = new
  // ActiveMQConnectionFactory(url);
  connection = conFactory.createConnection();

  // // Getting JMS connection from the server
  // ConnectionFactory connectionFactory = new
  // ActiveMQConnectionFactory(url);
  // Connection connection = connectionFactory.createConnection();
  try {
   connection.start();

   // Creating session for seding messages
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   // Getting the queue
   Destination destination = (Destination) jndi.lookup("MyQueue");

   // MessageConsumer is used for receiving (consuming) messages
   MessageConsumer consumer = session.createConsumer(destination);

   // Here we receive the message.
   // By default this call is blocking, which means it will wait
   // for a message to arrive on the queue.
   Message message = consumer.receive();

   // There are many types of Message and TextMessage
   // is just one of them. Producer sent us a TextMessage
   // so we must cast to it to get access to its .getText()
   // method.
   if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    System.out.println("Received message '" + textMessage.getText()
      + "'");
   }
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  BasicConfigurator.configure();
  try {
   new Consumer();
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}

Publisher Subscriber Model

Publisher publishes the message to a specified topic within JMS provider and all the subscribers who subscribed for that topic receive the message. Note that only the active subscribers receive the message.

Image courtesy Oracle
Point to Point Model Example
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.*;

import org.apache.log4j.BasicConfigurator;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DemoPublisherSubscriberModel implements javax.jms.MessageListener {
 private TopicSession pubSession;
 private TopicPublisher publisher;
 private TopicConnection connection;

 /* Establish JMS publisher and subscriber */
 public DemoPublisherSubscriberModel(String topicName, String username,
   String password) throws Exception {
  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
    .lookup("topicConnectionFactry");

  // Create a JMS connection
  connection = conFactory.createTopicConnection(username, password);

  // Create JMS session objects for publisher and subscriber
  pubSession = connection.createTopicSession(false,
    Session.AUTO_ACKNOWLEDGE);
  TopicSession subSession = connection.createTopicSession(false,
    Session.AUTO_ACKNOWLEDGE);

  // Look up a JMS topic
  Topic chatTopic = (Topic) jndi.lookup(topicName);

  // Create a JMS publisher and subscriber
  publisher = pubSession.createPublisher(chatTopic);
  TopicSubscriber subscriber = subSession.createSubscriber(chatTopic);

  // Set a JMS message listener
  subscriber.setMessageListener(this);

  // Start the JMS connection; allows messages to be delivered
  connection.start();

  // Create and send message using topic publisher
  TextMessage message = pubSession.createTextMessage();
  message.setText(username + ": Howdy Friends!");
  publisher.publish(message);

 }

 /*
  * A client can register a message listener with a consumer. A message
  * listener is similar to an event listener. Whenever a message arrives at
  * the destination, the JMS provider delivers the message by calling the
  * listener's onMessage method, which acts on the contents of the message.
  */
 public void onMessage(Message message) {
  try {
   TextMessage textMessage = (TextMessage) message;
   String text = textMessage.getText();
   System.out.println(text);
  } catch (JMSException jmse) {
   jmse.printStackTrace();
  }
 }

 public static void main(String[] args) {
  BasicConfigurator.configure();
  try {
   if (args.length != 3)
    System.out
      .println("Please Provide the topic name,username,password!");

   DemoPublisherSubscriberModel demo = new DemoPublisherSubscriberModel(
     args[0], args[1], args[2]);

   BufferedReader commandLine = new java.io.BufferedReader(
     new InputStreamReader(System.in));

   // closes the connection and exit the system when 'exit' enters in
   // the command line
   while (true) {
    String s = commandLine.readLine();
    if (s.equalsIgnoreCase("exit")) {
     demo.connection.close();
     System.exit(0);

    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
JMS programming model: Image Courtesy Oracle

Wednesday, July 4, 2012

Apache Thrift with Java quickstart

Apache Thrift is a RPC framework founded by facebook and now it is an Apache project. Thrift lets you define data types and service interfaces in a language neutral definition file. That definition file is used as the input for the compiler to generate code for building RPC clients and servers that communicate over different programming languages. You can refer Thrift white paper also.

According to the official web site Apache Thrift is a,
software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
Image courtesy wikipedia

Installing Apache Thrift in Windows

Installation Thrift can be a tiresome process. But for windows the compiler is available as a prebuilt exe. Download thrift.exe and add it into your environment variables.

Writing Thrift definition file (.thrift file)

Writing the Thrift definition file becomes really easy once you get used to it. I found this tutorial quite useful to begin with.

Example definition file (add.thrift)
namespace java com.eviac.blog.samples.thrift.server  // defines the namespace 

typedef i32 int  //typedefs to get convenient names for your types

service AdditionService {  // defines the service to add two numbers
        int add(1:int n1, 2:int n2), //defines a method
}

Compiling Thrift definition file

To compile the .thrift file use the following command.
 
thrift --gen <language> <Thrift filename>
For my example the command is,
 
thrift --gen java add.thrift
After performing the command, inside gen-java directory you'll find the source codes which is useful for building RPC clients and server. In my example it will create a java code called AdditionService.java

Writing a service handler

Service handler class is required to implement the AdditionService.Iface interface.

Example service handler (AdditionServiceHandler.java)
 
package com.eviac.blog.samples.thrift.server;

import org.apache.thrift.TException;

public class AdditionServiceHandler implements AdditionService.Iface {

 @Override
 public int add(int n1, int n2) throws TException {
  return n1 + n2;
 }

}
Writing a simple server

Following is an example code to initiate a simple thrift server. To enable the multithreaded server uncomment the commented parts of the example code.

Example server (MyServer.java)
package com.eviac.blog.samples.thrift.server;

import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;

public class MyServer {

 public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {
  try {
   TServerTransport serverTransport = new TServerSocket(9090);
   TServer server = new TSimpleServer(
     new Args(serverTransport).processor(processor));

   // Use this for a multithreaded server
   // TServer server = new TThreadPoolServer(new
   // TThreadPoolServer.Args(serverTransport).processor(processor));

   System.out.println("Starting the simple server...");
   server.serve();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));
 }

}

Writing the client

Following is an example java client code which consumes the service provided by AdditionService.

Example client code (AdditionClient.java)
package com.eviac.blog.samples.thrift.client;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class AdditionClient {

 public static void main(String[] args) {

  try {
   TTransport transport;

   transport = new TSocket("localhost", 9090);
   transport.open();

   TProtocol protocol = new TBinaryProtocol(transport);
   AdditionService.Client client = new AdditionService.Client(protocol);

   System.out.println(client.add(100, 200));

   transport.close();
  } catch (TTransportException e) {
   e.printStackTrace();
  } catch (TException x) {
   x.printStackTrace();
  }
 }

}


Run the server code(MyServer.java). It should output following and will listen to the requests.
Starting the simple server...
Then run the client code(AdditionClient.java). It should output following.
300