我写的socket监听过一段时间就需要重新启动一下程序是什么原因

private void StartReceive()
{
string msg = null;

        IPEndPoint iep = new IPEndPoint(IPAddress.Parse(PublicVariables.BJIP), PublicVariables.PORT);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        socket.Bind(iep);
        socket.Listen(PublicVariables.JTS);
        Socket newSocket;
        while (true)
        {
            try
            {

                newSocket = socket.Accept();
                byte[] byteMessage = new byte[1024 * 1024];
                int js = 1;
                js = newSocket.Receive(byteMessage, byteMessage.Length, 0);
                Console.WriteLine(js);

                msg = Encoding.UTF8.GetString(byteMessage);
                msg = msg.Replace("\0", "");
                PostGetData pgd = new PostGetData();
                string cz = msg.Substring(0, 4);
                if (cz.Equals("gxsj"))
                {
                    NetworkStream stream = new NetworkStream(newSocket);
                    bool result = pgd.SendFile("caipu.db3", stream);
                    stream.Close();
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("gkkt"))
                {
                    string ctInfo = null;
                    byte[] bytes = null;
                    ctInfo = pgd.GetCtInfo();
                    bytes = new byte[1024 * 1024];
                    bytes = Encoding.UTF8.GetBytes(ctInfo);
                    newSocket.Send(bytes);
                    //newSocket.Close();
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("gxtp"))
                {
                    NetworkStream stream = new NetworkStream(newSocket);
                    bool result = pgd.FsFile("image.zip", stream);
                    stream.Close();
                    newSocket.Close();
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("gkdc"))
                {
                    string data = msg.Substring(6);
                    Regex regex = new Regex("<<");
                    string[] strs = regex.Split(data);
                    SqlHelper helper = new SqlHelper();
                    byte[] bytes = new byte[1024 * 1024];
                    try
                    {
                        string result = helper.Gkdc(strs);
                        bytes = Encoding.UTF8.GetBytes(result);
                        if (newSocket.Connected)
                        {
                            newSocket.Send(bytes);
                        }

                        newSocket.Dispose();
                        msg = null;
                    }
                    catch
                    {
                        bytes = Encoding.UTF8.GetBytes("error");
                        if (newSocket.Connected)
                        {
                            newSocket.Send(bytes);
                        }

                        newSocket.Dispose();
                        msg = null;
                        continue;
                    }

                }
                else if (cz.Equals("ckxf"))
                {
                    string[] strs = msg.Split('@');
                    string str = strs[0].Substring(6);
                    SqlHelper helper = new SqlHelper();
                    string result = helper.ckxf(str);
                    NetworkStream stream = new NetworkStream(newSocket);
                    bool results = pgd.FsFile(str + ".log", stream);
                    stream.Close();
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("gqcp"))
                {
                    SqlHelper helper = new SqlHelper();
                    List<string> lists = helper.gqcp();
                    TxtFileManager tfm = new TxtFileManager();
                    tfm.DelFile("gqcp.log");
                    if (lists.Count > 0)
                    {
                        for (int i = 0; i < lists.Count; i++)
                        {
                            tfm.Write("gqcp.log", lists[i].ToString());
                        }
                    }
                    else
                    {
                        tfm.Write("gqcp.log", "null");
                    }
                    NetworkStream stream = new NetworkStream(newSocket);
                    bool results = pgd.FsFile("gqcp.log", stream);
                    stream.Close();
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("tcsl"))
                {
                    string data = msg.Substring(6);
                    Regex regex = new Regex("<<");
                    string[] strs = regex.Split(data);
                    SqlHelper helper = new SqlHelper();
                    string result = helper.GetTcsl(strs[0], strs[1], strs[2]);
                    byte[] bytes = new byte[1024 * 1024];
                    bytes = Encoding.UTF8.GetBytes(result);
                    newSocket.Send(bytes);
                    newSocket.Dispose();
                    msg = null;
                }
                else if (cz.Equals("gktc"))
                {
                    string data = msg.Substring(6);
                    Regex regex = new Regex("<<");
                    string[] strs = regex.Split(data);
                    SqlHelper helper = new SqlHelper();
                    string result = helper.Gktc(strs);
                    byte[] bytes = new byte[1024 * 1024];
                    bytes = Encoding.UTF8.GetBytes(result);
                    newSocket.Send(bytes);
                    newSocket.Dispose();
                    msg = null;
                }
            }
            catch (Exception ex)
            {
                TxtFileManager tfm = new TxtFileManager();
                tfm.Write("sys.log", ex.ToString() + "  " + DateTime.Now.ToString());
                continue;
            }
        }
    }

有可能是因为你退出你的socket服务端的时候,端口仍然被占用,在linux系统下,有时候你即使调用kill杀死进程后,端口仍然被占用。
这总情况下,你再次启动你的服务端程序将无法正常运行。当你重启后,所有系统占用都被释放,以及上次bind()过的端口。
因此,当你无法正常运行时,修改一个端口即可,(记住客户端要与服务端保持一致,要修改,都需要修改)
希望对你有用!

查看一下你侦听的socket是否被异常关闭了,或者进程退出了。造成socket没有侦听