Click here to Skip to main content
14,576,808 members
Rate this:
Please Sign up or sign in to vote.
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
   
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.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100