【问题标题】:error CS1022: Type or namespace definition, or end-of-file expected错误 CS1022:类型或命名空间定义,或预期文件结尾
【发布时间】:2018-12-31 15:34:03
【问题描述】:

我正在为 andriod/ios 创建一个消息传递应用程序,但我对 c# 和网络完全陌生,我已经按照简单套接字教程的第一步开始网络 (https://www.youtube.com/watch?v=KxdOOk6d_I0) 但我得到错误:

错误“CS1022:类型或命名空间定义,或预期文件结尾”。

我假设它与命名空间有关,因为我是 c# 新手,实际上并不了解命名空间的作用,但我的编译器说没有错误(如果那样的话,我正在使用 Visual Studio 代码有所作为)但它可能是别的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;

namespace server_test
{
    class program
    {
        static void main(string[] args)
        {

            IPAdress ip = Dns.GetHostEntry("localhost").AdressList[0];
            TcpListener server = new TcpListener(ip, 8080);
            TcpClient client = default(TcpClient);

            try
            {
                server.Start();
                Console.WriteLine("server started...");
                Console.ReadLine();
            }catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }

        }
    }
}

它应该说服务器已启动...”或抛出异常,但这是我每次得到的结果:

[Running] mono "C:\Users\Aidan\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "d:!computer science!!NEA!\test stuff\networking\server_test \程序.cs" 错误:无法编译指定的文件。

csscript.CompilerException: d:!computer science!!NEA!\test stuff\networking\server_test\program.cs(7,127): error CS1513: } 预期 d:!computer science!!NEA!\test stuff\networking\server_test\program.cs(37,1): error CS1022: Type or namespace definition, or end-of-file expected

在 csscript.CSExecutor.ProcessCompilingResult(System.CodeDom.Compiler.CompilerResults 结果,System.CodeDom.Compiler.CompilerParameters compilerParams,CSScriptLibrary.ScriptParser 解析器,System.String scriptFileName,System.String assemblyFileName,System.String[] additionalDependencies) [0x00102] 在:0 在 csscript.CSExecutor.Compile (System.String scriptFileName) [0x0080d] in :0 在 csscript.CSExecutor.ExecuteImpl () [0x005a1] in :0

[Done] 在 1.795 秒内以 code=1 退出

【问题讨论】:

  • IMO,在进入高级主题(即网络)之前,您确实应该学习基础知识(即 namespace 是什么)

标签: c# sockets compiler-errors namespaces tcpserver


【解决方案1】:

您缺少命名空间导入。

添加

using System.Net;

并将拼写错误AdressList 改成AddressList

【讨论】:

    【解决方案2】:

    错误显示无法编译文件。所以,很可能是编译器错误。如果不是拼写错误,我猜下面是 ipaddress 的拼写缺少一个 d,因此是 AddressList。

     IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
    

    【讨论】:

      【解决方案3】:

      两个答案都是正确的。

      System.Net 使用丢失。 地址列表中有错字。

      另一个问题是 - main 函数应该拼写为“Main”,大写 M。

      完整程序:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Net.Sockets;
      using System.Net;
      
      namespace server_test
      {
          class program
          {
              static void Main(string[] args)
              {
      
                  IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
                  TcpListener server = new TcpListener(ip, 8080);
                  TcpClient client = default(TcpClient);
      
                  try
                  {
                      server.Start();
                      Console.WriteLine("server started...");
                      Console.ReadLine();
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.ToString());
                      Console.ReadLine();
                  }
      
              }
          }
      }
      

      【讨论】:

      • 谢谢,不敢相信我错过了,大写的“主要”为我解决了它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多