c# - Exception in opening a socket at my IP address -


i new in socket programming c#. trying make chat server between 2 computers unable because cant start socket..... gave server program ip address gives me exception... "the requested address not valid in context" ...here code:

        ipaddress hostipaddress = ipaddress.parse("178.189.27.85");         tcplistener serversocket = new tcplistener(hostipaddress, 8888);         int requestcount = 0;         tcpclient clientsocket = default(tcpclient);         serversocket.start();         console.writeline(" >> server started");         clientsocket = serversocket.accepttcpclient();         console.writeline(" >> accept connection client");         requestcount = 0;          while ((true))         {             try             {                 requestcount = requestcount + 1;                 networkstream networkstream = clientsocket.getstream();                 byte[] bytesfrom = new byte[10025];                 networkstream.read(bytesfrom, 0, (int)clientsocket.receivebuffersize);                 string datafromclient = system.text.encoding.ascii.getstring(bytesfrom);                 datafromclient = datafromclient.substring(0, datafromclient.indexof("$"));                 console.writeline(" >> data client - " + datafromclient);                 string serverresponse = "last message client" + datafromclient;                 byte[] sendbytes = encoding.ascii.getbytes(serverresponse);                 networkstream.write(sendbytes, 0, sendbytes.length);                 networkstream.flush();                 console.writeline(" >> " + serverresponse);             }             catch (exception ex)             {                 console.writeline(ex.tostring());             }         }          clientsocket.close();         serversocket.stop();         console.writeline(" >> exit");         console.readline(); 

client program

try {         tcpclient tcpclnt = new tcpclient();         console.writeline("connecting.....");          tcpclnt.connect("192.168.128.1",8888);         // use ipaddress in server program          console.writeline("connected");         console.write("enter string transmitted : ");          string str=console.readline();         stream stm = tcpclnt.getstream();          asciiencoding asen= new asciiencoding();         byte[] ba=asen.getbytes(str);         console.writeline("transmitting.....");          stm.write(ba,0,ba.length);          byte[] bb=new byte[100];         int k=stm.read(bb,0,100);          (int i=0;i<k;i++)             console.write(convert.tochar(bb[i]));          tcpclnt.close();     }      catch (exception e) {         console.writeline("error..... " + e.stacktrace);     } 

ipaddress hostipaddress = ipaddress.parse("178.189.27.85"); 

is computer on public internet? expect computer have private ip (eg 192.168.x.y, 172.16-32.x.y, 10.x.y.z) unless directly connected internet connection.

assuming you're on windows, use command prompt , run ipconfig, or visit control panel , network settings. on windows 7 instance it's in network , sharing center, under "change adapter settings"; active adapter, right click , choose "status".

once find local ip address, use that. or can use 0.0.0.0 wildcard address – in effect saying "just accept connections wherever, don't care ip address is."


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -