Connecting to a TcpListener on a different thread, same process

I'm trying to unit test some comm. code over TCP in C#. I've created a quick thread that stands up a TcpListener. Each time the TcpClient tries to connect I get an "Only one usage of each socket address (protocol/network address/port) is normally permitted" exception. Can you not host on and connect to the same port in the same process?

    public void Foo()
        Thread listenerThread = new Thread(TcpListenerThread);



        TcpClient client = new TcpClient(new IPEndPoint(IPAddress.Loopback, 1234));        

    private void TcpListenerThread()
        TcpListener listener = new TcpListener(IPAddress.Any, 1234);
        TcpClient socket = listener.AcceptTcpClient();
        StreamWriter writer = new StreamWriter(socket.GetStream());

        writer.Write(File.ReadAllBytes("../../random file.txt"));



You are using wrong constructor of the TcpClient - this one binds the client to local address and port, so you end up with both the listener and the client trying to grab Use the TcpClient( String, int ) constructor.

Need Your Help

Loading mustache template in jQuery returns Document object instead of raw string

jquery lazy-loading mustache icanhaz.js

I am using icanhaz.js and mustache for loading templates, and I am loading mustache templates on demand using the following method:

JQuery Button - Maintain height when text removed

jquery html jquery-ui css

I have a button which I style using jQuery's UI's button.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.