Click here to Skip to main content
15,434,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating an app which has to wait for TCP messages and when there's one - create text file with that message data. For that I created .NET core console application. However I stumbled upon problem that after writing message/getting exception the application close. How do I keep it running and listening for other messages that might come? Or did I chose the wrong project type for that?

here's my code:

What I have tried:

class Program
{
    static void Main(string[] args)
    {
        TcpListener server = null;
        try
        {
            var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            var ipAddress = IPAddress.Parse("xx.x.xx.x");

            server = new TcpListener(ipAddress, Port);

            // Start listening for client requests.
            server.Start();
            Console.WriteLine("server started");
            // Buffer for reading data
            byte[] bytes = new Byte[1024];
            string data = null;

            // Enter the listening loop.
            while (true)
            {
                // Perform a blocking call to accept requests.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    string pathString = "";

                    if (data.Contains(XXX))
                    {
                        pathString = Path.Combine(folderName, "XXX");
                    }
                    else if (data.Contains(AAA))
                    {
                        pathString = Path.Combine(folderName, "AAA");
                    }
                    else if (data.Contains(BBB))
                    {
                        pathString = Path.Combine(folderName, "BBB");
                    }
                    else
                    {
                        pathString = Path.Combine(folderName, "Unrouteable");
                    }

                    if (!Directory.Exists(pathString))
                    {
                        Directory.CreateDirectory(pathString);
                    }

                    CreateTextFile(pathString, "fsdfsdf.txt", data);
                }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }
         Console.ReadLine();
    }

    private static void CreateTextFile(string path, string textTitle, string data)
    {
        var pathString = Path.Combine(path, textTitle);
        if (!File.Exists(pathString))
        {
            File.Create(pathString).Close();
            using (var tw = new StreamWriter(pathString))
            {
                tw.WriteLine(data);
            }
        }
    }
}
Posted
Comments
F-ES Sitecore 30-Jun-20 10:04am    
There's probably a clue in the exception message.
Luc Pattyn 30-Jun-20 11:43am     CRLF
File.Create(pathString).Close();
That is quite original. Not sure what your intentions are, I am sure there is a better way of getting there.
Richard MacCutchan 30-Jun-20 11:59am    
The try/catch/finally blocks should be inside the while loop. Not the other way round as you have it.
Jin Vincent Necesario 8-Aug-20 11:05am     CRLF
based on your codes here are some suggestion I can give 1. before you check the data, you can check if it is null or empty e.g. !string.IsNullOrEmpty(data) 2. before you call the method CreateTextFile make sure that the path string exists and check if it is null or empty. 3. or maybe you need some elevated permission on the files/folders that you are manipulating.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900