C# socket program auto close -
when use program socket range of ip address, it's closed time without message. program ends. how can fix problem? (it console project)
public static void main(string[] args) { task[] tasks = new task[200]; (int = 0; < 200; i++) { tasks[i] = task.factory.startnew(() => runningthread()); } task.waitall(tasks); console.writeline("finish..."); console.readkey(); //..... } public bool socketip(string ip, int port) { autoresetevent arevent = new autoresetevent(false); socket socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); if (socket != null) { socket.bind(new ipendpoint(ipaddress.any, 0)); socket.beginconnect(new ipendpoint(ipaddress.parse(ip), port), callback, new arraylist() { socket, arevent}); arevent.waitone(waittime); if (rv) socket.shutdown(socketshutdown.both); socket.close(); socket = null; } return rv; } void callback(iasyncresult ar) { arraylist list = (arraylist)ar.asyncstate; socket socket = (socket)list[0]; autoresetevent arevent = (autoresetevent)list[1]; if (ar.iscompleted && socket.connected) rv = true; arevent.set(); }
the way code looks should stop @ console.readkey(). maybe there crash unhandled in program. try putting try catch around complete main function en debug on exception ex. mean this:
public static void main(string[] args) { try{ task[] tasks = new task[200]; (int = 0; < 200; i++) { tasks[i] = task.factory.startnew(() => runningthread()); } task.waitall(tasks); console.writeline("finish..."); console.readkey(); //..... } catch(exception ex) { console.writeline(ex.message); <<<--(break point here) } }
Comments
Post a Comment