SocketException thrown when invoking TcpListener.Start() - c#

I am aware that there is already a similar question here. However, the only answer there does not solve my problem.
Here is my code:
TcpListener TcpListener = new TcpListener(localaddr: IPAddress.Any, port: 8080);
TcpListener.Start();
Executing TcpListener.Start(); gave me this error:
System.Net.Sockets.SocketException: 'An attempt was made to access a
socket in a way forbidden by its access permissions'
I have already defined an inbound security rule inside my firewall to make Port 8080 publicly accessible (I am aware that this is generally not recommended, but I am temporarily doing this for testing purposes):
When I run netstat -o, I am able to verify that Port 8080 does not already have an established connection.
For what it is worth, I am starting a TcpListener on an Azure virtual machine.
Any suggestions on how I can fix the SocketException?
UPDATE:
When I run netstat -o -n -a | findstr 0.0.0.0:8080, I see the following result:
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 4
Could the SocketException have been thrown because there already is a TcpListener listening on Port 8080?
I tried terminating the existing TcpListener by running taskkill/pid 4 /F (in order to see whether SocketException will still be thrown after terminating it), but didn't succeed:
ERROR: The process with PID 4 could not be terminated. Reason: Access
is denied.

Did the PID of 4 really not set alarm bells ringing before you tried taskkill? It's unusually low. In most windows systems I've worked on, it's been the system "process" that is the OS.
It's probable that something has registered to use that port as part of a URL reserved via HTTP.SYS.
If you have IIS running look for sites that are running on that port.

Related

C# find process id (pid) of client making an UDP/TCP call

I have a server listening for connections on UDP and another on TCP.
I need to know what process initiated the connection. Is that possible?
I have found multiple solutions, but all imply checking each process for each port, which has a performance issue in my view. Also, when i tried this, i only get the PID of my server application, not the client.
Thank you
you can try this
netstat -a -n -o | find "1688"
You will get the exact output of the process
UDP 10.4.112.77:55866 *:* 1688
UDP 127.0.0.1:1900 *:* 1688
UDP 127.0.0.1:55868 *:* 1688
Try this for complete process id port and process name.
Open a command prompt window (as Administrator) From "Start\Search box" Enter "cmd" then right-click on "cmd.exe" and select "Run as Administrator"
Enter: netstat -abno
Find the Port that you are listening on under "Local Address"
Look at the process name directly under that.
you can collect the information and then the parse the output
this one stack link might be helping you.
Which PID listens on a given port in c#

Renci ssh.net No connection could be made because the target machine actively refused it

We recently deployed a project into production and are now receiving this error message when we attempt to connect to the external sftp machine, "No connection could be made because the target machine actively refused it". When I was developing the application and testing it, we had no issues connecting to this server.
What would be different? I have administrative privileges and the app pool on the production server does not. I'm not sure if this could be what's causing the issue or if it may be something on the external client's server or their firewall blocking us.
If you are using localhost, specify 127.0.0.1 instead.
When I was using SSH.Net and the host was specified as localhost it threw the exception that the host actively refused the connection WSAECONNREFUSED - 10061. When I specified 127.0.0.1 instead, the connection could be made.
I think this is due to the implementation in SSH.Net:
internal static IPAddress GetIPAddress(this string host)
{
IPAddress ipAddress;
if (!IPAddress.TryParse(host, out ipAddress))
ipAddress = Dns.GetHostAddresses(host).First();
return ipAddress;
}
Which does not resolve a valid hostname for localhost, apparently. C# Interactive yields the following:
> Console.WriteLine(Dns.GetHostAddresses("localhost").First());
::1
The value ::1 does not seem to be a valid IP address for sockets to use.
This is a standard TCP error
WSAECONNREFUSED - 10061
From the Client's point of view it means 'there is no socket at that address listening to that port' (the "actively refused" is a red herring).
i) check the address
ii) check the port
iii) check firewall(s)
From our extensive experience (we offer own networking components) -- it's a firewall problem. The firewall doesn't let your requests pass. It's a common situation when you run say Putty, and it works, but your code doesn't. This is because many firewalls detect well-known applications and let them pass, while preventing other applications.

No connection could be made because the target machine actively refused it C#

I tried to play around with malware stranger sent me. I open it in vmware and run microsoft network monitor
screenshot
So I tried to make tcp connection using C# like this:
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("darcometweb.no-ip.org", 225);
tcpclnt.Close();
but it throws exception: Message "No connection could be made because the target machine actively refused it 95.244.217.192:225"
To make sure I also tried it in Python but it also throws the same exception
import socket
s = socket.socket()
host = 'darcometweb.no-ip.org'
port = 225
s.connect((host,port))
s.close
print 'done'
Anyone knows what is wrong ?
# Syed Ali Taqi: I have looked into that question, however in my case the malware can make connection but my c# app can't
Usually happens when the port is closed/ no port forwarding on the router/ firewall or anti virus blocking the connection.
First open command line and type
netstat -a
to see all the open connections and ports, if your port listed then go to
Open port checker and type in your port to see if there is an access from outside your network.
If no i would go with port forwarding then, windows firewall, anti virus.

Why does my "remote" machine have an ESTABLISHED connection to my dev machine?

I have created a simple "proof of concept" app that sends a text message over sockets using C# between two Windows computers (XP sending, W2K receiving).
When I run "netstat -a" on the receiving computer, I see a couple of strange things:
netstat tells me:
...
TCP <thismachinename>:netbios-ssn <DevMachineName>:1330 ESTABLISHED
...
--and:
TCP <thismachinename>:1041 a65.197.244.82.deploy.akamaitechologies.com:http CLOSE_WAIT
So:
1) Why is there a connection with my Dev machine (It SHOULD be listening on port 62222, but it's not, yet this mysterious ESTABLISHED connection does exist...)
2) Who/what/why is this akamai technologies connection?
Note: I restarted the remote/listening machine this morning; I checked netstat -a to be sure it was not listening on port 62222 yet (it wasn't); I then started the dual-purpose app that should listen on that port; I ran netstat -a again, and it was STILL not listening on port 62222. Yet these other two odd things...
The established connection to <thismachinename>:netbios-ssn is because you have connected to a Windows Share, printer or something on thismachinename from DevMachineName.
The second connection is that your computer has for some reason connected and downloaded something from akamai. The connection is closed, but in CLOSE_WAIT mode which it is for 120 seconds (if I remember the timeout correctly).
For the listening on port 62222 that is not appearing - do you get any errors in your app when you are opening the listening socket?

TCP connection lost problems in c#

In my application, I have a server listening on a port on localhost for client connection. Then I have clients running and establishes connection with the server. I used tcpListner in c# for the server and the clients were written in perl using Inet function.
The connection between clients and server were established without a problem and a message from client is received by the server. However, after about 3 to 4 seconds, the connection is lost. I use netstat -a to see if the status of the connection is still "established". The result is "Close_wait" after I use netstat command on DOS.
How can I debug this problem? Which side is causing the lost of connection?
Thanks
Edit:
$tx_socket = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 9000,
LocalPort => 8000, Proto => 'tcp',
Type =>SOCK_STREAM, Reuse=>1 ) or die("$!\n");
die("init_tx_socket failed!\n") unless $tx_socket; print "Socket good!\n";
$tx_socket->autoflush(1); $tx_socket->print("Hello");
sleep(5);
$tx_socket->print("World");
Capturing network packets with Microsoft Network Monitor or Wireshark, or use System.Net tracing.
Socket issues cannot be easily analyzed and handled without such analysis.

Categories