Sunday, 15 February 2015

java - Android 2-Way TCP/IP ServerSocket/Socket Communication -



java - Android 2-Way TCP/IP ServerSocket/Socket Communication -

using great tutorial java code geeks, able create client activity sends info via tcp server's port 4000 using next code:

public class client extends activity { private socket socket; private static final int serverport = 5000; private static final string server_ip = "10.0.2.2"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); new thread(new clientthread()).start(); } public void onclick(view view) { seek { edittext et = (edittext) findviewbyid(r.id.edittext01); string str = et.gettext().tostring(); printwriter out = new printwriter(new bufferedwriter( new outputstreamwriter(socket.getoutputstream())), true); out.println(str); } grab (unknownhostexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (exception e) { e.printstacktrace(); } } class clientthread implements runnable { @override public void run() { seek { inetaddress serveraddr = inetaddress.getbyname(server_ip); socket = new socket(serveraddr, serverport); } grab (unknownhostexception e1) { e1.printstacktrace(); } grab (ioexception e1) { e1.printstacktrace(); } } } }

then using other snippet server activity can grab messages using tcp on port:

public class server extends activity { private serversocket serversocket; handler updateconversationhandler; thread serverthread = null; private textview text; public static final int serverport = 6000; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); text = (textview) findviewbyid(r.id.text2); updateconversationhandler = new handler(); this.serverthread = new thread(new serverthread()); this.serverthread.start(); } @override protected void onstop() { super.onstop(); seek { serversocket.close(); } grab (ioexception e) { e.printstacktrace(); } } class serverthread implements runnable { public void run() { socket socket = null; seek { serversocket = new serversocket(serverport); } grab (ioexception e) { e.printstacktrace(); } while (!thread.currentthread().isinterrupted()) { seek { socket = serversocket.accept(); communicationthread commthread = new communicationthread(socket); new thread(commthread).start(); } grab (ioexception e) { e.printstacktrace(); } } } } class communicationthread implements runnable { private socket clientsocket; private bufferedreader input; public communicationthread(socket clientsocket) { this.clientsocket = clientsocket; seek { this.input = new bufferedreader(new inputstreamreader(this.clientsocket.getinputstream())); } grab (ioexception e) { e.printstacktrace(); } } public void run() { while (!thread.currentthread().isinterrupted()) { seek { string read = input.readline(); updateconversationhandler.post(new updateuithread(read)); } grab (ioexception e) { e.printstacktrace(); } } } } class updateuithread implements runnable { private string msg; public updateuithread(string str) { this.msg = str; } @override public void run() { text.settext(text.gettext().tostring()+"client says: "+ msg + "\n"); } } }

my question how can create these 2 can communicate , forth?

android -> server(4000) -> android(4001)?

in other words how can create app help device deed both client (sending out info device on port 4000) , server (listening info on port 4001) @ same time?

java android sockets tcp

No comments:

Post a Comment