javascript - isConnected failed: ECONNREFUSED (Connection refused) -
i try transfer data between phones via wifi-direct , code works fine everyfirst time run through it. however, after close sockets in both client , server , try connect socket again, gives me exception. also, after lock screen of server phone , unlock it, can built connection once again. can familar socket give me advice? shouldn't wrong ip address/port right?
code client phone
protected void onhandleintent(intent intent) { context context = getapplicationcontext(); if (intent.getaction().equals(action_send_file)) { if(!socket.isconnected()) { string host = intent.getextras().getstring( extras_group_owner_address); int port = intent.getextras().getint(extras_group_owner_port); try { socket.bind(null); inetsocketaddress myaddress = new inetsocketaddress(host, port); socket.connect(myaddress, socket_timeout); } catch(ioexception e) { log.e(tag, e.getmessage()); } } try{ /*returns output stream write data socket*/ outputstream stream = socket.getoutputstream(); string count = string.valueof(_count); stream.write(count.getbytes()); } catch (ioexception e) { log.e(tag, e.getmessage()); } { if (socket != null) { if (socket.isconnected()) { try { inputstream = socket.getinputstream(); socket.shutdownoutput(); // sends 'fin' on network while (is.read() >= 0) ; // "read()" returns '-1' when 'fin' reached socket.close(); } catch (ioexception e) { // give e.printstacktrace(); } } } } } }
code server phone
protected string doinbackground(void... params) { try { serversocket serversocket = new serversocket(8888); socket client = serversocket.accept(); inputstream inputstream = client.getinputstream(); bytearrayoutputstream baos = new bytearrayoutputstream(); int i; while ((i = inputstream.read()) != -1) { baos.write(i); } string str = baos.tostring(); client.shutdownoutput(); // sends 'fin' on network while (inputstream.read() >= 0) ; // "read()" returns '-1' when 'fin' reached client.close(); // can close socket serversocket.close(); return str; } catch (ioexception e) { return null; } }
Comments
Post a Comment