android- single server/ multiple clients? -
i'm working on programming live support chatting here, problem faced how server made receiving 1 client chat with.
can pro explain me make server receive more 1 client @ same time ?
and there problem : if close chat activity come make new chat through application, server couldn't response it.
any suggestion please ...
server side:
public class tcpserver extends thread { public static final int serverport = 7777; private boolean running = false; private printwriter mout; private onmessagereceived messagelistener; public static void main(string[] args) { //opens window messages received , sent serverboard frame = new serverboard(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); } /** * constructor of class * @param messagelistener listens messages */ public tcpserver(onmessagereceived messagelistener) { this.messagelistener = messagelistener; } /** * method send messages server client * @param message message sent server */ public void sendmessage(string message){ if (mout != null && !mout.checkerror()) { mout.println(message); mout.flush(); } } @override public void run() { super.run(); running = true; try { system.out.println("s: connecting..."); //create server socket. server socket waits requests come in on network. serversocket serversocket = new serversocket(serverport); //create client socket... method accept() listens connection made socket , accepts it. socket client = serversocket.accept(); system.out.println("s: receiving..."); try { //sends message client mout = new printwriter(new bufferedwriter(new outputstreamwriter(client.getoutputstream())), true); //read message received client bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream())); //in while wait receive messages client (it's infinite loop) //this while it's listener messages while (running) { string message = in.readline(); if (message != null && messagelistener != null) { //call method messagereceived serverboard class messagelistener.messagereceived("student: "+message); } } } catch (exception e) { system.out.println("s: error"); e.printstacktrace(); } { client.close(); system.out.println("s: done."); } serversocket.close(); } catch (exception e) { system.out.println("s: error"); e.printstacktrace(); } } //declare interface. method messagereceived(string message) must implemented in serverboard //class @ on startserver button click public interface onmessagereceived { public void messagereceived(string message); } }
for client separate them 2 activities :
public class tcpmainactivity extends activity { private listview mlist; private arraylist<string> arraylist; private mycustomadapter madapter; private tcpclient mtcpclient; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.tcpmain); arraylist = new arraylist<string>(); final edittext edittext = (edittext) findviewbyid(r.id.edittext); button send = (button)findviewbyid(r.id.send_button); //relate listview java 1 created in xml mlist = (listview)findviewbyid(r.id.list); madapter = new mycustomadapter(this, arraylist); mlist.setadapter(madapter); // connect server new connecttask().execute(""); send.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string message = edittext.gettext().tostring(); //add text in arraylist arraylist.add("student: " + message); //sends message server if (mtcpclient != null) { mtcpclient.sendmessage(message); } //refresh list madapter.notifydatasetchanged(); edittext.settext(""); } }); } public class connecttask extends asynctask<string,string,tcpclient> { @override protected tcpclient doinbackground(string... message) { //we create tcpclient object , mtcpclient = new tcpclient(new tcpclient.onmessagereceived() { @override //here messagereceived method implemented public void messagereceived(string message) { //this method calls onprogressupdate publishprogress(message); } }); mtcpclient.run(); return null; } @override protected void onprogressupdate(string... values) { super.onprogressupdate(values); //in arraylist add messaged received server arraylist.add("managment: "+values[0]); // notify adapter data set has changed. means new message received // server added list madapter.notifydatasetchanged(); } protected void onpostexecute(string file_url) { // dismiss dialog after getting courses } } protected void onstop() { // todo auto-generated method stub //mtcpclient.stopclient(); log.e("tcp client", "stooped"); super.onstop(); } protected void onstart() { // todo auto-generated method stub super.onstart(); } protected void onresume() { // todo auto-generated method stub super.onresume(); //new connecttask().execute(""); } /* public void ondestroy() { // todo auto-generated method stub super.ondestroy(); } */ }
the second 1 :
public class tcpclient { private string servermessage; public static final string serverip = "192.168.0.102"; //your computer ip address public static final int serverport = 7777; private onmessagereceived mmessagelistener = null; private boolean mrun = false; printwriter out; bufferedreader in; socket socket; /** * constructor of class. onmessagedreceived listens messages received server */ public tcpclient(onmessagereceived listener) { mmessagelistener = listener; } /** * sends message entered client server * @param message text entered client */ public void sendmessage(string message){ if (out != null && !out.checkerror()) { out.println(message); out.flush(); } } public void stopclient(){ mrun = false; try { socket.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void run() { mrun = true; try { if (socket != null) socket.close(); //here must put computer's ip address. inetaddress serveraddr = inetaddress.getbyname(serverip); log.e("tcp client", "c: connecting..."); //create socket make connection server socket = new socket(serveraddr, serverport); try { //send message server out = new printwriter(new bufferedwriter(new outputstreamwriter(socket.getoutputstream())), true); log.e("tcp client", "c: sent."); log.e("tcp client", "c: done."); //receive message server sends in = new bufferedreader(new inputstreamreader(socket.getinputstream())); //in while client listens messages sent server while (mrun) { servermessage = in.readline(); if (servermessage != null && mmessagelistener != null) { //call method messagereceived myactivity class mmessagelistener.messagereceived(servermessage); } servermessage = null; } log.e("response server", "s: received message: '" + servermessage + "'"); } catch (exception e) { log.e("tcp", "s: error", e); } { //the socket must closed. not possible reconnect socket // after closed, means new socket instance has created. socket.close(); } } catch (exception e) { log.e("tcp", "c: error", e); } } //declare interface. method messagereceived(string message) must implemented in myactivity //class @ on asyncktask doinbackground public interface onmessagereceived { public void messagereceived(string message); } }
thanking in advance.
Comments
Post a Comment