Multiple connections on one port

G

Guest

Hi,
I am trying to write an application that connects to the local port with a
TCPListener and also connects to the local port using a TCPCLient
Here is the code:
Dim m_IPPort As Integer = 502
Dim m_IPAddr As String = "10.1.1.204"
Dim m_LocalIPAddr As String = ""
Dim m_CommPort As Integer = 1
Dim locIP As IPAddress
Dim ipLocalEndPoint As IPEndPoint
locIP = IPAddress.Parse(m_LocalIPAddr)
ipLocalEndPoint = New IPEndPoint(locIP, m_IPPort)
Dim m_Listener As New TcpListener(ipLocalEndPoint)
m_Listener.Start()

Dim m_tcpClient As New TcpClient
m_tcpClient.Connect(m_IPAddr, m_IPPort)

And Here is the Error:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred
in System.dll

Additional information: This is usually a temporary error during hostname
resolution and means that the local server did not receive a response from an
authoritative server

Please advise.
Thanks
mblake
 
P

Paul G. Tobey [eMVP]

You lost me. You're trying to create a program that will open a server port
and then connect to itself and send/receive data? What is the text of the
exception which is thrown (there should be more information than just the
SocketException)? It occurs when calling the Connect() method? What device
are you trying to run this on?

Paul T.
 
A

Alex Feinman [MVP]

Try replacing
m_tcpClient.Connect(m_IPAddr, m_IPPort)
with
m_tcpClient.Connect(new IPEndPoint(IPAddress.Parse(m_IPAddr), m_IPPort))
 
G

Guest

thanks Alex,
i replaced that line of code and now i am getting this error:

'No connection could be made because the target machine actively refused it'
Please Help!
 
G

Guest

Hi Daniel,
I also tried this and the application locks up. Does the same thing with the
AcceptSocket Method!
 
D

Daniel Moth

So before you were getting an exception and now it locks up. That is
progress.

AcceptXXX will block (as per the documentation in the link I sent you). You
are not doing this from the main thread are you? If you are, you should
create a dedicated thread to block on the call OR don't expect your
application to respond until a tcp connection is made to it (from another
application).

Worth noting here, since it is an FAQ, that after that blocking call returns
and if you are in your own thread, do not touch UI elements or you
application will also hung (use Control.Invoke).

Cheers
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top