我的下面的服务器套接字代码有问题 -Main()
方法中的循环只执行一次,从不接受任何进一步的输入。
class Server
{
public Socket servSock(int Port)
{
Socket s1 = null;
IPHostEntry ipHE = Dns.GetHostEntry("localhost");
IPAddress ipA = ipHE.AddressList[0];
IPEndPoint ipEp = new IPEndPoint(ipA, Port);
s1 = new Socket(ipEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s1.Bind(ipEp);
return s1;
}
const int BUFFSIZE = 1024;
const int BACKLOG = 255;
static void Main(string[] args)
{
Nwork nwork = new Nwork();
Socket cl = null;
Socket s = nwork.servSock(400);
s.Listen(BACKLOG);
byte[] rcvBuffer = new byte[BUFFSIZE];
for (; ; )
{
string text = "";
Console.Clear();
cl = s.Accept();
Console.Write("Handling Client >> " + cl.RemoteEndPoint +"\n\n\n");
cl.Receive(rcvBuffer, BUFFSIZE, SocketFlags.None);
text = Encoding.ASCII.GetString(rcvBuffer, 0, BUFFSIZE).TrimEnd('\0');
Console.Write(text);
cl.Close();
}
}
}