title: C# tcp socket client downloads files from server
body: <p>Currently I have this problem. Client downloads from server successfully only the 1st time. The 2nd time it doesn't work(nothing happens, no crash). Here the code from both side:</p>
  
  <p>At client, in mainForm, If I click the download button, I will call a method sendComment(string request) from another class, loginForm. </p>
  
  <p>At server, after receiving string request from client, server will call sendComment(string listFiles). listFiles consists of all files' names and sizes that client need to download. </p>
  
  <p>The string listFiles format: "commitRequest mName usID fiName1 fiSize1 fiName2 fiSize2...".After receive this string, client will request each file in string.</p>
  
  <p>Client side:</p>
  
  <p>loginForm:</p>
  
  <pre><code>    private void Connect()
      {
          try
          {
              serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              serversocket.Blocking = true;
  
              IPHostEntry IPHost = Dns.Resolve(textBox1.Text);
              string[] aliases = IPHost.Aliases;
              IPAddress[] addr = IPHost.AddressList;
  
              IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
              serversocket.Connect(ipepServer);
              clientsock = serversocket;
  
              Thread MainThread = new Thread(new ThreadStart(listenclient));
              MainThread.Start();
              MessageBox.Show("Connected successfully", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
          }
          catch (SocketException se)
          {
              Console.WriteLine(se.Message);
          }
          catch (Exception eee)
          {
              MessageBox.Show("Socket Connect Error.\n\n" + eee.Message + "\nPossible Cause: Server Already running. Check the tasklist for running processes", "Startup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              Application.Exit();
          }
      }
      void listenclient()
      {
          Socket sock = clientsock;
          string cmd = server;
          byte[] sender = System.Text.Encoding.ASCII.GetBytes("CLIENT " + cmd);
          sock.Send(sender, sender.Length, 0);
  
              while (sock != null)
              {
                  cmd = "";
                  byte[] recs = new byte[32767];
                  int rcount = sock.Receive(recs, recs.Length, 0);
                  string clientmessage = System.Text.Encoding.ASCII.GetString(recs);
                  clientmessage = clientmessage.Substring(0, rcount);
  
                  string smk = clientmessage;
  
                  cmdList = null;
                  cmdList = clientmessage.Split(' ');
                  string execmd = cmdList[0];
  
                  sender = null;
                  sender = new Byte[32767];
  
                  string parm1 = "";
  
  
                  if (execmd == "CommitRequest") 
                  {
                      for (int i = 3; i &lt; cmdList.Length - 1; i++)
                      {
                          if (i % 2 == 1)
                          {
                              sendComment("downloadFile " + cmdList[i]); // after receiving this, server will upload the file requested
                              downloadMFromServer(sock, cmdList[2], cmdList[1], cmdList[i], cmdList[i + 1]);                                  
                          }
  
                      }
                      continue;
                  }
              }
  
  
  private void downloadMFromServer(Socket s, string userID, string mName, string fileN, string fileS)
      {                
                      Socket sock = s;
                      string rootDir;
                      rootDir = @"C:\Client Data" + "\\" + userID + "\\" + mName;
                      Directory.CreateDirectory(rootDir);
                      System.IO.FileStream fout = new System.IO.FileStream(rootDir + "\\" + fileN, FileMode.Create, FileAccess.Write);
                      NetworkStream nfs = new NetworkStream(sock);
                      long size = int.Parse(fileS);
                      long rby = 0;
                      try
                      {
                          while (rby &lt; size)
                          {
                              byte[] buffer = new byte[1024];
                              int i = nfs.Read(buffer, 0, buffer.Length);
                              fout.Write(buffer, 0, (int)i);
                              rby = rby + i;
                          }
                          fout.Close();
                      }
                      catch (Exception ed)
                      {
                          Console.WriteLine("A Exception occured in file transfer" + ed.ToString());
                          MessageBox.Show(ed.Message);
                      }
                  }
  </code></pre>
  
  <p>After the 1st download button click, files downloaded to client successfully, then I deleted all files downloaded, then I clicked the download button 2nd time, but this time it didn't work. </p>
  
  <p>Files weren't got downloaded. I tried debugging, it didn't show any error, but the client application stopped at the step receiving the string listFiles from server. I mean client sended string re ok. server received string re ok. server sended string listFiles ok. but client didn't get listFiles. Does anyone know why it doesn't work? thanks for your help in advance.</p>
  
  <p>Here is the code for the sendComment method, it's the same for both client and server app. </p>
  
  <pre><code>public void sendComment(string comment)
  {
      Socket serversock = serversocket;
      if (serversock == null)
      {
          MessageBox.Show("Client not connected", "Connect Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
      }
       byte[] b = System.Text.Encoding.ASCII.GetBytes(comment + " ");
      serversock.Send(b, b.Length, 0);
  }
  </code></pre>
  
  <p>I don't upload the server side code because I don't think there's any problem with it and this post would be a bit long, but if you need just say and I will post it.</p>