c# - Connecting to a TcpListener on a different thread, same process -


i'm trying unit test comm. code on tcp in c#. i've created quick thread stands tcplistener. each time tcpclient tries connect "only 1 usage of each socket address (protocol/network address/port) permitted" exception. can not host on , connect same port in same process?

    [test]     public void foo()     {         thread listenerthread = new thread(tcplistenerthread);          listenerthread.start();          thread.sleep(5000);          tcpclient client = new tcpclient(new ipendpoint(ipaddress.loopback, 1234));             }      private void tcplistenerthread()     {         tcplistener listener = new tcplistener(ipaddress.any, 1234);         listener.start();         tcpclient socket = listener.accepttcpclient();         streamwriter writer = new streamwriter(socket.getstream());          writer.write(file.readallbytes("../../random file.txt"));      } 

you using wrong constructor of tcpclient - 1 binds client local address , port, end both listener , client trying grab 127.0.0.1:1234. use tcpclient( string, int ) constructor.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -