为什么在源代码更改后服务器停止响应? [英] Why has the server stopped responding after a change in source code?

查看:104
本文介绍了为什么在源代码更改后服务器停止响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过TCP实现HTTP协议.我已经有一个TCP/IP客户端-服务器程序,该程序运行正常.

I need to implement HTTP protocol through TCP. I already had a TCP/IP client-server program which had been working absolutely fine.

现在,我对源代码进行了一些小的更改,以使其表现为

Now, I have done some minor change in the source code so that it behaves as an HTTP client-server. But, unfortunately it stopped working.

以下代码...

    public void Write(string str)
    {
        if (IsConnected)
        {
            byte[] strBytes = Encoding.UTF8.GetBytes(str);
            byte[] lenBytes = BitConverter.GetBytes(strBytes.Length);
            Array.Reverse(lenBytes);
            writer.Write(lenBytes);
            writer.Write(strBytes);
            writer.Flush();
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

    public string Read()
    {
        if (IsConnected)
        {
            byte[] lenBytes = reader.ReadBytes(4);
            Array.Reverse(lenBytes);
            int len = BitConverter.ToInt32(lenBytes, 0);
            byte[] bytes = reader.ReadBytes(len);
            string str = Encoding.UTF8.GetString(bytes);

            return str;
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

更改为:

    public void Write(string str)
    {
        if (IsConnected)
        {
            byte[] send = Encoding.ASCII.GetBytes(str);
            writer.Write(send);
            writer.Flush();
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

    public string Read()
    {
        if (IsConnected)
        {
            StreamReader sr = new StreamReader(stream);
            string str = sr.ReadToEnd();

            return str;
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

这是什么问题?

详细的源代码

服务器程序和客户端程序都使用以下类.

The following class is used by both server program and client program.

已修改:

namespace MyClientServer
{
    public class ClientClass
    {
        private string Host { get; set; }
        private int Port { get; set; }

        private bool IsConnected = false;

        public string ID { get; private set; }
        public TcpClient Tcp { get; private set; }

        NetworkStream stream;
        private StreamReader sr;
        private StreamWriter writer;

        public ClientClass()
        {
            Random rnd = new Random();
            ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
        }

        //constructor for server program.
        public ClientClass(TcpListener listener)
        {
            Tcp = listener.AcceptTcpClient();

            Host = ((IPEndPoint)Tcp.Client.RemoteEndPoint).Address.ToString();
            Port = ((IPEndPoint)Tcp.Client.LocalEndPoint).Port;

            IsConnected = true;

            stream = Tcp.GetStream();
            sr = new StreamReader(stream);
            writer = new StreamWriter(stream);

            ID = Read();

            Console.WriteLine("Client [{0}] is now connected.", ID);

        }

        public bool Connect()
        {
            if (IsConnected == false)
            {
                Console.WriteLine("Client [{0}] is now connected.", ID);

                IsConnected = true;

                Tcp = new TcpClient(Host, Port);

                stream = Tcp.GetStream();
                sr = new StreamReader(stream);
                writer = new StreamWriter(stream);

                return true;
            }

            return false;
        }

        //constructor for client.
        public ClientClass(string host, int port)
        {
            Random rnd = new Random();
            ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
            Host = host;
            Port = port;
        }

        public void Write(string str)
        {
            if (IsConnected)
            {
                byte[] send = Encoding.ASCII.GetBytes(str);
                writer.Write(send);
                writer.Flush();
            }
            else
            {
                throw new Exception("Client " + ID + " is not connected!");
            }
        }

        public string Read()
        {
            if (IsConnected)
            {
                StreamReader sr = new StreamReader(stream);
                string str = sr.ReadToEnd();

                return str;
            }
            else
            {
                throw new Exception("Client " + ID + " is not connected!");
            }
        }

        public void PrintID()
        {
            Console.WriteLine("Client ID = {0}", ID);
        }

        public void SendIdToServer()
        {
            this.Write(ID);
        }

        public bool Disconnect()
        {
            if (IsConnected)
            {
                if (Tcp != null)
                {
                    Tcp.Close();
                    Tcp = null;

                    Console.WriteLine("\nClient [{0}] is now disconnected.", ID);

                    return true;
                }
            }

            return false;
        }        
    }
}

原始:

namespace MyClientServer
{
    public class ClientClass
    {
        private string Host { get; set; }
        private int Port { get; set; }

        private bool IsConnected = false;

        public string ID { get; private set; }
        public TcpClient Tcp { get; private set; }

        private BinaryReader reader;
        private BinaryWriter writer;

        public ClientClass()
        {
            Random rnd = new Random();
            ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
        }

        //constructor for server program.
        public ClientClass(TcpListener listener)
        {
            Tcp  = listener.AcceptTcpClient();

            Host = ((IPEndPoint)Tcp.Client.RemoteEndPoint).Address.ToString();
            Port = ((IPEndPoint)Tcp.Client.LocalEndPoint).Port;

            IsConnected = true;

            NetworkStream stream = Tcp.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);

            ID = Read();

            Console.WriteLine("Client [{0}] is now connected.", ID);

        }

        public bool Connect()
        {
            if (IsConnected == false)
            {
                Console.WriteLine("Client [{0}] is now connected.", ID);

                IsConnected = true;

                Tcp = new TcpClient(Host, Port);

                NetworkStream stream = Tcp.GetStream();
                reader = new BinaryReader(stream);
                writer = new BinaryWriter(stream);

                return true;
            }

            return false;
        }

        //constructor for client.
        public ClientClass(string host, int port)
        {
            Random rnd = new Random();
            ID = AlphaNumRandom.GenerateUpperCaseString(5, rnd);
            Host = host;
            Port = port;
        }

        public void Write(string str)
        {
            if (IsConnected)
            {
                byte[] strBytes = Encoding.UTF8.GetBytes(str);
                byte[] lenBytes = BitConverter.GetBytes(strBytes.Length);
                Array.Reverse(lenBytes);
                writer.Write(lenBytes);
                writer.Write(strBytes);
                writer.Flush();
            }
            else
            {
                throw new Exception("Client " + ID + " is not connected!");
            }
        }

        public string Read()
        {
            if (IsConnected)
            {
                byte[] lenBytes = reader.ReadBytes(4);
                Array.Reverse(lenBytes);
                int len = BitConverter.ToInt32(lenBytes, 0);
                byte[] bytes = reader.ReadBytes(len);
                string str = Encoding.UTF8.GetString(bytes);

                return str;
            }
            else
            {
                throw new Exception("Client " + ID + " is not connected!");
            }
        }

        public void PrintID()
        {
            Console.WriteLine("Client ID = {0}", ID);
        }

        public void SendIdToServer()
        {
            this.Write(ID);
        }

        public bool Disconnect()
        {
            if (IsConnected)
            {
                if (Tcp != null)
                {
                    Tcp.Close();
                    Tcp = null;

                    Console.WriteLine("\nClient [{0}] is now disconnected.", ID);

                    return true;
                }
            }

            return false;
        }
    }
}

推荐答案

我听了 @Jimi 的建议,还看了

I took the advice of @Jimi and also took a look at the source code from this link.

现在我的源代码如下:

    public string Read()
    {
        if (IsConnected)
        {
            byte[] buffer = new byte[Tcp.ReceiveBufferSize];//create a byte array
            int bytesRead = stream.Read(buffer, 0, Tcp.ReceiveBufferSize);//read count
            string str = Encoding.ASCII.GetString(buffer, 0, bytesRead);//convert to string
            return str;
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

    public void Write(string str)
    {
        if (IsConnected)
        {
            byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(str);
            stream.Write(bytesToSend, 0, bytesToSend.Length);
            stream.Flush();
        }
        else
        {
            throw new Exception("Client " + ID + " is not connected!");
        }
    }

而且工作正常.

这篇关于为什么在源代码更改后服务器停止响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆