I am working on LAN chat software over the past few days. I want to learn how to write winform. But I have encountered a problem and I don't know how to solve it. Please refer to the following for details. Please help me solve the problem:
1. This error will pop up after running:
2. After clicking OK, the following exception is prompted:
3. I found through one-step debugging:
My IPaddress address = iphostentry. Addresslist [0]; the first element of Addresslist [0] is used, but the exact IP address of my computer should be Addresslist [2].
However, I have also tested it on other computers. The exact IP addresses they run include Addresslist [1], Addresslist [3], and Addresslist [5]. I did it, please help me solve this problem.
4. My source code is as follows:
/// <Summary> /// process the received information and process the file and common messages respectively. /// </Summary> private void merge enews () {try {// initialize accept socket: addressing scheme, socketreceive = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // obtain the local IP address and set the port string hostname = DNS for receiving information. gethostname (); iphostentry = DNS. gethostentry (hostname); IPaddress address = iphostentry. addresslist [0]; ipreceive = new ipendpoint (address, 8001); // bind the local IP address and accept port to the accept socket socketreceive. BIND (ipreceive); // listening port, and set the listening cache size to 1024 byte socketreceive. listen (1024);} catch (exception ERR) {MessageBox. show (err. message);} // defines the buffer byte [] buff = new byte [1024] When receiving information; // continuously accepts the information sent by the client while (true) {// define a chat socket to accept information socket chat = socketreceive. accept (); // defines the chatsession cs = new chatsession (chat, this. lvwdisplayuser); // defines a new thread to receive information sent by other hosts thread newthread = new thread (New threadstart (CS. startchat); newthread. setapartmentstate (apartmentstate. sta); // start the new thread newthread. start ();}}
Please give me more advice.