Reputation: 85
I'm creating a messaging application for andriod/ios, but I am completely new to c# and networking, I've followed the first steps of a simple socket tutorial to get me started in networking (https://www.youtube.com/watch?v=KxdOOk6d_I0) but I get the error:
error "CS1022: Type or namespace definition, or end-of-file expected".
I'm assuming that it has something to do with the namespace because im new to c# and don't actually understand what the namespace does, but my compiler says there are no errors (I'm using visual studio code if that makes a difference) but it may be something else.
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();
}
}
}
}
it should say server started..." or throw up an exeption but this is what im getting every time:
[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
Upvotes: 0
Views: 38366
Reputation: 5634
both answers are correct.
System.Net using was missing. There was a typo in addresslist.
One more problem was - The main function should be spelled as "Main" with capital M.
Full program:
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();
}
}
}
}
Upvotes: 2
Reputation: 561
The error shows The file could not be compiled. So, most probably it's compiler error. I guess below if it's not a typo, the spelling of ipaddress missing a d and so thus AddressList.
IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
Upvotes: 2
Reputation: 11963
You are missing namespace import.
Add
using System.Net;
and fix the typo AdressList
to AddressList
Upvotes: 2