我现在要建立一个SOCKET服务端,要实现的功能如下:
客户端利用Conn:连接命令用于连接接口服务器。格式: Conn 用户名 口令。如Conn aaa bbb。
如果错误则返回error.
客户端利用Sear:查询编码,返回相关信息。参数为编码和主叫号码。格式: search 编码。如 search 1212122
如果错误则返回error.
end 退出
建议:返回消息用##NL表示换行。疑问:怎么样确定是否已经用Conn连接?客户端用END退出是须要消毁此线程吗?如果消毁线程之后怎么重起?

解决方案 »

  1.   

    让服务端给你回发一个消息,用来确定是否已经连接!
    线程销毁了,用new再建就是了!
      

  2.   

    bool user = false;
            string strRead = null;
                bool done = false;
                TcpListener listener = new TcpListener(portNum);
                listener.Start();
                 Console.WriteLine("Waiting for connection...");
                while (!done)
                {
                    TcpClient client = listener.AcceptTcpClient();
                    Console.WriteLine("connecting....");
                    NetworkStream ns = client.GetStream();
                    
                    byte[] bytes = new byte[1024];
                    int bytesRead = ns.Read(bytes, 0, bytes.Length);
                    if (bytesRead == 0)
                    {
                        continue;
                    }
                    strRead = Encoding.ASCII.GetString(bytes, 0, bytesRead);                if (strRead == "cnn")
                    {
                        byte[] byteTime = Encoding.ASCII.GetBytes("OK");
                        try
                        {
                            ns.Write(byteTime, 0, byteTime.Length);
                            ns.Close();
                            client.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                        user = true;
                        continue;
                    }
                    if (user)
                    {
                        if (strRead == "123")
                        {
                            byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
                            try
                            {
                                ns.Write(byteTime, 0, byteTime.Length);
                                ns.Close();
                                client.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            continue;
                        }
                        if (strRead == "246")
                        {
                            byte[] byteTime = Encoding.ASCII.GetBytes("Yes");
                            try
                            {
                                ns.Write(byteTime, 0, byteTime.Length);
                                ns.Close();
                                client.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            continue;
                        }
                    }
                    else
                    {
                        byte[] byteTime = Encoding.ASCII.GetBytes("error");
                        try
                        {
                            ns.Write(byteTime, 0, byteTime.Length);
                            ns.Close();
                            client.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                    }                
                } 
                listener.Stop(); sjm2003 这样子写是否正确?