DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Avatar

Pavithra Gunasekara

Software Engineer at Codegen

San Jose, US

Joined Dec 2010

http://blog.eviac.org/

About

A Programmer, blogger and an enthusiast of Big Data and Data Science.

Stats

Reputation: 67
Pageviews: 1.4M
Articles: 9
Comments: 0
  • Articles

Articles

article thumbnail
A 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 #include #include #include #include #include #include #include #include #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, ""); send_new(fd, "404 Not Found: The requested resource could not be found!"); //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, ""); send_new(fd, "415 Unsupported Media Type!"); } } 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!
November 17, 2012
· 42,956 Views · 1 Like
article thumbnail
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.
September 10, 2012
· 61,381 Views
article thumbnail
Installing Maven 3.0.4 on Ubuntu 12.04
To install Apache Maven 3.0.4 on Ubuntu 12.04, take the following steps.
August 24, 2012
· 42,752 Views
article thumbnail
Installing Oracle Java 6 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. Download jdk-6u32-linux-x64.bin from this link. If you have used 32-bit Ubuntu installation, download jdk-6u32-linux-x32.bin instead. To make the downloaded bin file executable use the following command chmod +x jdk-6u32-linux-x64.bin To extract the bin file use the following command ./jdk-6u32-linux-x64.bin Using the following command create a folder called "jvm" inside /usr/lib if it is not already existing sudo mkdir /usr/lib/jvm Move the extracted folder into the newly created jvm folder sudo mv jdk1.6.0_32 /usr/lib/jvm/ 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 To make this default java sudo update-alternatives --config javac sudo update-alternatives --config java sudo update-alternatives --config javaws To make symlinks point to the new Java location use the following command ls -la /etc/alternatives/java* To verify Java has installed correctly use this command java -version
August 13, 2012
· 59,332 Views · 1 Like
article thumbnail
Installing LaTeX on Ubuntu
From theses to reports and books, LaTeX is a pretty useful document markup language. Learn how to install the Tex Live distribution here.
August 9, 2012
· 374,115 Views · 6 Likes
article thumbnail
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. 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 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 Change into the Thrift installation directory(the one extracted) and carry on the following command ./configure Run the following command within Thrift installation directory make After that carry on the following command sudo make install To verify Thrift has installed properly, use the following command thrift -version
August 8, 2012
· 23,184 Views
article thumbnail
Synchronous Client Server Application in C#
TCP Based Server Establish the local end point for the socket Open a socket using Socket() Name the socket using Bind() Listen for incoming connections using Listen(), parameter inside the method specifies the maximu number of pending connections Accept client connections using Accept(), a new socket is created here but the original socket keeps listen Send, Receive data using Send() and Receive() Close Socket using Close() Server.cs using System; using System.Net.Sockets; using System.Net; using System.Text; public class SocketServer { public static void Main(string [] args) { // establish the local end point for the socket IPHostEntry ipHost = Dns.Resolve("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); // create a Tcp/Ip Socket Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // bind the socket to the local endpoint and // listen to the incoming sockets try { sListener.Bind(ipEndPoint); sListener.Listen(10); // Start listening for connections while (true) { Console.WriteLine("Waiting for a connection on port {0}",ipEndPoint); // program is suspended while waiting for an incoming connection Socket handler = sListener.Accept(); string data = null; // we got the client attempting to connect while(true) { byte[] bytes = new byte[1024]; int bytesRec = handler.Receive(bytes); data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (data.IndexOf("") > -1) { break; } } // show the data on the console Console.WriteLine("Text Received: {0}",data); string theReply = "Thank you for those " + data.Length.ToString() + " characters..."; byte[] msg = Encoding.ASCII.GetBytes(theReply); handler.Send(msg); handler.Shutdown(SocketShutdown.Both); handler.Close(); } } catch(Exception e) { Console.WriteLine(e.ToString()); } } // end of Main } TCP Based Client Establish a remote endpoint Open a socket using Socket() Connect to the remote host using Connect Send, receive data using Send() and Receive() Close Socket using Close() Client.cs using System; using Systern.Net.Sockets; using Systern.Net; using Systern.Text; public class SocketClient { public static void Main(string [] args) { // data buffer for incoming data byte[] bytes = new byte[1024]; // connect to a Remote device try { // Establish the remote end point for the socket IPHostEntry ipHost = Dns.Resolve("127.0.0.1"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); Socket sender = new Socket(AddressFamily.Internetwork, SocketType.Stream, ProtocolType.Tcp); // Connect the socket to the remote endpoint sender.Connect(ipEndPoint); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); string theMessage = "This is a test"; byte[] msg = Encoding.ASCII.GetBytes(theMessage+""); // Send the data through the socket int bytesSent = sender.Send(msg); // Receive the response from the remote device int bytesRec = sender.Receive(bytes); Console.WriteLine("The Server says : {0}", Encoding.ASCII.GetString(bytes,0, bytesRec)); // Release the socket sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch(Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } } } Fllowing image shows how client and server send and receive data
August 5, 2012
· 23,057 Views
article thumbnail
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 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 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(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
July 16, 2012
· 43,026 Views · 2 Likes
article thumbnail
JMS With ActiveMQ
Java Message Service is a mechanism for integrating applications in a loosely coupled, flexible manner and delivers data asynchronously across applications.
July 14, 2012
· 158,482 Views · 13 Likes

User has been successfully modified

Failed to modify user

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: