.net - C# issue sending string to a different socket on the same computer -
i have program , sending string different socket on same computer every 45 seconds. below function send string on socket using built in socket class of c#.
issue having when run program , give 127.0.0.1 not getting responses socket. when run program on different computer using ipaddress of computer works flawlessly.
so questioned if allowed pass in 127.0.0.1 ip socket class , have recognize want send data different socket on same computer. have different make sure works on 127.0.0.1?
thanks!
static string queryminer(string command) { byte[] bytes = new byte[1024]; try { //code gettting current machines ip use code if client running on miner ipaddress ipaddr = ipaddress.parse("127.0.0.1"); //ipaddress ipaddr = ipaddress.parse("198.xxx.xxx.xxx"); ipendpoint ipendpoint = new ipendpoint(ipaddr, 4028); socket sender = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); sender.connect(ipendpoint); console.writeline("socket connected {0}", sender.remoteendpoint.tostring()); string summarymessage = command; byte[] msg = encoding.ascii.getbytes(summarymessage); sender.send(msg); byte[] buffer = new byte[1024]; int lengthofreturnedbuffer = sender.receive(buffer); char[] chars = new char[lengthofreturnedbuffer]; decoder d = system.text.encoding.utf8.getdecoder(); int charlen = d.getchars(buffer, 0, lengthofreturnedbuffer, chars, 0); string summaryjson = new string(chars); sender.shutdown(socketshutdown.both); sender.close(); return summaryjson; } catch (exception ex) { console.writeline("exception: {0}", ex.tostring()); return ex.tostring(); } }
the code set server portion of (e.g. call bind) isn't here, problem certianly binding specific ip address , not ipaddress.any
.
if using local ip rather loopback should work.
Comments
Post a Comment