Correct use of StreamWriter

2019-03-06 19:22发布

问题:

After several attempts I can't get StreamWriter to build / work corectly so I am doing something fundamentally wrong (C#, Visual Studio)

I have an exisitng TCP Client which connects and acts as a reader, this is working without fault -

private System.Net.Sockets.NetworkStream ns;
        private System.IO.StreamReader sr;
        private string strIP;
        private string strPort;
        public System.Net.Sockets.TcpClient tc;

        public Socketclient(Reader.Search objSearch)
        {
            m_search = objSearch;
        }

        public void EthernetConnection()
        {
            try
            {
                bool flag = !System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + "\\IPAddress.txt");
                if (!flag)
                {
                    System.IO.TextReader textReader = System.IO.File.OpenText(System.Windows.Forms.Application.StartupPath + "\\IPAddress.txt");
                    string s = textReader.ReadLine();
                    textReader.Close();
                    char[] chArr = new char[] { ';' };
                    string[] sArr = s.Split(chArr);
                    strPort = sArr[0];
                    strIP = sArr[1];
                    flag = strIP == System.String.Empty || strPort == System.String.Empty;
                    if (!flag)
                    {
                        int i = System.Convert.ToInt16(strPort);
                        tc = new System.Net.Sockets.TcpClient(strIP, i);
                        flag = !tc.Connected;
                        if (!flag)
                        {
                            ns = tc.GetStream();
                            sr = new System.IO.StreamReader(ns);
                            m_search.threadClient = new System.Threading.Thread(new System.Threading.ThreadStart(ReceiveData));
                            m_search.threadClient.Priority = System.Threading.ThreadPriority.Lowest;
                            m_search.threadClient.Name = "Ethernet Thread";


   }

I then want to add (in another .cs which is part of the same application) a StreamWriter thread to write back some characters to the same port and then close the StreamWriter thread (leaving the reader running) -

private System.IO.StreamWriter sw;

string line = "TH1/r/n";
using (StreamWriter sw = new StreamWriter(ns));
sw.WriteLine(line);
sw.Flush();
sw.Close();

Which, somehow (I think) needs to refer back to

ns = tc.GetStream();

Any thougts appreciated

Regards Active

回答1:

this sort of question has been asked many times already.

Receving and sending data in C#

brief cut and past from example above showing sending of data

using(TcpClient tcpClient = new TcpClient("ADDRESS"))
{
    NetworkStream networkStream = tcpClient.GetStream();
    using(StreamWriter streamWriter = new StreamWriter(networkStream))
    {
        streamWriter.WriteLine(data);
    }
}


回答2:

The issue was the using block around StreamWriter, removing it allowed the underlying stream to continue.



标签: c# tcp