c# - Can't make TCP client code work without USB cable attached -
c# - Can't make TCP client code work without USB cable attached -
i'm having bit of problem in getting simple tcp client working on htc titan w/ windows phone 7.5.
when usb cable attached phone, tcp client works charm, cable unplugged, client unable connect tcp server running on development machine. devices on same network , i'm using explicit ip-address of desktop machine connect, there's no name resolution going on afaik.
here's code use. of taken sockets samples on msdn (can't seem find link though).
private socket _sock = null; private manualresetevent _done = new manualresetevent(false); private const int timeout = 5000; //connect server public string connect(string ip, int port) { string result = string.empty; var host = new ipendpoint(ipaddress.parse(ip), port); _sock = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); _sock.setnetworkrequirement(networkselectioncharacteristics.noncellular); var args = new socketasynceventargs(); args.remoteendpoint = host; args.completed += new eventhandler((s,e) => { result = e.socketerror.tostring(); _done.set(); }); _done.reset(); _sock.connectasync(args); _done.waitone(timeout); homecoming result; } //send message public string send(string msg) { string response = "operation timeout"; if (_sock != null) { var args= new socketasynceventargs(); args.remoteendpoint = _sock.remoteendpoint; args.completed += new eventhandler(s, e) => { response = e.socketerror.tostring(); _done.set(); }); var payload = encoding.utf8.getbytes(data); args.setbuffer(payload, 0, payload.length); _done.reset(); _sock.sendasync(args); _done.waitone(timeout); } homecoming response; } //receive message public string receive() { string response = "operation timeout"; if (_sock != null) { var args= new socketasynceventargs(); args.remoteendpoint = _sock.remoteendpoint; args.setbuffer(new byte[max_bufsize], 0, max_bufsize); args.completed += new eventhandler((s,e) => { if (e.socketerror == socketerror.success) { response = encoding.utf8.getstring(e.buffer, e.offset, e.bytestransferred); response = response.trim('\0'); } else { response = e.socketerror.tostring(); } _done.set(); }); _done.reset(); _sock.receiveasync(args); _done.waitone(timeout); } homecoming response; }
the code used like:
connect(...); send(...); receive(...); //and close socket
as said before, code works charm when device attached development machine. when cable unplugged, connection phase times out (regardless of timeout interval should say).
also, manifest contains id_cap_networking capability understand should give app permission access network.
any ideas?
edit: discovered switching udp communication works charm. means problem reason, phone unable set persistant tcp connection dev machine. getting stranger minute.
do have wireless ap nearby on phone connected? because when plug in pc uses pc's network connection.
c# windows-phone-7
Comments
Post a Comment