c# - No connection could be made because the target machine actively refused it why? -
hi using tcpclient , tcplitner transmit data getting error not connect below code
private void button1_click(object sender, eventargs e) { tcpclient tcpc = new tcpclient("192.168.21.46", 10); networkstream nts = tcpc.getstream(); if (nts.canwrite) { byte[] sends = system.text.encoding.ascii.getbytes(textbox1.text.tochararray()); nts.write(sends, 0, sends.length); nts.flush(); } } private void button2_click(object sender, eventargs e) { tcplistener mylistener = new tcplistener(10); mylistener.start(); while (true) { //accept new connection socket mysocket = mylistener.acceptsocket(); if (mysocket.connected) { //make byte array , receive data client byte[] receive = new byte[64]; int = mysocket.receive(receive, receive.length, 0); char[] unwanted = { ' ', ' ', ' ' }; string rece = system.text.encoding.ascii.getstring(receive); label1.text = rece.trimend(unwanted); } } }
this 2 buttons have added in same form , ip apddress mentioned systems ip address. can tell me why happen. remove firewall setting also.
first of ui hang on button 2 click because it's stuck on while(true) loop use beginacceptsocket(iasyncresult r, object state) async.
second must use loopback address or otherwise firewall should block port 10 assuming it's not open. tcplistener(int port) obsolote , better use tcplistener(ipaddress localddr, int port) , use both loopback address.
Comments
Post a Comment