Communicate over TCP Socket and Serialize data with json format in C#

Client :

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            socket.Connect("127.0.0.1",9011);

            Console.WriteLine("connected...");

            Person person=new Person(){Name = "Mahmood"};

            string jsonData = JsonConvert.SerializeObject(person);
            byte[] dataBytes = Encoding.Default.GetBytes(jsonData);

            socket.Send(dataBytes);

            Console.WriteLine("sent...");

            byte[] buffer=new byte[1024*4];
            int readBytes = socket.Receive(buffer);
            MemoryStream memoryStream = new MemoryStream();

            while (readBytes>0)
            {
                memoryStream.Write(buffer, 0, readBytes);

                if (socket.Available > 0)
                {
                    readBytes = socket.Receive(buffer);   
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("read...");

            byte[] totalBytes = memoryStream.ToArray();

            memoryStream.Close();

            string readData = Encoding.Default.GetString(totalBytes);

            Greeting response = JsonConvert.DeserializeObject<Greeting>(readData);

            Console.WriteLine(response.Msg);

            Console.ReadKey();

Server :

            Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint=new IPEndPoint(IPAddress.Any, 9011);

            socket.Bind(endPoint);
            socket.Listen(5);

            while (true)
            {
                Console.WriteLine("waiting for new connection...");

                Socket newSocket = socket.Accept();

                MemoryStream memoryStream = new MemoryStream();

                Console.WriteLine("new connection...");

                byte[] buffer=new byte[1024];

                int readBytes = newSocket.Receive(buffer);

                while (readBytes>0)
                {
                    memoryStream.Write(buffer,0,readBytes);

                    if (socket.Available > 0)
                    {
                        readBytes = newSocket.Receive(buffer);    
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine("data received...");

                byte[] totalBytes = memoryStream.ToArray();

                memoryStream.Close();

                string readData = Encoding.Default.GetString(totalBytes);

                Person p= JsonConvert.DeserializeObject<Person>(readData);

                Greeting g = SayHello(p);

                string dataToSend = JsonConvert.SerializeObject(g);

                byte[] dataToSendBytes = Encoding.Default.GetBytes(dataToSend);

                newSocket.Send(dataToSendBytes);

                newSocket.Close();

                Console.WriteLine("data sent...");
            }

Communicate over TCP Socket and Serialize data with binary format in C#

Client :

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect("127.0.0.1", 9011);

            Console.WriteLine("connected...");

            BinaryFormatter formatter=new BinaryFormatter();
            MemoryStream memoryStream = new MemoryStream();
            Person person = new Person() { Name = "Mahmood" };

            formatter.Serialize(memoryStream,person);

            byte[] dataBytes = memoryStream.ToArray();
            socket.Send(dataBytes);

            Console.WriteLine("sent...");

            memoryStream = new MemoryStream();
            byte[] buffer = new byte[1024 * 4];
            int readBytes = socket.Receive(buffer);
            
            while (readBytes > 0)
            {
                memoryStream.Write(buffer, 0, readBytes);

                if (socket.Available>0)
                {
                    readBytes = socket.Receive(buffer);
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("read...");
            formatter=new BinaryFormatter();

            // set position to 0 or create a new stream
            memoryStream.Position = 0;
            Greeting response = (Greeting) formatter.Deserialize(memoryStream);

            Console.WriteLine(response.Msg);

            memoryStream.Close();
            socket.Close();

            Console.ReadKey();

Server :

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 9011);

            socket.Bind(endPoint);
            socket.Listen(5);

            while (true)
            {
                Console.WriteLine("waiting for new connection...");

                Socket newSocket = socket.Accept();

                Console.WriteLine("new connection...");

                byte[] buffer = new byte[1024];

                int readBytes = newSocket.Receive(buffer);
                MemoryStream memoryStream = new MemoryStream();

                while (readBytes > 0)
                {
                    memoryStream.Write(buffer, 0, readBytes);

                    if (socket.Available>0)
                    {
                        readBytes = newSocket.Receive(buffer);
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine("data received...");

                BinaryFormatter formatter=new BinaryFormatter();
                memoryStream.Position = 0;
                Person p = (Person) formatter.Deserialize(memoryStream);

                memoryStream.Close();

                Greeting g = SayHello(p);

                formatter=new BinaryFormatter();
                memoryStream=new MemoryStream();

                formatter.Serialize(memoryStream,g);

                newSocket.Send(memoryStream.ToArray());

                memoryStream.Close();
                newSocket.Close();

                Console.WriteLine("data sent...");

Android Exception : Only the original thread that created a view hierarchy can touch its views

 private void startingUp() {
    Thread timer = new Thread() { //new thread         
        public void run() {
            Boolean b = true;
            try {
                do {
                    counter++;
                    title();
                    sleep(1000);

                    runOnUiThread(new Runnable() {  
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        title.clearComposingText();
                    }
                });


                }
                while (b == true);
            } catch (IntruptedException e) {
                e.printStackTrace();
            }
            finally {
            }
        };
    };
    timer.start();
}

References :
http://stackoverflow.com/questions/14978052/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views-on-a

Serialize Object to JSON in C#

 public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new()
{
        var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite, new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
        });
        using (var writer = new StreamWriter(filePath, append))
        {
            writer.Write(contentsToWriteToFile);
        }
}

References :
http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4

Python UUID

>>> import uuid

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

References :
https://docs.python.org/3.5/library/uuid.html

Build libcurl static library on Windows

Download the latest curl generic source from: http://curl.haxx.se/latest.cgi?curl=tar.gz
Extract the source to a local directory (we’ll be using C:\libcurl)
Open a command prompt
“C:\Program Files (x86)\Microsoft Visual Studio 15.0\VC\bin\vcvars32.bat” To initialize your VC environment variables (adjust your VS 2015 installation directory as needed)
cd C:\libcurl\winbuild

nmake /f Makefile.vc mode=static VC=14

The build should appear in C:\libcurl\builds\libcurl-vc14-x86-release-static-ipv6-sspi-winssl

References :
http://stackoverflow.com/questions/20171165/getting-libcurl-to-work-with-visual-studio-2013