send data from socket to c# in android -
i trying develop application in android sent gps data pc .the android part :
@override public void onlocationchanged(location location) { // todo auto-generated method stub float latitude = (float) (location.getlatitude()); float longitude = (float) (location.getlongitude()); showmessage("student details", "latitude: " + latitude + ", longitude: " + longitude); log.i("geo_location", "latitude: " + latitude + ", longitude: " + longitude); try { socket socket = new socket("192.168.1.5",5000); dataoutputstream dos = new dataoutputstream(socket.getoutputstream()); dos.writeutf("hello_world"); socket.close(); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
and c# code or server code :
public asynccallback pfnworkercallback; private socket m_mainsocket; private socket[] m_workersocket = new socket[25]; private int m_clientcount = 0; private string ipaddress; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { startfun(); } public void startfun() { try { // drawmappersian(); ipaddress = "192.168.1.5"; // check port value string portstr = "5000"; int port = system.convert.toint32(portstr); // create listening socket... m_mainsocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipendpoint iplocal = new ipendpoint(ipaddress.any, port); // bind local ip address... m_mainsocket.bind(iplocal); listbox1.items.add("server started..."); // start listening... m_mainsocket.listen(20); listbox1.items.add("server listening ..."); // create call client connections... m_mainsocket.beginaccept(new asynccallback(onclientconnect), null); } catch (exception qqq) { using (streamwriter writer = new streamwriter(@"e:\a.txt")) { writer.write(qqq.message); } } } public void ondatareceived(iasyncresult asyn) { try { socketpacket socketdata = (socketpacket)asyn.asyncstate; int irx = 0; // complete beginreceive() asynchronous call endreceive() method // return number of characters written stream // client irx = socketdata.m_currentsocket.endreceive(asyn); string res = getparameters(socketdata.databuffer); console.writeline(res.tostring()); }
another thing should add permissions manifest <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.internet" />
i connected mobile phone wireless network ,i mean mobile can access ip . problem gps data generated don't know why ?another problem android application doesn't send data server ,
client
to accelerate location update, have specify activity location update interval when setting locationlistener
implementation. :
locationlistener impl = new mylocationlistenerimpl(); long interval = 4000;//change code here setlocationlistener(impl, interval);
(note code above more pseudo code, since it's been long time since have touched locationlistener).
server
the code provided not mention "ondatareceived" member function. think should dig in way.
unless sockets used on purpose, consider using http protocol , frameworks asp.net , loopj async http pass data server, it's easier !
Comments
Post a Comment