错误 CS1022:类型或命名空间定义,或预期的文件结尾
error CS1022: Type or namespace definition, or end-of-file expected
我正在为 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();
}
}
}
}
它应该说服务器已启动...”或抛出异常,但这是我每次都得到的:
[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
错误显示无法编译文件。所以,很可能是编译器错误。我猜下面如果不是拼写错误,ipaddress 的拼写缺少 d,因此 AddressList。
IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
两个答案都正确。
System.Net using 丢失。
地址列表中有错字。
另一个问题是 - 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();
}
}
}
}
我正在为 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();
}
}
}
}
它应该说服务器已启动...”或抛出异常,但这是我每次都得到的:
[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
错误显示无法编译文件。所以,很可能是编译器错误。我猜下面如果不是拼写错误,ipaddress 的拼写缺少 d,因此 AddressList。
IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
两个答案都正确。
System.Net using 丢失。 地址列表中有错字。
另一个问题是 - 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();
}
}
}
}