c# - Sockets: Only one usage of each socket address is normally permitted -
i want reuse socket after used once, subj exception. found multiple questions same issue no working solution. tried using
code, tried manual closing - without success
a little sample
using system; using system.net; using system.net.sockets; using system.text; namespace sockettest { class program { static void main() { (int = 1; <= 2; i++) { var encoding = new utf8encoding(false); var host = dns.gethostentry("localhost"); var endpoint = new ipendpoint(host.addresslist[0], 11322); console.writeline("iteration #{0}\tenpdoint = {1}", i, endpoint); try { var client = new tcpclient(endpoint); const string message = "hello world!"; byte[] data = encoding.getbytes(message); client.connect(endpoint); var stream = client.getstream(); stream.write(data, 0, data.length); console.writeline("sent: {0}", message); byte[] receivedata = new byte[4096]; int totalbytes = 0; while (true) { int bytesread = stream.read(receivedata, 0, receivedata.length); totalbytes += bytesread; if (bytesread < receivedata.length) break; array.resize(ref receivedata, 2 * receivedata.length); } var response = encoding.getstring(receivedata, 0, totalbytes); console.writeline("received: {0}", response); client.client.shutdown(socketshutdown.both); client.client.disconnect(true); stream.close(); client.close(); } catch (exception ex) { console.writeline(ex.tostring()); } console.writeline(); } } } }
Comments
Post a Comment