Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# รับไฟล์จาก FTP Server ยังไงครับ พอดีผมเขียนไฟล์ฝั่งเซิร์ฟเวอร์ได้เเล้ว เเต่กดดาวน์โหลดไฟล์เเล้วไม่ได้อะครับ



 

C# รับไฟล์จาก FTP Server ยังไงครับ พอดีผมเขียนไฟล์ฝั่งเซิร์ฟเวอร์ได้เเล้ว เเต่กดดาวน์โหลดไฟล์เเล้วไม่ได้อะครับ

 



Topic : 100833



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์




ผมเขียนไฟล์ Server ได้เเล้วครับเเต่ไม่สามารถโหลดไฟล์ได้
โดยผมกําหนดให้ Client โหลดไฟล์จาก Server จากพาท C:/ftpService/


//ไฟล์ฝั่ง Server
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;

class FtpServer
{
    public static void Main()
    {
        /* Creating Port no 3099 on server for listening the client request*/
        TcpListener listener = new TcpListener(3099);
        Console.WriteLine("        Welcome to FTP Server.");
        /*Show Computer Name and IP Address*/
        string hostName = Dns.GetHostName();
        Console.WriteLine("\n Local hostname: {0}",hostName);
        IPHostEntry myself = Dns.GetHostByName(hostName); 
        foreach (IPAddress address in myself.AddressList)
        Console.WriteLine("\n IP Address: {0}", address.ToString());
        Console.WriteLine("\n Server Starts and Listening on Port(3099)....");

        
        

        /* start listening for any request*/
        listener.Start();

        /* calling function Service*/
        Service(listener);

    }

    

    public static void Service(TcpListener server)
    {
        while (true)
        {
            //creating object of TcpClient to catch the stream of connected computer
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("\n\nConnected");

            //getting the networkclient stream object
            NetworkStream clientstream = client.GetStream();

            //creating streamreader object to read messages from client
            StreamReader reader = new StreamReader(clientstream);

            //creating streamwriter object to send messages to client
            StreamWriter writer = new StreamWriter(clientstream);

            writer.AutoFlush = true;
            // reading file name from client
            string sourcefile = reader.ReadLine();

            Stream inputstream;
            try
            {
                //opening file in read mode
                inputstream = File.OpenRead(sourcefile);

                //sending file name to the client
                writer.WriteLine(sourcefile);

            }
            catch
            {
                Console.WriteLine("\n\n  File not found named: {0}", sourcefile);

                //sending message to client if file not found
                writer.WriteLine("\n\nFile not found");
                clientstream.Close();
                continue;
            }
            const int sizebuff = 1024;
            try
            {
                /*creating the bufferedstrem object for reading 1024 size of bytes from the 
                    file */
                BufferedStream bufferedinput = new BufferedStream(inputstream);

                /*creating the bufferedstrem object for sending bytes which are read 
                    from file   */
                BufferedStream bufferedoutput = new BufferedStream(clientstream);

                /* creating array of bytes size is 1024 */
                byte[] buffer = new Byte[sizebuff];
                int bytesread;

                /* Reading bytes from the file until the end */
                while ((bytesread = bufferedinput.Read(buffer, 0, sizebuff)) > 0)
                {
                    /* sending the bytes to the client */
                    bufferedoutput.Write(buffer, 0, bytesread);
                }
                Console.WriteLine("\n\n    file copied name:{0}", sourcefile);

                /* Closing connections*/
                bufferedoutput.Flush();
                bufferedinput.Close();
                bufferedoutput.Close();
            }
            catch (Exception)
            {
                Console.WriteLine("\n\n Connection Couldnot Esablished  because Client forget to create-\n \"ftpService\" folder in his (C) Drive or his/her harddisk is full or Client close its Connection in between process");
                writer.Close();
                reader.Close();
                continue;
            }
            /* Closing connections*/
            writer.Close();
            reader.Close();

        }
    }
}


เมื่อรันโปรเเกรมจะได้รูปตามนี้ครับ

server


มาถึงฝั่ง Client นะครับ

Code (C#)
//code Client
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.IO;
using System.Net.Sockets;
using System.Net;
using System.Reflection;

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

        //botton disconnect
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //botton connect
        private void button1_Click(object sender, EventArgs e)
        {

            //creating object of TcpClient to catch the stream of connected computer
            TcpClient Server = null;
            try
            {
                IPAddress myIP = IPAddress.Parse(textBox1.Text);
                //sending the computer IPAddress and port number on the network			

                IPHostEntry entry = Dns.GetHostByAddress(myIP);

                Server = new TcpClient(entry.HostName, 3099);
                MessageBox.Show("Connected.");

            }
            catch (Exception)
            {
                MessageBox.Show("Server Not found Enter Correct Machine Name.");
                return;
            }

        }

        

        private void button4_Click(object sender, EventArgs e)
        {
            

        }
        OpenFileDialog ofd = new OpenFileDialog();
        private void button3_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = ofd.FileName;

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

    }
}


รูป

client

คืนตอนนี้ผมสามารถต่อกับเซิร์ฟเวอร์ได้เเล้วครับ เเต่ไม่สามารถโหลดไฟล์ได้ ผมอยากให้กดปุ่ม Download เเล้วก็โหลดไปเก็บในโฟลเดอร์ C:/ftp/ เลยอ่ะครับ

ช่วยผมหน่อยนะครับ ขอบคุณมากครับ



Tag : .NET, C#, VS 2012 (.NET 4.x), Windows







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-22 19:53:30 By : aristotle View : 1929 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เขียนมาได้ขนาดนี้น่าจะต่อได้ไม่ยากครับ ไม่ลอง Debug ดูล่ะครับ ว่าติดตรงไหน






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-23 09:33:53 By : mr.win
 


 

No. 2



โพสกระทู้ ( 174 )
บทความ ( 0 )



สถานะออฟไลน์


ทำดัก error ไว้ รึเปล่าครับ จะได้รุ้ ติดตรงไหน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-23 11:29:45 By : nongpaoza
 

 

No. 3



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์


ผมใส่เเอคชั่นในปุ่มเเล้วมันรันเเล้วค้างอ่าครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-23 14:05:21 By : aristotle
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# รับไฟล์จาก FTP Server ยังไงครับ พอดีผมเขียนไฟล์ฝั่งเซิร์ฟเวอร์ได้เเล้ว เเต่กดดาวน์โหลดไฟล์เเล้วไม่ได้อะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่