visual studio - C# SocketException was unhandled -


i went through youtube tutorial on tcpip servers , clients , using code it: https://www.youtube.com/watch?v=uxfso7xsswk both part 1 , part 2. have redone code twice , have made sure same. whenever run server error "socketexception unhandled" @ line 'tcplistener.start();' in following method.

private void tcpserverrun()         {             tcplistener tcplistener = new tcplistener(ipaddress.any, 5004);             tcplistener.start();             updateui("listening");             while (true)             {                 tcpclient client = tcplistener.accepttcpclient();                 updateui("connected");                 thread tcphandlerthread = new thread (new parameterizedthreadstart(tcphandler));                 tcphandlerthread.start(client);             }         } 

i same error when run client, @ line 'client.connect(ipaddress.parse("10.3.29.252"), 5004);' in following method:

private void connectasclient()         {             tcpclient client = new tcpclient();             client.connect(ipaddress.parse("10.3.29.252"), 5004);             networkstream stream = client.getstream();             string s = "hello client";             byte[] message = encoding.ascii.getbytes(s);             stream.write(message, 0, message.length);             updateui("message send");             stream.close();             client.close();         } 

any , appreciated. pretty new coding , new c# sorry maybe unclear.

here entire code server:

namespace tcpservertutorial {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void form1_load(object sender, eventargs e)         {          }          private void bstartserver_click(object sender, eventargs e)         {             thread tcpserverrunthread = new thread(new threadstart(tcpserverrun));             tcpserverrunthread.start();             tcpserverrun();         }          private void tcpserverrun()         {             tcplistener tcplistener = new tcplistener(ipaddress.any, 5004);             tcplistener.start();             updateui("listening");             tcpclient client = tcplistener.accepttcpclient();             while (true)             {                 tcpclient client = tcplistener.accepttcpclient();                 updateui("connected");                 thread tcphandlerthread = new thread (new parameterizedthreadstart(tcphandler));                 tcphandlerthread.start(client);             }           }          private void tcphandler(object client)         {             tcpclient mclient = (tcpclient)client;             networkstream stream = mclient.getstream();             byte[] message = new byte[1024];             stream.read(message, 0, message.length);             updateui("new message = " + encoding.ascii.getstring(message));             stream.close();             mclient.close();         }          private void updateui(string s)         {             func<int> del = delegate()             {                 textbox1.appendtext(s + system.environment.newline);                 return 0;             };             invoke(del);         }     } } 

bingo.look @ chunk of code:

    private void bstartserver_click(object sender, eventargs e)     {         // called once when thread starts         thread tcpserverrunthread = new thread(new threadstart(tcpserverrun));         tcpserverrunthread.start();         // called again here         tcpserverrun();     } 

the tcpserverrun() method being called twice: once when thread starts, , again via explicit call right after start thread (or possibly in reverse order). that's call socketexception occurs, because server has started. remove second reference , problem should resolved; similar situation on client.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -