2012-02-27 42 views
0

我在我的項目中有一個服務器和多個客戶端。服務器需要通過請求發送文件,並存儲來自客戶端的文件。我有編碼,與多個客戶端連接,但在一次接收一個文件。其他文件存儲請求將在前一個文件完全收到後繼續進行。如何通過使用C#的TCP連接一次從多個客戶端接收文件?

我的文件大小約爲200 MB。所以需要更多時間來回復所有客戶。我將如何解決這個問題。任何人都可以幫助我。提前致謝。

public partial class Form1 : Form 
{ 
    byte[] Echo; 
    byte[] a; 
    Thread t1; 
    int flag = 0; 
    string receivedPath = "yok"; 
    public delegate void MyDelegate(); 
    private string fileName; 
    public Form1() 
    { 
     t1 = new Thread(new ThreadStart(StartListening)); 
     t1.Start(); 
     InitializeComponent(); 
    } 

    delegate void SetTextCallback(string text); 

    public class StateObject 
    { 
     // Client socket. 
     public Socket workSocket = null; 

     public const int BufferSize = 8069; 
     // Receive buffer. 
     public byte[] buffer = new byte[BufferSize]; 
    } 

    public static ManualResetEvent allDone = new ManualResetEvent(true); 

    public void StartListening() 
    { 

     StateObject state = new StateObject(); 
     byte[] bytes = new Byte[8069]; 
     IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050); 
     Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     try 
     { 
      listener.Bind(ipEnd); 
      listener.Listen(100); 
      while (true) 
      { 
       allDone.Reset(); 
       listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); 
       allDone.WaitOne(); 

      } 
     } 
     catch (Exception ex) 
     { 
      SetText(ex.ToString()); 
     } 

    } 

    public void AcceptCallback(IAsyncResult ar) 
    { 

     Socket listener = (Socket)ar.AsyncState; 
     Socket handler = listener.EndAccept(ar); 

     StateObject state = new StateObject(); 
     state.workSocket = handler; 
     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReadCallback), state); 
     flag = 0; 


    } 

    public void ReadCallback(IAsyncResult ar) 
    { 
     int fileNameLen = 1; 
     String content = String.Empty; 
     StateObject state = (StateObject)ar.AsyncState; 
     Socket handler = state.workSocket; 
     try 
     { 
      int bytesRead = handler.EndReceive(ar); 
      if (bytesRead > 0) 
      { 

       if (flag == 0) 
       { 

        fileNameLen = BitConverter.ToInt32(state.buffer, 0); 
        fileName = Encoding.UTF8.GetString(state.buffer, 4, fileNameLen); 
        receivedPath = @"D:\" + fileName; 
        flag++; 

       } 

       if (flag >= 1) 
       { 
        BinaryWriter writer = new BinaryWriter(File.Open(receivedPath, FileMode.Append)); 
        if (flag == 1) 
        { 
         writer.Write(state.buffer, 4 + fileNameLen, bytesRead - (4 + fileNameLen)); 
         flag++; 
        } 
        else 
        writer.Write(state.buffer, 0, bytesRead); 
        writer.Close(); 
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReadCallback), state); 
       } 
      } 
      else 
      { 

       allDone.Set(); 
       Invoke(new MyDelegate(LabelWriter)); 
      } 
     } 
     catch 
     { 
     } 
    } 
    public void LabelWriter() 
    { 
     label1.Text = "Data has been received " + fileName; 
    } 

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     t1.Abort(); 
    } 


} 

回答

0

您可以在AcceptCallback方法中觸發您的allDone事件。在接受客戶端後,您可以旋轉與客戶端通信的進程,同時開始監聽下一個客戶端。

+0

如果我在同一時間得到兩個文件存儲請求意味着它如何同時存儲兩個文件? – Murugesan 2012-02-27 08:09:01