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 :) .