Saturday, 15 February 2014

C# TCP/IP sending image
















//client.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace tcpclient_async
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            pictureBox1.Image = Image.FromFile(@"C:\Users\Public\Pictures\demo photo\13.JPG");

            string strHostName = System.Net.Dns.GetHostName();
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress x in ipHostInfo.AddressList)
            {
                comboBox1.Items.Add (x.ToString());
            }
        }

        public delegate void callrichtext(String ss);
        public callrichtext myDelegate;

        public void richtextappend(String s)
        {
            richTextBox1.AppendText(s);
        }

        public delegate void callrichtext2(String ss);
        public callrichtext2 myDelegate2;

        public void richtextappend2(String s)
        {
            richTextBox3.AppendText(s);
        }

        public delegate void buttonenable();
        public buttonenable myDelegate3;

        public void button()
        {
            button2.Enabled=true;
        }

        private Socket client;
        private byte[] data = new byte[1024000];
        private int size = 1024000;
        private OpenFileDialog y;
        private bool initialpic = true;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.myDelegate = new callrichtext(richtextappend);
            this.myDelegate2 = new callrichtext2(richtextappend2);
            this.myDelegate3 = new buttonenable(button);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox3.AppendText( "Connecting... \n");
            Socket newsock = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(textBox1.Text), 9050);
            newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] message = ms.ToArray();*/

            FileInfo fileinfo;
            byte[] message;
            FileStream fs;

            if (initialpic == true)
            {
                fileinfo = new FileInfo(@"C:\Users\Public\Pictures\demo photo\13.JPG");
                message = new byte[fileinfo.Length];
                fs = new FileStream(@"C:\Users\Public\Pictures\demo photo\13.JPG", FileMode.Open, FileAccess.Read);
            }
            else
            {
                fileinfo = new FileInfo(y.FileName);
                message = new byte[fileinfo.Length];
                fs = new FileStream(y.FileName, FileMode.Open, FileAccess.Read);
            }

            fs.Read(message, 0, message.Length);
            fs.Close();
            GC.ReRegisterForFinalize(fileinfo);
            GC.ReRegisterForFinalize(fs);

            client.BeginSend(message, 0, message.Length, SocketFlags.None,
                         new AsyncCallback(SendData), client);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            client.Close();
            richTextBox3.AppendText ("Disconnected \n");
        }

        private void Connected(IAsyncResult iar)
        {
            client = (Socket)iar.AsyncState;
            try
            {
                client.EndConnect(iar);
                richTextBox3.Invoke(this.myDelegate2, new Object[] { "Connected to: " + client.RemoteEndPoint.ToString() +"\n"});
                client.BeginReceive(data, 0, size, SocketFlags.None,
                              new AsyncCallback(ReceiveData), client);
                button2.Invoke(this.myDelegate3, new Object[] { });
            }
            catch (SocketException)
            {
                richTextBox3.Invoke(this.myDelegate2, new Object[] { "Error connecting \n"});
            }
        }

        private void ReceiveData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            richTextBox1.Invoke(this.myDelegate, new Object[]{stringData+"\n"});
        }

        private void SendData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int sent = remote.EndSend(iar);
            remote.BeginReceive(data, 0, size, SocketFlags.None,
                          new AsyncCallback(ReceiveData), remote);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            y = new OpenFileDialog();
            y.Title = "pic selection";
            y.InitialDirectory = "c:\\";
            y.Filter = "all files (*.*)|*.*|image files(*.jpg,*.bmp,*.gif)|*.jpg;*.bmp;*.gif";
            y.FilterIndex = 2;
            if (y.ShowDialog() == DialogResult.OK)
            { pictureBox1.Image = Image.FromFile(y.FileName); }

            initialpic = false;
        }

    }
}
--------------------------------------------------------------------------------------
//server.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace server_async
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string strHostName = System.Net.Dns.GetHostName();
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            richTextBox1.AppendText("local server address\n");

            foreach (IPAddress x in ipHostInfo.AddressList)
            {
                richTextBox1.AppendText(x.ToString()+"\n");
            }

            server = new Socket(AddressFamily.InterNetwork,
                   SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            server.Bind(iep);
            server.Listen(5);
            server.BeginAccept(new AsyncCallback(AcceptConn), server);
        }

        public delegate void callrichtext(String ss);
        public callrichtext myDelegate;

        public void richtextappend(String s)
        {
            richTextBox1.AppendText(s);
        }

        private Socket server, oldclient;
        private byte[] data = new byte[10240000];
        private int size = 10240000;

        private void AcceptConn(IAsyncResult iar)
        {
            Socket oldserver = (Socket)iar.AsyncState;
            oldclient = oldserver.EndAccept(iar);
            richTextBox1.Invoke(this.myDelegate, new Object[] { "Connected to: " + oldclient.RemoteEndPoint.ToString() + "\n" });
            string stringData = "Welcome to my server";
            byte[] message1 = Encoding.ASCII.GetBytes(stringData);
            oldclient.BeginSend(message1, 0, message1.Length, SocketFlags.None,
                        new AsyncCallback(SendData), oldclient);
        }

        private void SendData(IAsyncResult iar)
        {
            Socket client = (Socket)iar.AsyncState;
            int sent = client.EndSend(iar);
            client.BeginReceive(data, 0, size, SocketFlags.None,
                        new AsyncCallback(ReceiveData), client);
        }

        private void ReceiveData(IAsyncResult iar)
        {
            Socket client = (Socket)iar.AsyncState;
            int recv = client.EndReceive(iar);
            //MessageBox.Show(recv.ToString());
           
            if (recv == 0)
            {
                client.Close();
                richTextBox1.Invoke(this.myDelegate, new Object[]{ "Waiting for client..."+"\n"});
                server.BeginAccept(new AsyncCallback(AcceptConn), server);
                return;
            }

            MemoryStream ms = new MemoryStream(data);
            Image returnImage = Image.FromStream(ms);
            pictureBox1.Image = returnImage;

            string receivedData = "server has received pic";
           
            byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
            client.BeginSend(message2, 0, message2.Length, SocketFlags.None,
                         new AsyncCallback(SendData), client);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.myDelegate = new callrichtext(richtextappend);
        }

    }
}


reference:
http://www.codeproject.com/Questions/333909/Sending-a-picturebox-image-from-server-to-client-i
http://w3mentor.com/learn/asp-dot-net-c-sharp/asp-dot-net-language-basics/use-sockets-to-send-and-receive-images-in-c-net/

No comments:

Post a Comment