java - Connection between server and android -
i'm trying develop app instant messaging (like whatsapp). i'm confused type of connection should use: socket, http or other? have use non-standard java api? server use java , client use andoid (java). have tried socket connection without success. don't error nothing happens because device , server not connected(i don't know why).
this client class (android) sends messages:
public class sendmsg implements runnable { private socket socket; string msg; public sendmsg(string msg){ this.msg=msg; } @override public void run() { try { socket = new socket("ip_globale_server",5000); socket.setsotimeout(5000); bufferedwriter writer= new bufferedwriter(new outputstreamwriter(socket.getoutputstream())); writer.write(msg); writer.flush(); } catch (ioexception e){ system.err.println("connection error"); } finally{ if (socket!=null) { try{socket.close();} catch(ioexception e){} } } } }
this java server class waits connection:
public class receivemsg implements runnable { serversocket server= null; socket connection=null; inputstream in; inputstreamreader inr; stringbuilder smsg; public void run() { system.out.print("server has started"); try { server = new serversocket(5000); system.out.println("i'm waiting connection"); connection = server.accept(); system.out.println("i'm connected "+ connection.tostring()); in = connection.getinputstream(); inr = new inputstreamreader(in,"ascii"); (int c= inr.read();c!=-1; c= inr.read()){ smsg.append((char)c); } //this class storing message new store (smsg.tostring(),1,2); } catch(ioexception ex){} { try{ if (server != null) server.close(); if (server != null) server.close(); } catch(ioexception e){ } }
could me start please. thank in advance.
i suggest using google cloud messaging
here. has benefit once device runs app, can make register against gcm
, can send them messages needed, way don't have struggle sockets close after amount of time.
this approach need have implement server (for instance, web server php) , make users communicate it, flow be:
your server sends broadcast message registered devices telling them have register against
gcm
service.the devices message, , implement
broadcastreceiver
both latitude , longitude , send remote server,http://www.mysuperserver.com/getmsg.php
, viapost
request.your server processes parameters , save them or whatever need.
if want follow approach, steps have follow:
go https://console.developers.google.com. google account, register new project. provide api key , sender id.
you'll need make device register against project in
gcm
. can achieve importinggoogle play services
within project, , once done, alike this:googlecloudmessaging gcm = googlecloudmessaging.getinstance(context); final string regid = gcm.register("your_sender_id");
at time,
gcm
server knows user has registered whithin project , has given himregid
.the next step informing own remote server user has done so, , save somewhere
regid
has been given, can later send them messages. in client side, makehttp post
request against server sendingregid
, process somethat this:$regid = $_post['regid']; if (($regid) && ($ipaddr)) sql_query("insert gcm_subscribers(regid) values($regid')");
once done, know have registered against database. can
select
users , send them new messages (simulating multicast behavior).function sendnotification($registrationidsarray, $messagedata) { $apikey = "your_api_key"; $headers = array("content-type:" . "application/json", "authorization:" . "key=" . $apikey); $data = array( 'data' => $messagedata, 'registration_ids' => $registrationidsarray ); $ch = curl_init(); curl_setopt( $ch, curlopt_httpheader, $headers ); curl_setopt( $ch, curlopt_url, "https://android.googleapis.com/gcm/send" ); curl_setopt( $ch, curlopt_ssl_verifyhost, 0 ); curl_setopt( $ch, curlopt_ssl_verifypeer, 0 ); curl_setopt( $ch, curlopt_returntransfer, true ); curl_setopt( $ch, curlopt_postfields, json_encode($data) ); $response = curl_exec($ch); curl_close($ch); return $response; }
once in client, process
gcm
message , display accordingly. process messages i'm including few links below.
more this:
Comments
Post a Comment