i'm trying connect phone notebook using wifi. both devices connected 1 network. notebook has ip 192.168.1.35 tried connect ip through smart phone (firewall off). here's code:
package org.me.androidapp; import android.app.activity; import android.os.bundle; import android.widget.toast; import java.io.bufferedwriter; import java.io.ioexception; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; import java.util.logging.level; import java.util.logging.logger; public class mainactivity extends activity { @override public void oncreate(bundle icicle) { super.oncreate(icicle); try { toast.maketext(this.getapplicationcontext(), "ok", toast.length_long).show(); socket socket = new socket("192.168.1.35", 9999); printwriter out = new printwriter( new bufferedwriter( new outputstreamwriter(socket.getoutputstream())),true); out.println("dupa"); socket.close(); } catch (unknownhostexception ex) { toast.maketext(this.getapplicationcontext(), "nie odnaleziono hosta", toast.length_long).show(); logger.getlogger(mainactivity.class.getname()).log(level.severe, null, ex); } catch (ioexception ex) { toast.maketext(this.getapplicationcontext(), ex.tostring(), toast.length_long).show(); logger.getlogger(mainactivity.class.getname()).log(level.severe, null, ex); } } }
im using code (written in python) server:
import socket _connector = none _running = true _host = 'localhost' _port = '9999' _maxclient = 999 debug = true _policyfile = '<?xml version="1.0" encoding="utf-8"?><cross-domain-policy xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="http://www.adobe.com/xml/schemas/policyfilesocket.xsd"><allow-access-from domain="*" to-ports="*" secure="false" /><site-control permitted-cross-domain-policies="master-only" /></cross-domain-policy>' ## trace debugging messages. # @param astring string printed. def printd( astring ): if debug: print astring _connector = socket.socket( socket.af_inet, socket.sock_stream ) _connector.bind ( ( str(_host), int(_port) ) ) _connector.listen ( int(_maxclient) ) _running = true while _running: printd('running on port ' + _port + ' ... ') channel, details = _connector.accept() if _running: printd( 'new connection with: ' + str(details) ) channel.setblocking( 1 ) recvdata = channel.recv(2000) if("policy-file-request" in recvdata): channel.send(_policyfile) printd( 'host: ' + str(details[0]) ) printd( 'port: ' + str(details[1]) ) printd( 'port: ' + str(int(details[1])) ) channel.close()
im testing app on phone because im not sure how configure emulator (any help? :) ). phone shows error "java.net.connectionerror localhost/192.168.1.1:9999 - connection refused". im wondering why shows ip 192.168.1.1...
please help, chris
your python code binding localhost means it's not listening on network on loopback should change have listen on network address (i.e. 192.168.1.35) of computer rather on 127.0.0.1
Comments
Post a Comment