none
Download+Socket+C# RRS feed

  • Pergunta

  • Hi... I'm developing a kind of "Stuff Downloader" and I need to create the code for downloads in C#. I have done the upload code, but I had never thought in how to create this code... I think it's easy for you all, but not for me^^

    I hope the answer come as fast as you can

    see you all

    :: Felipe ::
    segunda-feira, 26 de outubro de 2009 02:17

Respostas

  • I have an example for image files..

    //
    //                      (C) Fadi Abdelqader - SocketCoder.Com 2009
    //                   - This Project Is Created to Work Behind The NAT - 
    //                - Just The Server Should be on a PC that has a public IP - 
    //
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Drawing.Imaging;
    
    namespace SocketCoder
    {
        public partial class Form1 : Form
        {
            private Socket ClientSocket;
            bool Connected = false;
            private byte[] buffer = new byte[1280000];
            FullScreen full = new FullScreen();
            public Form1()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false;
            }
    
            void SendImage()
            {
                MemoryStream ms = new MemoryStream();
                pictureBox_Sender.Image.Save(ms, ImageFormat.Jpeg);
                byte[] TempBuffer = ms.GetBuffer();
                ClientSocket.Send(TempBuffer,SocketFlags.None);
                ms.Flush();
            }
    
            void StartSending()
            {
                try
                {
                    // Option: To Send Continuously for Example: WebCam
                    // You have to make a new thread to call this method
                    while (Connected)
                    {
                        SendImage();
                        Thread.Sleep(100);
                    }
                }
                catch (Exception ex)
                {
                    Disconncet(); 
                    MessageBox.Show(ex.Message);
                }
            }
            void Establishing(string ServerIP,int Port)
            {
                try
                {
                    if (ClientSocket != null && ClientSocket.Connected)
                    {
                        ClientSocket.Shutdown(SocketShutdown.Both);
                        System.Threading.Thread.Sleep(10);
                        ClientSocket.Close();
                    }
    
                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint Server_EndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), Port);
                    ClientSocket.Blocking = false;
                 
                    ClientSocket.BeginConnect(Server_EndPoint,new AsyncCallback(OnConnect), ClientSocket);
    
                }
                catch (Exception) {}
            }
    
            public void OnConnect(IAsyncResult ar)
            {
                Socket sock = (Socket)ar.AsyncState;
    
                try
                {
                    if (sock.Connected)
                    {
                        SetupRecieveCallback(sock);
                        Conncet.Enabled = false;
                        DisConncet.Enabled = true;
                        textIP.Enabled = false;
                        text_Port.Enabled = false;
                        Connected = true;
                    }
                    else
                    {
                        Disconncet();
                        MessageBox.Show("Connection Failed");
                    }
                }
                catch (Exception){}
            }
    
            public void OnRecievedData(IAsyncResult ar)
            {
                Socket sock = (Socket)ar.AsyncState;
    
                try
                {
                    int nBytesRec = sock.EndReceive(ar);
                    if (nBytesRec > 0)
                    {
                        MemoryStream mss = new MemoryStream(buffer);
                        pictureBox_View.Image = Image.FromStream(mss);
                        full.pictureBox1.Image = pictureBox_View.Image;
                        SetupRecieveCallback(sock);
                    }
                    else
                    {
                        sock.Shutdown(SocketShutdown.Both);
                        sock.Close();
                    }
                }
                catch (Exception){}
            }
    
            public void SetupRecieveCallback(Socket sock)
            {
                try
                {
                    AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
                    sock.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, recieveData, sock);
                }
                catch (Exception){}
            }
    
            void Disconncet()
            {
                try
                {
                    if (ClientSocket != null & ClientSocket.Connected)
                    {
                        ClientSocket.Close();
                    }
                }
                catch (Exception) { }
                Conncet.Enabled = true;
                textIP.Enabled = true;
                text_Port.Enabled = true;
                DisConncet.Enabled = false;
                Connected = false;
            }
    
            private void Conncet_Click(object sender, EventArgs e)
            {
                Establishing(textIP.Text,int.Parse(text_Port.Text));
            }
            private void DisConncet_Click(object sender, EventArgs e)
                        }
                        catch (Exception ex)
                        {
                            Disconncet();
            {
                Disconncet();
            }
    
            private void button_ImportAndSend_Click(object sender, EventArgs e)
            {
                ImportFileDialog.Filter = "Image  JPEG|*.JPG";
                DialogResult = ImportFileDialog.ShowDialog();
                string FileName = ImportFileDialog.FileName;
                if (FileName != "" & DialogResult == DialogResult.OK)
                {
                    pictureBox_Sender.Image = Image.FromFile(FileName);
    
                    if (Connected)
                        try
                        {
                            SendImage();
                            MessageBox.Show(ex.Message);
                        }
                }
            }
    
            private void pictureBox_View_DoubleClick(object sender, EventArgs e)
            {
                full.Show();
            }
        }
    }
    quinta-feira, 5 de novembro de 2009 19:22