c# - 错误 CS1022 : Type or namespace definition, 或预期的文件结尾

标签 c# sockets compiler-errors namespaces tcpserver

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

error "CS1022: Type or namespace definition, or end-of-file expected".



我假设它与命名空间有关,因为我是 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();
            }

        }
    }
}

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

[Running] mono "C:\Users\Aidan\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "d:!computer science!!NEA!\test stuff\networking\server_test\program.cs" Error: Specified file could not be compiled.

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

at csscript.CSExecutor.ProcessCompilingResult (System.CodeDom.Compiler.CompilerResults results, System.CodeDom.Compiler.CompilerParameters compilerParams, CSScriptLibrary.ScriptParser parser, System.String scriptFileName, System.String assemblyFileName, System.String[] additionalDependencies) [0x00102] in :0 at csscript.CSExecutor.Compile (System.String scriptFileName) [0x0080d] in :0 at csscript.CSExecutor.ExecuteImpl () [0x005a1] in :0

[Done] exited with code=1 in 1.795 seconds

最佳答案

您缺少命名空间导入。

添加

using System.Net;

并修正拼写错误 AdressListAddressList

关于c# - 错误 CS1022 : Type or namespace definition, 或预期的文件结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989073/

相关文章:

c# - C# 或其他语言中的 Scala 风格抽象模块?

.net - Windows 版本的 "Netem"(模拟慢速网络连接)?

c++ - UDP 接收不工作

c - main通常是一个函数?

c++ - 在macOS上编译gpsim会导致 “error: ' long long type-name'无效”

Java程序计算变量数组的平均值,从命令行输入,显示结果

c# - 如何优雅地检查 Gravatar 或第三方网站是否正常运行?

c# - DataContractSerializer 不序列化继承 ISerializable 的类的成员

c# - 使用反射在我的 ViewModel 中创建通用类型命令有什么缺点?

c - 获取Socket使用的本地端口