错误CS1022:类型或名称空间定义,或预期的文件结尾

时间:2018-12-31 15:34:03

标签: c# sockets compiler-errors namespaces tcpserver

我正在为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();
            }

        }
    }
}

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

  

[运行]单声道“ C:\ Users \ Aidan \ AppData \ Roaming \ Code \ User \ cs-script.user \ cscs.exe”“ d :!计算机科学!NEA!\测试内容\网络\ server_test \ program.cs”   错误:无法编译指定的文件。

     

csscript.CompilerException:d :!计算机科学!NEA!\测试内容\网络\ server_test \ program.cs(7,127):错误CS1513:}   d :!计算机科学!NEA!\测试资料\网络\ server_test \ program.cs(37,1):错误CS1022:类型或名称空间定义,或预期的文件结尾

     在csscript.CSExecutor.ProcessCompilingResult中的

(System.CodeDom.Compiler.CompilerResults结果,System.CodeDom.Compiler.CompilerParameters编译器参数,CSScriptLibrary.ScriptParser解析器,System.String scriptFileName,System.String assemblyFileName,System.String []其他依赖项) [0x00102]在:0中     在csscript.CSExecutor.Compile(System.String scriptFileName)[0x0080d]在:0中     在csscript.CSExecutor.ExecuteImpl()[0x005a1] in:0

     

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

3 个答案:

答案 0 :(得分:2)

您缺少名称空间导入。

添加

using System.Net;

并将拼写错误AdressList修改为AddressList

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

两个答案都是正确的。

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

另一个问题是-主功能应使用大写M拼写为“ Main”。

完整程序:

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();
            }

        }
    }
}
相关问题