Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666
Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666
This version is only a test version of function implementation, so the interface is very ugly. Please forgive me, mainly look at the function !!
Main functions:
Connect to the server and enable a thread that listens for incoming mail. If there is a incoming mail, enable the Receiving File thread;
Button controls the voice sending thread;
Use a third-party class to implement native uncompressed recording format. wav;
Use mediaplayer provided by Android to enable sound playback;
Step 1: connect to the server
Btnconnect. setonclicklistener (newbutton. onclicklistener ()
{
@ Override
Publicvoid onclick (view arg0 ){
// Todo auto-generated method stub
// Ip = etip. getcontext (). tostring ();
IP = etip. gettext (). tostring ();
Try {
Port = integer. parseint (etport. gettext (). tostring ());
} Catch (effectione)
{
Log. I (TAG, "ip" + IP + "Port" + port );
Toast. maketext (mainactivity. This, "enter the correct port number !! ", Toast. length_long). Show ();
Return;
}
Try {
Client = new socket (IP, Port); // connect to the server
} Catch (unknownhostexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
Toast. maketext (mainactivity. This, "unknownhostexception", Toast. length_long). Show ();
Return;
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
Toast. maketext (mainactivity. This, "ioexception! ", Toast. length_long). Show ();
Return;
}
Printwriterout;
Try {
Out = new printwriter (client. getoutputstream (), true );
Out. println ("android"); // send the client name to the server
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Tvstate. settext ("connection to the server successful !!! ");
Socketlistenslisten = new socketlisten (client, mhandler); // enable the listener receiving thread
Slisten. setcontrolbar (tvrecvcount );
Slisten. Start ();
}
});
Step 2: Listen to the receiving thread
Package er. fly; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import java.net. socket; import er. fly. nettandclient. myhandler; import android. util. log; import android. widget. textview; public class socketlisten extends thread {private Socket Client; // Private string savepath = "/sdcard/myvoice/"; myhandler mhandler; myhandler slhandler; textview TVR; Public socketl Isten (socket S, myhandler h) {This. client = s; mhandler = H;} public void setcontrolbar (textview TV) {TVR = TV;} public void run () {int Len; If (client = NULL) return; while (true) // listen to the incoming message {bufferedreader in; // = new bufferedreader (New inputstreamreader (system. in); // receives the response data from the server. Try {In = new bufferedreader (New inputstreamreader (client. getinputstream (); try {string strlen = in. readline (); // Read the network stream, file length // system. out. println ("file size:" + strlen); log. I ("socketlisten", strlen); Len = integer. parseint (strlen); // enable the downloadfile downfile = new downloadfile (client, Len); downfile. sethanglers (mhandler, slhandler); downfile. setcontrolbar (TVR); downfile. start (); // This. suspend (); While (true) // determines whether the file is received. // wait {If (! Downfile. isalive () break;} catch (ioexception e) {log. I ("socketlisten", "No Recv") ;}} catch (ioexception e) {// todo auto-generated Catch Block // E. printstacktrace ();}}}}
Receiving File Thread class:
Package er. fly; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. socket; import Java. util. date; import er. fly. nettandclient. myhandler; import android. OS. logoff; import android. OS. message; import android. util. log; import android. widget. textview; // import er. fly. nettandclie NT; public class downloadfile extends thread {private Socket Client; file = NULL; string sendinfo = NULL; int filelen; string tag = "downloadfile"; myhandler mhandler; // used to communicate with the main thread and update the audio list myhandler slhandler; textview tvrecvcount; string recvvoicesavepath; Public downloadfile (socket S, int L) {This. client = s; filelen = L;} public void sethanglers (myhandler M, myhandler SL) {mhandler = m; slhandler = SL;} public void setcontrolbar (textview TV) {tvrecvcount = TV;} // create a directory (if the directory does not exist) Public file createdir (string DIR) {file = new file (DIR); If (! File. exists () {file. mkdirs ();} return file;} public void run () {low.curlow.= loid. myloopers (); Loopers mainloopers = loopers. getmainlooper (); string MSG = NULL; Boolean flag = false; If (client = NULL) {return;} Try {inputstream = client. getinputstream (); byte [] buffer = new byte [1024]; int readcount = 0; int COUNT = 0; createdir ("sdcard/myrecvvoice /"); int H = new date (). gethou RS (); int M = new date (). getminutes (); int S = new date (). getseconds (); string strtime = string. valueof (h) + "-" + String. valueof (m) + "-" + String. valueof (s); bufferedoutputstream fo = new bufferedoutputstream (New fileoutputstream (new file ("sdcard/myrecvvoice/" + strtime + ". wav "); // The while (true) {readcount = inputstream. read (buffer); Count = count + readcount; FO. write (buffer, 0, readcount); // system. ou T. println ("Recv len" + String. valueof (count) + "readcount" + String. valueof (readcount); log. I (TAG, "Recv len" + String. valueof (count) + "readcount" + String. valueof (readcount); If (count> = filelen) // judge the receipt end break;} flag = true; recvvoicesavepath = "sdcard/myrecvvoice/" + strtime + ". wav "; log. I (TAG, "received successfully !! "); FO. Flush (); FO. Close (); // inputstream. Close ();// You must not close the input and output streams, because disabling them will also close the socket // This is very important. // Outputstream. close ();} catch (ioexception e) {e. printstacktrace ();} mhandler = new myhandler (mainloler); mhandler. sethandlerargs (tvrecvcount); If (FLAG) MSG = "downloadfile:" + recvvoicesavepath + "#" + "received successfully !! "; Else MSG =" downloadfile: "+" $ "+" failed to receive !! "; Mhandler. removemessages (0); message m = mhandler. obtainmessage (1, 1, 1, MSG); // send a message to the main thread, notifying the receiving end of mhandler. sendmessage (m );}}
File Sending Thread class:
Package er. fly; import Java. io. bufferedreader; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. printwriter; import java.net. socket; import java.net. socketexception; import android. util. log; public class upfile extends thread {private sock Et client; file = NULL; string tag = "upfile"; Public upfile (Socket Client) {This. client = client;} public void setfile (File file) {This. file = file;} public void run () {If (client = NULL) {return;} If (client. isoutputshutdown () {try {client. setkeepalive (true);} catch (socketexception e) {// todo auto-generated catch blocke. printstacktrace (); log. I (TAG, "client setkeepalive worry") ;}} fil Einputstream reader = NULL; fileinputstream readlen = NULL; byte [] Buf = NULL; try {inputstream input = client. getinputstream (); outputstream output = client. getoutputstream (); readlen = new fileinputstream (File); bytearrayoutputstream ow = new bytearrayoutputstream (); int COUNT = 0; int buffersize = 256; int filelen = 0; buf = new byte [buffersize]; while (COUNT = readlen. read (BUF)> 0) // read the object's byte length {Filelen = filelen + count;} string strlen = string. valueof (filelen); strlen = strlen + "\ n"; log. I (TAG, strlen); byte [] bufflen = strlen. getbytes (); byte [] buffl = new byte [50]; for (INT I = 0; I <50; I ++) // Add the file length to a 50-byte array // because the service also receives 50 bytes at a time {if (I <bufflen. length) {buffl [I] = bufflen [I];} else {buffl [I] = 0 ;}} output. write (buffl); // sends the object Length byte array output. flush (); // sends the network stream, otherwise the server cannot receive COUNT = 0; // read File byte, and send reader = new fileinputstream (File) cyclically; while (COUNT = reader. read (BUF)> 0) {ow. write (BUF, 0, count); log. I (TAG, "reading file");} log. I (TAG, "Start sending !! "); Output. Write (ow. tobytearray (); output. Flush (); log. I (TAG," the file has been sent !!! "); // Client. shutdownoutput (); // This sentence cannot be added; otherwise, the socket closes the sending stream and cannot be sent here in the future, this is also a question about how to use the file byte length to determine the reason for receiving the end} catch (exception e) {system. out. println ("socket execution exception:" + E. tostring ();} finally {try {// end object Buf = NULL; // out. close (); // out must not be closed, because colleagues will also close socket // out. close (); // In. close (); reader. close (); // client. close () ;}catch (exception e ){}}}}
Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666