Wednesday, 15 June 2011

how can i send my message from android client to other android client using my socket java PC server -



how can i send my message from android client to other android client using my socket java PC server -

i have code sucessfuly send client message server.. can help pls ? i'm new in socket programming. want found server sends message of android client other android client... here code

client android :

package com.example.websocketclient; import java.io.bufferedwriter; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class chatroom extends activity { public static string msgtoserver = null; textview uname; edittext in; button snd; textview out; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_chatroom); uname = (textview) findviewbyid(r.id.textviewmynam); in = (edittext) findviewbyid(r.id.edittextinput); snd = (button) findviewbyid(r.id.buttonsend); out = (textview) findviewbyid(r.id.textviewoutput); bundle bundle = getintent().getextras(); string urname = bundle.getstring("myname"); uname.settext(urname); myclienttask clienttask = new myclienttask(); clienttask.execute(); } public class myclienttask extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { connect(); homecoming null; } } public void connect() { socket sock; int[] allports = {1111,1919,2020}; int portnum; for(int i=1;i<allports.length;i++) { seek { portnum = allports[i]; boolean socketno = middleware.available(portnum); string advertisement = "192.168.149.1"; if(socketno = true) { sock = new socket(ad,portnum); while(socketno) { sendmsg(sock); }//while // sock.close(); }//if }catch (unknownhostexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } } } public void sendmsg (final socket s) { snd.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { //boolean listening = true; string inmsg = ""+in.gettext().tostring(); string response = ""; msgtoserver = inmsg; //out.append("you:"+msgtoserver+"\n"); seek { dataoutputstream outtoserver = new dataoutputstream(s.getoutputstream()); datainputstream intoserver = new datainputstream(s.getinputstream()); if(outtoserver != null){ outtoserver.writebytes(msgtoserver+"\n"); } } grab (exception e) { } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.chatroom, menu); homecoming true; } }

here pc server side :

package server; import java.net.*; import java.io.*; import java.util.scanner; import java.net.serversocket; import java.net.socket; import java.sql.*; public class socketserver extends thread { final static int _portnumber = 1919; //arbitrary port number public static void main(string[] args) { seek { new socketserver().startserver(); } grab (exception e) { system.out.println("i/o failure: " + e.getmessage()); } } public void startserver() throws exception { serversocket serversocket = null; boolean listening = true; seek { serversocket = new serversocket(_portnumber); } grab (ioexception e) { system.err.println("could not hear on port: " + _portnumber); system.exit(-1); } while (listening) { system.out.println("huwat huwat kang client..!"); handleclientrequest(serversocket); } serversocket.close(); } private void handleclientrequest(serversocket serversocket) { seek { new thread(new connectionrequesthandler(serversocket.accept())).start(); } grab (ioexception e) { e.printstacktrace(); } } /** * handles client connection requests. */ public class connectionrequesthandler implements runnable{ private socket _socket; private printwriter _out; private bufferedreader _in; public connectionrequesthandler(socket socket) { _socket = socket; } public void run() { string info; int maxid; system.out.println("client connected socket: " + _socket.tostring()); info = _socket.tostring(); //incrementing msg_id seek { bufferedwriter out = new bufferedwriter(new outputstreamwriter( _socket.getoutputstream())); _in = new bufferedreader(new inputstreamreader(_socket.getinputstream())); string inputline; class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost/finalclient","root",""); preparedstatement st = con.preparestatement("select *from msgs order msg_id desc"); resultset r1 = st.executequery(); r1.first(); // or r1.next() maxid = r1.getint("msg_id") + 1; while ((inputline = _in.readline()) != null) { system.out.println("message client: "+inputline); out.write(inputline); seek { preparedstatement st1=con.preparestatement("insert msgs values(?,?,?)"); st1.setint(1,maxid); st1.setstring(2,info); st1.setstring(3,inputline); int count = st1.executeupdate (); system.out.println (count + " rows inserted database"); } grab (exception e) { system.out.println("failed insert db"+e); } } } grab (exception e) { system.out.println("failed select db"+e); } } } }

java android sockets

No comments:

Post a Comment