我怎样才能改进我的代码?

时间:2016-12-22 08:40:58

标签: c# networking

因为我没有使用套接字的经验,而且我不知道怎么制作套接字,所以我有这个代码:

public void getGameInfo()
{
     string content;
     do
     {
          WebClient client = new WebClient();
          client.DownloadFile(fileadress, filename);
          client.Dispose();
          StreamReader reader = new StreamReader(filename);
          content = reader.ReadToEnd();
          reader.Close();
     } while (content == "");
     File.Delete(filename);
     string[] lines = content.Split(separator, StringSplitOptions.RemoveEmptyEntries);
     mode = zeilen[0];
     gameInfo = new string[line.Length-1];
     Array.Copy(lines, 1, gameInfo, 0, lines.Length-1);
}

它使用.txt文件连接到Apache服务器并读取它。但是如果有太多Programms(三个)使用代码,它将抛出一个WebException。 那么有没有办法改进这个,或者为此制作套接字的指南?

编辑1: 如果我想像这个函数一样写入文件怎么办?

public void setSpielInfo(int line, string input)
{
    WebClient client = new WebClient();
    string content;
    do
    {
        client.DownloadFile(gameadress, filename);
        StreamReader reader = new StreamReader(filename);
        content = reader.ReadToEnd();
        reader.Close();
    } while (content == "");

    string[] lines = content.Split(separator, StringSplitOptions.RemoveEmptyEntries);
    lines[zeile+1] = input;
    byte[] bytearray = Encoding.ASCII.GetBytes(string.Join(Environment.NewLine, lines)); // I've read that byte arrays are faster than string arrays

    FileStream writer = new FileStream(filename, FileMode.Truncate);
    writer.Write(bytearray, 0, bytearray.Length);
    writer.Close();

    client.UploadFile(ftpAdress, filename);
    client.Dispose();
    File.Delete(filename);
}

1 个答案:

答案 0 :(得分:1)

您想阅读string,对吧?那你为什么要下载文件?

  string content;

  // Do not dispose explicitly, wrap into using instead  
  using (WebClient client = new WebClient()) {
    content = client.DownloadString(fileadress);
  }

  string[] lines = content.Split(separator, StringSplitOptions.RemoveEmptyEntries);

  mode = lines.FirstOrDefault();    // 1st line
  gameInfo = lines.Skip(1).ToArray();  // all the others

您可以进一步缩短代码

  using (WebClient client = new WebClient()) {
    var lines = client
      .DownloadString(fileadress)  
      .Split(separator, StringSplitOptions.RemoveEmptyEntries);

    mode = lines.FirstOrDefault();
    gameInfo = lines.Skip(1).ToArray();
  }
再次

编辑:您实际想要执行的操作:下载string,写入文件,上传文件:

  string content;

  // Do not dispose explicitly, wrap into using instead  
  using (WebClient client = new WebClient()) {
    // Download string (text) 
    content = client.DownloadString(fileadress);
    // Write the text to file (override existing if it is)
    File.WriteAllText(filename, content);
    // Upload file
    // think on uploading the string - client.UploadString(ftpAdress, content);
    client.UploadFile(ftpAdress, filename); 
  }

  string[] lines = content.Split(separator, StringSplitOptions.RemoveEmptyEntries);

  mode = lines.FirstOrDefault();    // 1st line
  gameInfo = lines.Skip(1).ToArray();  // all the others

随着进一步的改进考虑使用string而不是文件:

  using (WebClient client = new WebClient()) {
    // Download string (text) 
    content = client.DownloadString(fileadress);
    client.UploadString(ftpAdress, content);
  }