SocketException, TCPListener, TCPClient reason?
a) Why do I get a
SocketException
when opening a TCPListener
+ TCPClient
@ IP 127.0.0.1
, Port 5089 (there is only my listener at that port, and only my client sends on it) telling me the system actively refused the connection
- however it still goes through and sends everything/gets received?! and the firewall didn't block it
b) what information to look at to know such stuff?62 Replies
Port is in use
Doesn't have to be your socket
Did you try changing port?
Or use
0
as port
0
meaning it will use a random available portport isn't in use, since there's only my 1 server and my 1 client
Any application on your computer can use the port
it doesn't have to be your program
Nothing uses the port but these 2 programs - says netstat
And what happens if you change the port as previously mentioned?
say to 0 so it uses a random available port
changing the port didn't help, tried multiple very crazy ones
or actually tried several that were ok in netstat
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
wait
I don't use IIS/IIS Express, my original intention for using the TCPClient was just to be able to save some Diagnostic data to disk from a SourceGenerator - which sadly have an Analyzer Rule that prohibits usage of IO and the File- / Path-Namespaces during their activities. [which makes it a bit hard to debug their work]
I give you the code in a sec
Pastebin
using System;using System.Diagnostics;using System.Net;using System...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
Client - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
nope, no firewall
because it actually works
the send works, but I get an exception every time I do it
additionally my firewall hasn't had anything in the blocked log
+ I found out when the server is configured to use port 0 - this is when the connection won't work (since server port = 0, client port != 0, the connection fails and that is the only time that the exception ever gets caught in my catch (Socketexception ex), clientside
the other times when the server is set correctly and when the connection actually works but yet excepts... it doesn't get caught
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
which sadly have an Analyzer Rule that prohibits usage of IO and the File- / Path-Namespaces during their activitiesCorrect. These things are verbotten. Do not use them
back ok
I know, but does this relate to the error I'm getting?
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
it doesn't matter what he says, because there's an analyzer rule that shows you an Error that prevents compilation when you use System.IO or System.IO.Path
but it doesn't do so when using the networkstack
there's no interference with the network stack to be true
the one thing has nothing to do with the other
so what they don't want is you spawning your own sourcecode files when using the sourcegenerator, that must have been the initial idea
@sharwell, this is another thing we apparently need to block
of blocking IO
I cannot say this strongly enough: do not do IO
Network calls are IO
to be true
Do not use it
We do not want you doing any kind of IO
IO, in general, will not work
I tried to use the AnalyzerConfigOptions , ..
but it didn't work, I couldn't get the diagnostic outside
using the steps described
what do you mean "couldn't get the diagnostic outside"?
(wherever that was, i suppose microsoft.com)
well I don't remember what the goal was
whether it was spawning a file or printing it to console or something else
I cannot remember
but it clearly didn't work, then I went for writing my TCP client / sv
that's the big deal?
I know this is not a good procedure, since you state, whenever IO happens during SourceGenerators' activities - you cannot guarantee the correct execution and random stuff might happen
If you're trying to troubleshoot an analyzer, write a unit test
Much easier, more repeatable
no I am not trying to trouble shoot an analyzer
And at the end, you have tests
I tried to trouble shoot my own code in the sourcegen
Then why are you putting IO in your source generator?
because I cannot use the Debugger inside it?!
and the Diagnostic didn't do whatever it was supposed to
i'm afraid
Why do you think you can't debug your source generator?
That's what tests are for
because it didn't stop on any breakpoints
Did you write a unit test?
nope
Then... what were you expecting?
once you write a unit test, you can debug the test and it will stop at breakpoints
since when do you have to write unit-tests to be able to debug VS compilation/compiled runtime
that's not the way we look at it
writing unit tests is so easy and so reliable for debugging analyzers and source generators that all the work on looking at other approaches became irrelevant
basically there is nothing left to gain. it works as close to perfect as anyone could want
why do you enforce the usage of unit-tests
Everyone, and I mean everyone, reacts the same way to our suggestions to write unit tests. With visceral denial. And by the time they're done, almost everyone agrees it was the right approach
There are a couple here and there that still disagree. But almost everyone does not
thank you for enlightening me regarding the UnitTest. and yet, does anyone of you two smart people, have an idea what the answer to my actual question is?
: a) Why do I get a SocketException when opening a TCPListener + TCPClient @ IP 127.0.0.1, Port 5089 (there is only my listener at that port, and only my client sends on it) telling me the system actively refused the connection - however it still goes through and sends everything/gets received?! and the firewall didn't block it
b) what information to look at to know such stuff?
what question was that?
1) IO behavior is undefined. SocketException is allowed. Not having a SocketException is also allowed.
in regard of 1) ok, what if I repeat it without a sourcegen
and the same thing happens?
then it would be a question for dotnet/runtime, different team
undefined I guess means you're using a not-thread-safe-way of parallel task processing?
is that correct?
(thread safe for invocation of external code)
undefined means you cannot infer anything about the behavior
well, but why is it undefined?
because we chose to not attempt to define or support it
it's fairly common in compiler work to leave things as undefined
as soon as we define it, it becomes a guarantee that we have to preserve basically forever
Pasteboard - Uploaded Image
Simple and lightning fast image sharing. Upload clipboard images with Copy & Paste and image files with Drag & Drop
see, I unloaded all sourcegenerator projects, and removed any code that depended on that
and yet I get the same message
I have no experience with that API
it has nothing to do with the API, I can show you what happens:
1/3: Program.Main:
2/3: Client:
3/3 Server:
sorry - just realized the last few lines were missing
that's trivial code, I mean it should be possible to read it in discord and make assumptions
@sharwell , @333fred
Like Sam, I don't have much experience with sockets
I won't be able to help with this as I'm not familiar with these APIs. I haven't done any network I/O in probably almost 10 years
me neither
just this
it's absolutely trivial, I took the example from the Microsoft-page even for writing the client and the server https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/sockets/tcp-classes
Use TcpClient and TcpListener - .NET
Learn how to use the TcpClient class to create a socket to request and receive data using TCP in .NET.
I would close this thread and start over; just use your sample code above, and then hopefully someone who can actually help you will
so it was actually just a goto-quicker completed coding work?
This thread has too much cruft in it
because nobody could appear on the stage and say
:)
hey I have an error,... using IO and a sourcegen, right?
to be fair, it's a good choice, since it prevents people from spawning actual sourcefiles
then just tell me how to correctly use the diagnostic?
Let us know when you've written a unit test 🙂
actually I gave up on that project
since then
but it makes no sense for me to quickly hack something together and write a unit-test for ensuring it works correctly
when I just want to try something out
a Unit-Test,... that's what I write when I want to ensure it works forever
so I see any issues in the CI
So the answer was ...
someone is running a Virtual Machine on my computer (and probably I am in a VM right now) - since after uninstalling the Hypervisor in Add/Remove Programs -> Windows Features -> []Hypervisor, reboot .... but doing /systeminfo still gives me 'A hypervisor has been detected.'
closing/closed the post