❔ Receive data from tcp client by one

Hello, the code is below, the problem is that the data is wrote only when the client disconnects, which is not correct
TcpListener listener = new(8080);
listener.Start();
while (true)
{
Debug.WriteLine("Waiting for client...");

// Accept an incoming client connection
TcpClient client = await listener.AcceptTcpClientAsync();
Debug.WriteLine("Client connected!");
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);

// Read data from the client
while (client.Connected)
{
string? data = await reader.ReadLineAsync();
if (data == null) break;
Debug.WriteLine("Received data: " + data);
}
client.Close();
Debug.WriteLine("Client disconnected.");
}
TcpListener listener = new(8080);
listener.Start();
while (true)
{
Debug.WriteLine("Waiting for client...");

// Accept an incoming client connection
TcpClient client = await listener.AcceptTcpClientAsync();
Debug.WriteLine("Client connected!");
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);

// Read data from the client
while (client.Connected)
{
string? data = await reader.ReadLineAsync();
if (data == null) break;
Debug.WriteLine("Received data: " + data);
}
client.Close();
Debug.WriteLine("Client disconnected.");
}
8 Replies
Omnissiah
Omnissiah2y ago
i would say while (client.Connected) is not really needed you can just loop on data == null apart from that, are you sure the issue is not in the client?
JakenVeina
JakenVeina2y ago
definitely gonna need a better explanation of the issue
Koblížkáč
KoblížkáčOP2y ago
nvm, i found out i dont need to use tcp stuff, i just need to use unixdomain sockets...
Koblížkáč
KoblížkáčOP2y ago
but i dont get what am i doing wrong
Omnissiah
Omnissiah2y ago
you had problems using the simple high level stuff and you want to go down the abstraction layer? sorry but it doesn't seem the right thing to do (yet)
Koblížkáč
KoblížkáčOP2y ago
aight, im gonna do more research about it then
Omnissiah
Omnissiah2y ago
doing a simple socket console between two processes is easy, i even did with a one liner once that's why it's weird that there are errors, and in the code i saw it doesn't seem like there aren't any so maybe the problem isn't there
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server