我正在为 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;
并修正拼写错误
AdressList
至 AddressList
关于c# - 错误 CS1022 : Type or namespace definition, 或预期的文件结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989073/