Wednesday, December 9, 2015

c# Simple TCP Client Server

This is a simple implementation of a TCP client server relationship. In this samle, the TCP server listens to a port and if any client is trying to connect to the server , the server accepts and reads the message from the socket.


TCP Server


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

/**
 * @Author : JITHINRAJ.P
 * 
 * 
 * */
namespace TCP_server
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");

                TcpListener myList = new TcpListener(ipAd, 5150);

                myList.Start();

                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is  :" +
                                  myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");

                while (true)
                {
                    Socket s = myList.AcceptSocket();

                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                    byte[] b = new byte[100];
                    while (true)
                    {
                    int k = s.Receive(b);
                    Console.WriteLine("Recieved...");
                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(b[i]));

                    ASCIIEncoding asen = new ASCIIEncoding();
                    s.Send(asen.GetBytes("The string was recieved by the server."));
                    Console.WriteLine("\nSent Acknowledgement");
                    //s.Close();

                    if (k == 0)
                    {
                        s.Close();
                        break;
                    }
                    }
                }
                myList.Stop();
                Console.ReadLine();
            }
            catch (Exception e)
            {

            }
        }
    }
} 


TCP Client 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;

/**
 * @Author : JITHINRAJ.P
 * 
 * 
 * */

namespace TCP_Client
{
    class Program
    {
        private static TcpClient client;
        static void Main(string[] args)
        {
            client = new TcpClient();

            Console.WriteLine("Connecting to server");


            client.Connect("127.0.0.1" , 5150);

            Console.WriteLine("Connected");

            Console.WriteLine("enter The string to be  transmitted");

            String textToTransmit = Console.ReadLine();

            Stream tcpStream = client.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(textToTransmit);
            Console.WriteLine("Transmitting.....");

            tcpStream.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = tcpStream.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            client.Close();

        }
    }
}

Wednesday, June 3, 2015

Restful API using Java/Jersey and MongoDB


About this post

In this example I have created a REST web-service which stores some data about the books in the MongoDB database and will retrieve those data in the JSON format. I have used Openshift cloud platform to host the web application. 


Project structure

Project structure






ServletContainer.class


java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.sun.jersey.spi.container.servlet.ServletContainer

It is a Servlet Filter for deploying root resource classes.

Tuesday, May 26, 2015

ANDROID : IntentService Example

IntentService is a base class for Service  that handle asynchronous requests on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.



Here I am implemeted a IntentService , wich download the html data of http://www.facebook.com and http://www.google.com

Monday, May 25, 2015

ANDROID : Toogle button with Customised OnCkeckedChangeListner

The CompoundButton.OnCheckedChangeListener is using for creating  callback to be invoked when the checked state of a compound button changed. It can be assign to any view class inheriting the CompoundButton class.

       But i found a problem when using this interface with  view Adapters. The problem is if we set a callback for getting the checked change state to a ToggleButton ,  we can't understand the callback is fired when the user touches the screen or by changing the checked state by code .

Consider the code Below:

ToggleButton toogle = (ToggleButton) findViewById(R.id.btnId);
toogle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    //
   }
  });


So when the user changed the state of toggleButton by taping in it , the onCheckedChanged will fired.

ToggleButton toogle = (ToggleButton) findViewById(R.id.btnId);
It is also possible to do the same by this


toogle.setChecked(true);

So we cant understand the callback is fired by the user or by the ToogleButton class.


I have solved this issue by creating a custom callBack option for The ToggleButton.  And i am blessed if anyone found this post is helpful :) .

Friday, April 11, 2014

SMART SMS : SMS COMPRESSION USING LZW COMPRESSION ALGORITHM IN ANDROID

        In this work we investigate the possibility of reliably sending a small file via Short Message Service (SMS) by using data compression for a more effective mobile data exchange in which basic GSM is the only available data communication option.
        The Short Message Service (SMS) is a fundamental facility in the GSM communication standard. It is also the simplest form of data communication and sometimes also the cheapest when a terminal has no CSD data channel. Every day, billions of SMSes are sent and received all over the world, bringing SMS to be one of the most used applications in the mobile communication arena. Usually SMS is used for private conversation, but many other applications use it, as for example online banking, mobile payment, etc.. A typical SMS is composed at most of 160 symbols (by using 7 bits per symbol in the GSM standard) or up to 140 bytes (for binary messages). This is a relevant limitation essentially due to the need to support the service also on devices with very limited hardware capabilities.
          So the SMS I am imported the LZW Algorithm in the SMS application. before sending the SMS, which is compressed by the LZW Based compression and Send to the recipient. The SMS is then decompressed  using the Same App.

Wednesday, March 26, 2014

Android SQLite

      This section,describes how to use the SQLite database in Android applications. SQLite is an Open Source database. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements. The database requires limited memory at runtime (approx. 250 KByte) which makes it a good candidate from being embedded into other runtimes.
       SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java). All other types must be converted into one of these fields before getting saved in the database. SQLite itself does not validate if the types written to the columns are actually of the defined type, e.g. you can write an integer into a string column and vice versa.

More information about SQLite can be found on the SQLite website: http://www.sqlite.org.

SQLite in Android

       SQLite is embedded into every Android device. Using an SQLite database in Android does not require a setup procedure or administration of the database.


You only have to define the SQL statements for creating and updating the database. Afterwards the database is automatically managed for you by the Android platform.

Access to an SQLite database involves accessing the file system. This can be slow. Therefore it is recommended to perform database operations asynchronously.

If your application creates a database, this database is by default saved in the directoryDATA/data/APP_NAME/databases/FILENAME.

Saturday, September 28, 2013

Connect Arduino With Bluetooth Module To Android Device


       I recently done a project with Arduino , on which i needed to communicate the Arduino with my Android phone via bluetooth interface, after referencing through the tutorials about the Bluetooth , i succeeded to implement the connectivity of  my phone and Arduino. So here am going to implement a simple example containing two buttons on the android to control the led connected in the led pin of Arduino.
   Here i used Arduino nano v3 micro-controller and HC-05 Bluetooth Module. The majority of the Arduino boards contain a LED connected to the pin 12.