Thursday, 31 October 2013

Sending File from one computer to another using C#

     

In this article we are going to see how to send a file from one computer to another using TCP protocol.
now in this example i am taking my own laptop as Client and server, so IpAddress are same when you want to send to different mention the correct IpAddress and binding Port Number.

For Server Side Code 
C# :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace RServer
{
    public partial class Form1 : Form
    {

        public delegate void Add(String Message);

        Add mess;
        public void AddMessage(String Message)
        {
            listBox1.Items.Add(Message);
        }

        public Form1()
        {
            InitializeComponent();
            mess = new Add(AddMessage);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(StartTcpServer);
        }

        public  string GetIP()
        {
            string name = Dns.GetHostName();
            IPHostEntry entry= Dns.GetHostEntry(name);
            IPAddress[] addr = entry.AddressList;
            if (addr[1].ToString().Split('.').Length == 4)
            {
                return addr[1].ToString();
            }
            return addr[2].ToString();
        }

        public void Message(string data)
        {
            listBox1.BeginInvoke(mess, data);

        }

        public  void StartTcpServer(object state)
        {
            TcpListener filelistener = new TcpListener(IPAddress.Parse(GetIP()),8085);
            filelistener.Start();
            TcpClient client = filelistener.AcceptTcpClient();
            Message("Client connection accepted from :" + client.Client.RemoteEndPoint + ".");
            byte []buffer = new byte[1500];
            int bytesread = 1;

            StreamWriter writer = new StreamWriter("D:\\sample.rar");

            while (bytesread > 0)
            {
                bytesread= client.GetStream().Read(buffer, 0, buffer.Length);
                if (bytesread == 0)
                    break;
                writer.BaseStream.Write(buffer, 0, buffer.Length);
                Message(bytesread + " Received. ");
            }
            writer.Close();

        }
    }
}



For Client Side Code:
C# :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace RServer
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public string GetIP()
        {
            string name = Dns.GetHostName();
            IPHostEntry entry = Dns.GetHostEntry(name);
            IPAddress[] addr = entry.AddressList;
            if (addr[1].ToString().Split('.').Length == 4)
            {
                return addr[1].ToString();
            }
            return addr[2].ToString();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                StreamReader sr = new StreamReader(textBox1.Text);

                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(new IPEndPoint(IPAddress.Parse(GetIP()), 8085));

                byte[] buffer = new byte[1500];
                long bytesSent = 0;

                while (bytesSent < sr.BaseStream.Length)
                {
                    int bytesRead = sr.BaseStream.Read(buffer, 0, 1500);
                    tcpClient.GetStream().Write(buffer, 0, bytesRead);
                    Message(bytesRead + " bytes sent.");

                    bytesSent += bytesRead;
                }

                tcpClient.Close();

                Message("finished");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Message(string data)
        {
            listBox1.Items.Add(data);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.ShowDialog();
            textBox1.Text = op.FileName;
        }

    }
}




Output :
Server


Client



I Hope from this article you can learn how to send file from computer to another.

C# - Create a Chat Application Client - Server







In this article we are going to see how to create a chat application using C#, for this we have to create a two projects in Console Applications one for client and another for server.

please dis-connet the internet while checking with this code.

Server:

First Let we see about Server, Which will get the request from client .Get the IP address of the Server Machine and Get the username and Port at which server has to watch for request.Socket is the used to receive and accept the request in TCP as stream.

C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatServer
{
    class Program
    {
        static Socket soc;
        static Socket acc;
        static int port = 9000;
        static IPAddress ip;
        static Thread rec;
        static string name;

        static string GetIp()
        {
            string hostname = Dns.GetHostName();
            IPHostEntry ipentry = Dns.GetHostEntry(hostname);
            IPAddress[] addr = ipentry.AddressList;
            return addr[addr.Length - 1].ToString();
        }


        static void RecV()
        {
            while (true)
            {
                Thread.Sleep(500);
                byte[] buffer = new byte[300];
                int rece = acc.Receive(buffer, 0, buffer.Length, 0);
                Array.Resize(ref buffer, rece);
                Console.WriteLine(Encoding.Default.GetString(buffer));
            }
        }



        static void Main(string[] args)
        {
            rec = new Thread(RecV);
            Console.WriteLine("Your Local Ip is " + GetIp());
            Console.WriteLine("Please enter your name");
            name = Console.ReadLine();
            Console.WriteLine("Please enter HostPort");
            string prt = Console.ReadLine();

            try
            {
                port = Convert.ToInt32(prt);
            }
            catch
            {
                port = 9000;
            }

            ip = IPAddress.Parse(GetIp());
            soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Bind(new IPEndPoint(ip, port));
            soc.Listen(0);
            acc = soc.Accept();
            rec.Start();
            while (true)
            {
                byte[] sdata = Encoding.Default.GetBytes("<"+name+">"+Console.ReadLine());
                acc.Send(sdata, 0, sdata.Length, 0);
            }


        }
    }
}



Client :

Now we see for the client same steps , but difference is here is send is the request we are make a separate thread for see the message from server.

C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatClient
{
    class Program
    {
        static string name;
        static int port = 9000;
        static IPAddress ip;
        static Socket soc;
        static Thread rec;


        static string GetIp()
        {
            string hostname = Dns.GetHostName();
            IPHostEntry ipentry = Dns.GetHostEntry(hostname);
            IPAddress[] addr = ipentry.AddressList;
            return addr[addr.Length - 1].ToString();
        }

        static void RecV()
        {
            while (true)
            {
                Thread.Sleep(500);
                byte[] buffer = new byte[300];
                int rece = soc.Receive(buffer, 0, buffer.Length, 0);
                Array.Resize(ref buffer, rece);
                Console.WriteLine(Encoding.Default.GetString(buffer));
            }
        }

       

        static void Main(string[] args)
        {
            rec = new Thread(RecV);
            Console.WriteLine("Please enter your name");
            name = Console.ReadLine();
            ip = IPAddress.Parse(GetIp());
            Console.WriteLine("Please enter HostPort");
            string prt = Console.ReadLine();

            try
            {
                port = Convert.ToInt32(prt);
            }
            catch
            {
                port = 9000;
            }

            soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Connect(new IPEndPoint(ip, port));
            rec.Start();
            byte[] data = Encoding.Default.GetBytes("<" + name + "> Connected");
            soc.Send(data, 0, data.Length, 0);

            while (soc.Connected)
            {
                byte[] sdata = Encoding.Default.GetBytes("<" + name + ">" + Console.ReadLine());
                soc.Send(sdata, 0, sdata.Length, 0);

            }

        }
    }
}




Output:

Server ;




Client :




From this article , I hope you can learn how to create a sample chat application.

Tuesday, 29 October 2013

WPF Multi Touch - Rotate the World

In this article we are going to see how to create a 3D multi touch example, this example we are going to create a rotatable 3D world with touch as well as in Mouse.For this we have to use StoryBoard for animation, and ViewPort3D for 3D support of image.




<Window x:Class="CSWPF3DMultiTouch.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:shapes="clr-namespace:CSWPF3DMultiTouch"
        Title="Touch 3D World">
    <Window.Resources>
        <Storyboard x:Key="sb">
            <DoubleAnimation Storyboard.TargetName="InfoScale" Storyboard.TargetProperty="ScaleX" From="0" To="1">
                <DoubleAnimation.EasingFunction>
                    <BounceEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
            <DoubleAnimation Storyboard.TargetName="InfoScale" Storyboard.TargetProperty="ScaleY" From="0" To="1">
                <DoubleAnimation.EasingFunction>
                    <BounceEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </Window.Resources>

        <Grid Background="Black"  IsManipulationEnabled="True" ManipulationDelta="OnManipulationDelta" TouchUp="OnTouchUp" MouseLeftButtonDown="Grid_MouseLeftButtonDown" MouseMove="Grid_MouseMove" MouseLeftButtonUp="Grid_MouseLeftButtonUp" MouseWheel="Grid_MouseWheel">
              <Viewport3D x:Name="viewport">
                     <Viewport3D.Camera>
                           <PerspectiveCamera Position="0,0,5"/>
                     </Viewport3D.Camera>

                     <ModelVisual3D x:Name="Light">
                           <ModelVisual3D.Content>
                                  <Model3DGroup>
                                         <AmbientLight x:Name="light" Color="White"/>
                                  </Model3DGroup>
                           </ModelVisual3D.Content>
                     </ModelVisual3D>

                     <shapes:Sphere x:Name="earth">
                           <shapes:Sphere.Material>                              
                                  <DiffuseMaterial>
                                         <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Map.png"/>
                                         </DiffuseMaterial.Brush>
                                  </DiffuseMaterial>
                           </shapes:Sphere.Material>
                           <shapes:Sphere.Transform>
                                  <Transform3DGroup x:Name="transformGroup">
                                         <ScaleTransform3D x:Name="scaleTransform"/>
                                  </Transform3DGroup>
                           </shapes:Sphere.Transform>
                     </shapes:Sphere>                
              </Viewport3D>
       
              <Rectangle Fill="Transparent"/>
        <CheckBox x:Name="MouseSimulationCheckBox" Content="Allow mouse simulation" Foreground="RosyBrown" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10"/>
        <TextBlock x:Name="InfoTextBox" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="10" FontSize="24" Foreground="RosyBrown" FontWeight="Bold" RenderTransformOrigin="0.5,0.5">
            <TextBlock.RenderTransform>
                <ScaleTransform x:Name="InfoScale"/>
            </TextBlock.RenderTransform>
        </TextBlock>
        <Line x:Name="TouchLine" Stroke="Red" StrokeThickness="2"/>
    </Grid>
</Window>



using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace CSWPF3DMultiTouch
{
       public partial class MainWindow : Window
       {
              private double _angleBuffer = 0d;
              private static int _imageWidth = 1995;
              private static int _imageHeight = 2051;
              private List<DataCenter> _dataCenters;

              private bool _isMouseDown;
        private Point _startPoint;

              public MainWindow()
              {
                     InitializeComponent();
                     this._dataCenters = new List<DataCenter>(6);
            this._dataCenters.Add(new DataCenter() { Name = "Chicago", Bound = new Rect(328, 790, 20, 20) });
            this._dataCenters.Add(new DataCenter() { Name = "San Antonio", Bound = new Rect(285, 873, 20, 20) });
            this._dataCenters.Add(new DataCenter() { Name = "Amsterdam", Bound = new Rect(856, 711, 20, 20) });
            this._dataCenters.Add(new DataCenter() { Name = "Dublin", Bound = new Rect(796, 703, 20, 20) });
            this._dataCenters.Add(new DataCenter() { Name = "Hong Kong", Bound = new Rect(1454, 923, 20, 20) });
            this._dataCenters.Add(new DataCenter() { Name = "Singapore", Bound = new Rect(1406, 1040, 20, 20) });
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
              }
              private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
              {
                     this.scaleTransform.ScaleX *= e.DeltaManipulation.Scale.X;
                     this.scaleTransform.ScaleY *= e.DeltaManipulation.Scale.Y;
                     this.scaleTransform.ScaleZ *= e.DeltaManipulation.Scale.X;

                     this._angleBuffer++;
                     if (_angleBuffer >= 0)
                     {
                           Vector delta = e.DeltaManipulation.Translation;
                this.RotateEarth(delta);
            }
            e.Handled = true;
              }

        private void RotateEarth(Vector delta)
        {
            if (delta.X != 0 || delta.Y != 0)
            {
                Vector3D vOriginal = new Vector3D(-delta.X, delta.Y, 0d);
                Vector3D vZ = new Vector3D(0, 0, 1);
                Vector3D perpendicular = Vector3D.CrossProduct(vOriginal, vZ);
                RotateTransform3D rotate = new RotateTransform3D();
                QuaternionRotation3D quatenion = new QuaternionRotation3D();
                quatenion.Quaternion = new Quaternion(perpendicular, 3);
                rotate.Rotation = quatenion;
                this.transformGroup.Children.Add(rotate);
                this._angleBuffer = 0;
            }
        }




              private void OnTouchUp(object sender, TouchEventArgs e)
              {           
            DoHitTest(e.GetTouchPoint(this.viewport).Position);
              }



        private void DoHitTest(Point point)
        {
            VisualTreeHelper.HitTest(this.viewport, null, new HitTestResultCallback(target =>
            {
                RayMeshGeometry3DHitTestResult result = target as RayMeshGeometry3DHitTestResult;
                if (result != null)
                {
                    Point p1 = result.MeshHit.TextureCoordinates[result.VertexIndex1];
                    Point p2 = result.MeshHit.TextureCoordinates[result.VertexIndex2];
                    Point p3 = result.MeshHit.TextureCoordinates[result.VertexIndex3];
                    double hitX = p1.X * result.VertexWeight1 + p2.X * result.VertexWeight2 + p3.X * result.VertexWeight3;
                    double hitY = p1.Y * result.VertexWeight1 + p2.Y * result.VertexWeight2 + p3.Y * result.VertexWeight3;
                    Point pointHit = new Point(hitX * _imageWidth, hitY * _imageHeight);

                    foreach (DataCenter dc in this._dataCenters)
                    {
                        if (dc.Bound.Contains(pointHit))
                        {
                            this.InfoTextBox.Text = "You've just touched the " + dc.Name + " data center!";
                            Storyboard sb = this.Resources["sb"] as Storyboard;
                            if (sb != null)
                            {
                                sb.Begin();
                            }
                            return HitTestResultBehavior.Stop;
                        }
                    }
                }
                return HitTestResultBehavior.Continue;
            }), new PointHitTestParameters(point));
        }


        void Touch_FrameReported(object sender, TouchFrameEventArgs e)
        {
            var touchPoints = e.GetTouchPoints(this.viewport);
            if (touchPoints.Count >= 2 && touchPoints[0].Action == TouchAction.Up)
            {
                this.TouchLine.X1 = touchPoints[0].Position.X;
                this.TouchLine.X2 = touchPoints[1].Position.X;
                this.TouchLine.Y1 = touchPoints[0].Position.Y;
                this.TouchLine.Y2 = touchPoints[1].Position.Y;
            }
        }

              private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
              {
                     this._isMouseDown = true;
            this._startPoint = e.GetPosition(this.viewport);
              }

              private void Grid_MouseMove(object sender, MouseEventArgs e)
              {
                     if (this._isMouseDown && this.MouseSimulationCheckBox.IsChecked.Value)
                     {
                this._angleBuffer++;
                if (_angleBuffer >= 0)
                {
                    Point currentPoint = e.GetPosition(this.viewport);
                    Vector delta = new Vector(currentPoint.X - this._startPoint.X, currentPoint.Y - this._startPoint.Y);
                    RotateEarth(delta);
                }
                     }
              }

              private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
              {
                     this._isMouseDown = false;
            if (this.MouseSimulationCheckBox.IsChecked.Value)
            {
                this.DoHitTest(e.GetPosition(this.viewport));
            }
              }

        private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (this.MouseSimulationCheckBox.IsChecked.Value)
            {
                double delta = e.Delta > 0 ? 1.2 : 0.8;
                this.scaleTransform.ScaleX *= delta;
                this.scaleTransform.ScaleY *= delta;
                this.scaleTransform.ScaleZ *= delta;
            }
        }
       }
}




 From this article i hope you will learn how to create a 3D view.