Hello all. I am doing a chat server project v socket programming.
I am doing it with visuakl studio 2008, C sharp and i build it with a console application project. Below are my codes and the errors i meet. can any1 help me on how to solve these errors? anyhelp is welcome.
Thx.

private void ServiceClient()
{
Socket client=clientsocket;
bool keepalive=true;
while(keepalive)
{
Byte[]buffer=new Byte[1024];
client.Receive(buffer);
string clientcommand=System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens=clientcommand.Split(new Char[]{'|'};
Console.WriteLine(clientcommand);
if(token[0]=="CONN")
{
for(int n=0;n<clients.Count;n++){
Client cl=(Client)client[n];
SendToClient(cl,"JOIN|"+tokens[1]);
}
EndPoint ep=client.RemoteEndPoint;
Client c=new Client(tokens[1],ep,clientservice,client);
clients.Add(c);
string message="LIST|"+GetChatterList()+"\r\n";
SendToClient(c,message);
lbClients.Items.Add(c);
}

if(tokens[0]=="CHAT")
{
for(int n=0;n<clients.Count;n++)
{
Client cl=(Client)clients[n];
SendToClient(cl,clientcommand);
}
}

if(tokens[0]=="PRIV"){
string destclient=tokens[3];
for(int n=0;n<clients.Count;n++){
Client cl=(Client)clients[n];
if(cl.Name.CompareTo(tokens[3])==0)
SendToClient(cl,clientcommand);
if(cl.Name.CompareTo(tokens[1])==0)
SendToClient(cl,clientcommand);
}
}

if(tokens[0]=="GONE")
{
int remove=o;
bool found=false;
int c=client.Count;
for(int n=0;n<c;N++)
{
Client cl=(Client)clients[n];
SentToClient(cl,clientcommand);
if(cl.Name.CompareTo(tokens[1])==0)
{
remove=n;
found=true;
lbClients.Items.Remove(cl);
}
}
if(found)
clients.RemoveAt(remove);

client.Close();
keepalive=false;
}
}
}
namespace ChatServer
{
using System.Net.Sockets;
using System.Net;
public class Client
{
	private Thread clthread;
	private EndPoint endpoint;
	private string name;
	private Socket sock;
	public Client(String_name, EndPoint_endpoint, Thread_thread. Socket_sock)
	{
		clthread = _thread;
		endpoint = _endpoint;
		name = _name:
		sock = _sock;
	}
	public override string ToString()
	{
		return endpoint.ToString()+ " : " + name;
	}
	public Thread CLThread
	{
		get{return clthread;}
		set{clthread = value;}
	}
	public EndPoint Host
	{
		get{return endpoint;}
		set{endpoint = value;}
	}
	public string Name
	{
		get{return name;}
		set{name = value;}
	}
	public Socket Sock
	{
		get{return sock;}
		set{sock = value;}
	}
}
}
private void StartListening()
{
listener = new TcpListener(listenport);
listener.Start();
while  (true)  {
try
{
Socket s = listener.AcceptSocket() ;
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch(Exception e)
{Console.WriteLine(e.ToString() );
}
}
}

Errors:
------ Build started: Project: msn, Configuration: Debug Any CPU ------
c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\msn.exe /target:exe ChatServer.cs ServerClient.cs Properties\AssemblyInfo.cs StartListening.cs
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(1,9): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(7,6): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(7,8): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(7,19): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(7,24): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,8): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,10): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,41): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,46): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,47): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ServerClient.cs(10,51): error CS1022: Type or namespace definition, or end-of-file expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ChatServer.cs(11,27): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ChatServer.cs(11,45): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ChatServer.cs(11,59): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\ChatServer.cs(11,71): error CS1001: Identifier expected
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\StartListening.cs(1,9): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\StartListening.cs(3,16): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\StartListening.cs(10,21): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\StartListening.cs(10,32): error CS1518: Expected class, delegate, enum, interface, or struct
C:\Documents and Settings\JiE\My Documents\Visual Studio 2008\Projects\msn\msn\StartListening.cs(12,1): error CS1022: Type or namespace definition, or end-of-file expected

Compile complete -- 20 errors, 0 warnings
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Are you placing your ServiceClient() and StartListening() methods inside classes? CS1518 is caused by declaring something other than a class, delegate, enum, interface, or struct in a namespace.
CS1001 is caused by using something other than an identifier where an identifier is expected; an identifier is the name you give to a variable or object. For example, in int x = 1; 'x' is the identifier. You will usually get this error if you have omitted the identifier or if you have used an invalid identifier. Invalid identifiers could include keywords or names which violate the rules for identifiers (such as using a number as the first character).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.