c# - TcpListener won't acknowledge connections -
so writing simple client-server application. should send packet, wait receive packet, send 1 etc... problem is, recieves first packet, when start tcplistener
in second iteration, gives me error:
no connection made because target machine actively refused 127.0.0.1:13
private void listenforconnections() { bool prejelpaket = false; listener = new tcplistener(ipaddress, port); listener.start(); while (!packetreceived) { try { client = listener.accepttcpclient(); listener.stop(); networkstream = client.getstream(); byte[] message = new byte[1024]; networkstream.read(message, 0, message.length); networkstream.close(); string strmessage = encoding.utf8.getstring(message); packetreceived= true; messagebox.show("received message: " + strmessage); client.close(); } catch (exception ee) { thlisten.join(); } } } private void sendpacket(object pclient) { string message = "test message; try { client = (tcpclient)pclient; client.connect(ipaddress, port); networkstream = client.getstream(); byte[] strmessage = encoding.utf8.getbytes(message); networkstream.write(strmessage, 0, strmessage.length); networkstream.close(); client.close(); } catch (exception ex) { messagebox.show(ex.tostring()); } }
create client / networkstream once. store them in property until finished sending , receiving. close , dispose. not stop / close connection between each iteration.
Comments
Post a Comment