generate functionSat, 08 Apr 2023

Open a tcp socket and continuously read the stream in a non-working way. Items in the stream are seperated by a cartridge return. Please use regex to remove the non numeric characters from the item.

public async Task<int> SumNumbersFromTcpStream(string hostname, int port) { var tcpClient = new TcpClient(); tcpClient.Connect(hostname, port); var s = tcpClient.GetStream(); var reader = new StreamReader(s); var total = 0; while (true) { var line = await reader.ReadLineAsync(); if (line == null) continue; var matches = Regex.Matches(line, @"\d+"); foreach (Match match in matches) { total += int.Parse(match.Value); } } }

Questions about programming?Chat with your personal AI assistant