TcpListener actively refused connection - vb.net

I am maintaining an application that uses a TcpListener to listen for incoming communication. The following code opens the connection:
Dim listener As TcpListener
_listenFailed = False
Try
listener = New TcpListener(System.Net.IPAddress.Parse(Me.Host), Me.Port)
listener.Start()
Catch ex As Exception
' an error here means the settings are likely bogus
_listenFailed = True
Return
End Try
While Not _stoplistening
{
' Accept connection
}
The problem that I am having is that when i send a file from a different computer, I get the error "No connection could be made because the target machine actively refused it."
I have checked for firewalls and antivirus, and there were no blocks. I used
netstat -a -n
to determine that the port is active and listening. Both applications are running from Visual Studios in Administrator mode, not that this should make a difference. I have a break point set at the first line of the accept connection code, but it never gets run.
I stopped the application and examined the TcpListener, and found that there were a couple errors if I dug deep down. At TcpListener.LocalEndpoint.IPEndPoint.IPAddress.Address.ErrorCode there was the error 10045, "OperationNotSupported". Also, at TcpListener.Socket.RemoteEndPoint.ErrorCode there was an error 1057, "A request to send or receive data was disallowed because the socket is not connected, and (when sending on a datagram socket using a sendto call) no address was supplied. I don't know if either of these errors are relevant.
If anyone has any insights as to what might be causing this problem, or steps that can be taken to trace the root of the issue, I would be grateful.

Try changing this:
listener = New TcpListener(System.Net.IPAddress.Parse(Me.Host), Me.Port)
To this:
listener = New TcpListener(System.Net.IPAddress.Any, Me.Port)
This will allow you to listen across all network interfaces.
Additionally, you can use IPv6Any instead of Any if you want to target IPv6 instead. This choice has an impact on the address used by the client side obviously.

Related

VB.Net checking if a port is open and accepting connections

I'm writing a VB.Net app which connects to a remote server and listens for data on that port.
I've got the following function which checks that the IP/port in question is listening for connections or not. When the process on the remote port is running is works fine but when it's not running rather than the exception handler running I get an ugly dialogue box in Visual Studio rather when I run it.
Any ideas why the exception handler is not firing ?
Private Function testSelectedPort(ip As String, port As Integer) As Boolean
' Function to open a socket to the specified port to see if it is listening
' Connect to socket
Dim testSocket As New System.Net.Sockets.TcpClient()
Try
testSocket.Connect(ip, port)
' The socket is accepting connections
testSocket.Close()
Return True
Catch ex As Exception
' The port is not accepting connections
Return False
End Try
Return False
End Function
The error that's being thrown in "No connection could be made because the target machine actively refused it" and it's being thrown at the testSocket.Connect(ip, port) line.
I thought that it would execute the code in the Catch section if it failed to connect ?
Once the exception is thrown it should be handled by the Catch-block. There are only a few exceptions that are generally not very easy to handle via Try/Catch (for instance a System.AccessViolationException), but those do not include the errors a socket might throw.
Please make sure that you haven't ticked the specific exception type to break when thrown, in the Exception Settings window

VisM with VB.net Connection "Server Receive Timed Out"

I added a VisM control into vb.net via adding a reference to VisM.ocx and adding it in my toolbox as an activeX control.
I added the following code in a button:
Try
'open the connection
AxVisM1.Connect("CN_IPTCP:127.0.0.1[57772]", "LIVEDATA")
'do stuff.
MsgBox("Cache is now active")
'close the connection
AxVisM1.DeleteConnection()
Catch ex As Exception
'close the connection
AxVisM1.DeleteConnection()
MsgBox(ex.ToString)
End Try
however when I run the application the connection hangs, followed by a messagebox that says "Server Receive Timed Out".
I have tried turning off my firewall, and even my antivirus. How will I resolve this? Also, is what I am trying an effective way to access GLOBALS variables in VB.net??
I'm not familiar with VisM, but you using wrong port, and this port only for web, try to change it to 1972, which is used by default. Your server may use another one. Your real port number you can find at System Management Portal on page About (link in the header), and parameter is - Superserver Port.

FIX Protocol 4.4 Connection not responding to Login

To anyone who can help,
I am having a show stopping issue connecting to the test server.
My TCP/IP connection is established via:
Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
I send the connection message to the server via:
_socket.Send(_txData);
where _txData is a byte[] _txData array initialized with the connection request message:
8=FIX.4.4\0019=121\00135=A\00149=NotRealSenderID\0 0156=NotRealTargetID\00134=1\00152=20140522-20:12:04.392\00198=0\001108=30\001554=NotRealPassw ord\00110=017\001 converted to a byte array.
I wait for the login success message via:
int responseCount = _socket.Receive(_rxBuffer);
but after a few seconds, the _socket.Receive unblocks and returns zero bytes suggesting unsuccessful connection.
What am I doing wrong?
Is my login message at fault? Is it the way I establish server connection? I am at a loss!
Currently, My default Windows 7 Firewall is disabled, and I attempted to telnet into the server, and the image below snapshots the outcome:
Any help will be appreciated, as this is a major show stopper for me.
Thanks.
I managed to resolve this issue. The problem was with the SOH character in my message.
I was using the literal SOH = "\001" converted to a byte, when I should have done this: SOH = (char) 0x1; DUH!!
For the record, I used the following tools to 'debug' this issue:
Wireshark with display filter and capture filter set to the host server's ip address, and MINI-FIX to generate and transmit FIX messages to the host server.
I then compared the output from my client to those produced by MINI-FIX via Wireshark. It didn't take me long to spot the error from there.
What is "the test server" ?
Can you ping it ?
From the look of that telnet session you haven't permission to make a connection, or there is nothing running at the destination.

Bio_free not closing connection

I have developed a ssl client using the code shown in this website
http://www.chesterproductions.net.nz/blogs/it/c/an-ssl-client-using-openssl/245/
But after calling r = BIO_free(bio); the connection still does not get closed. Only after closing my client application does the connection close.
I am verifying this using CurrPorts software, which shows which all ports are in connected state.
I have tried BIO_ssl_shutdown(bio); but it didnt work either.
Ok I found the fault. It was the server not clearing its FD_SET which was creating an issue. Though I am not sure why it happened so :(

Difference between new TcpClient(MachineName, port) and new TcpClient(new IPEndpoint(..., Port))

Hej Hej,
I have a .Net program that has to run on a cluster of server 2008.
To find out the right IP I resolve the dns by
GetHostEntry(VarDefinedInfConfig).AddressList(0)
but when I am converting my old code (this code picks the wrong IP) dns.GetHostName().AddressList(0) => this returns a virtual IP and not the right one.
So I changed my TcpListener to (dns is parameter from config)
Dim listener As TcpListener = New TcpListener(New IPEndPoint(Net.Dns.GetHostEntry(dns).AddressList(0), 8001))
listener.Start()
In the old way a tcpclient was defined by this code
Dim client As TcpClient = New TcpClient(Environment.MachineName, 8001)
Console.WriteLine("Done...")
client.Close()
This also connects to the wrong IP so I found the overload of TcpClient and used that one
New way:
Dim client2 As TcpClient = New TcpClient(New IPEndPoint(Net.Dns.GetHostEntry(dns).AddressList(0), 8001))
Console.WriteLine("Done")
client2.Close()
But when I use the second one I got the exception "Only one usage of each socket address (protocol/network address / port) is normally permitted.
Weird thing is if I get the IP from the MachineName is exactly the same as the ip retreived from dns with parameter.
Does anybody know the cause of this exception? Normally they should have the same result.
Greetz,
Jonathan
Is it possible that you overlooked the main difference between the 2 mentioned TcpClient constructors (MSDN)?:
TcpClient(String, Int32) .. connects to the specified port on the specified host.
TcpClient(IPEndPoint) .. binds it to the specified local endpoint.
So effectively, with the first constructor a socket is opened on an available, "OS-assigned" local port and then connected to the server, whose address or DNS-name and port are passed in as arguments; after that you're ready to send or receive data.
With the second constructor a socket is just opened on a certain local port (which is identified in the endpoint argument), but that's all - no connection to any [remote, or even local] server is made, because no server info is known yet; you'd need to call one of Connect(.) methods before making any comm operations (similar to the workaround you found).
Why don't you specify the correct IP in the application config?
Only one usage of each socket address (protocol/network address / port) is normally permitted
Means that someone (your or another application) is already listening on that ip/port.
Right now I use an work arround. I create an TcpClient() with an empty constuctor and then I use the Connect(IPendpoint) and this works.