C
C#15mo ago
Koblížkáč

❔ 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
boiled goose
boiled goose15mo 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
JakenVeina15mo ago
definitely gonna need a better explanation of the issue
Koblížkáč
Koblížkáč15mo ago
nvm, i found out i dont need to use tcp stuff, i just need to use unixdomain sockets...
Koblížkáč
Koblížkáč15mo ago
but i dont get what am i doing wrong
boiled goose
boiled goose15mo 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áč15mo ago
aight, im gonna do more research about it then
boiled goose
boiled goose15mo 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
Accord15mo 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
More Posts
❔ Memory Leakage for beginnerany tips or roadmaps for learning how to avoid or identify memory leakage issues in your code?❔ help with self referential class typingI was trying to make a node class I can inherit from and ran into a ton of trouble, in my original s✅ Only add certain controllers?Is there a way I can only add/map certain controllers based on if the application is in development ❔ RazorPages - require authorization, but load title/Open Graph information still?I have a page that requires authorization, is there a way to let the title appear for example when s✅ How should I use SelectAsync?I have this: ```cpp static public async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>( ❔ convert to svg imagewhen i try to create a svg element for the image and run it i get a error code that looks like this.❔ System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'Why cant i use the int mainStory inside my array? `System.IndexOutOfRangeException: 'Index was outs❔ dotnet workload install errors.Trying to use the uno framework. Using uno-check to download dependencies. When it executes `dotnet ❔ Is there a way to quickly change all instances of "var" in my code to "Country"?I had ChatGPT do some stuff and it named everything var and I have a Country method, I need to conve❔ Issue with Google authentication in dotnet 7 react app [SOLVED]Hi! I am trying to use google authentication with dotnet 7 react app. I followed the instructions he