Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666
Program functions:
Server: PC
Language C #
Button to enable serversocket
Private void btnstartserver_click (Object sender, eventargs e) {string strip = tbip. text; // manually enter the IP address string strport = tbport. text; // port number IPaddress IP; int port; try {If (strip! = "" & Strport! = "") {IP = IPaddress. parse (strip); Port = convert. toint32 (strport); this. lip. TEXT = IP. tostring (); this. lport. TEXT = strport;} else // If no information is specified, the local address and port number 1234 are used by default. {IP = getip (); // obtain the local IP address port = 1234; this. lip. TEXT = IP. tostring (); this. lport. TEXT = port. tostring ();} If (IP! = NULL) serverfulladdr = new ipendpoint (IP, Port); else {MessageBox. Show ("the correct IP address is not obtained !! "); Return;} Server = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); server. BIND (serverfulladdr); lbserverstate. TEXT = "the server has been started !! ";} Catch (exception El) {MessageBox. show (El. message); return;} // enable the listener thread with parameters, and pass the server socket to the thread parameterizedthreadstart parstart = new parameterizedthreadstart (connectlisten); thread thlisten = new thread (parstart ); threadlist. add (thlisten); thlisten. start (object) server );}
Obtain the local IP address.
Public IPaddress getip () {// string straddr = ""; IPaddress IP = NULL; try {string strhostname = system. net. DNS. gethostname (); system. net. iphostentry ipentry = system. net. DNS. gethostentry (strhostname); foreach (IPaddress _ IPaddress in ipentry. addresslist) {If (_ IPaddress. addressfamily. tostring (). tolower () = "Internetwork") {// straddr = _ IPaddress. tostring (); IP = _ IPaddress; break ;}// log. de Bug ("IP:" + straddr);} catch (system. exception e) {// log. error ("Get IP address error:" + E. message); MessageBox. show ("no IP address of the host is obtained !!! ");} Return IP ;}
Listening to client connection threads
Private void connectlisten (Object Server) {socket serversocket = (socket) server; while (true) // loop answer connection {serversocket. listen (20); socket acceptsock = serversocket. accept (); byte [] bytearray = new byte [50]; acceptsock. receive (bytearray); // the user name sent from the receiving client is 50 bytes by default. The processed string is obtained. // convert the byte array to string strrec = system. text. encoding. default. getstring (bytearray); // C # byte [] is converted to string socketname = strrec. substring (0, strrec. indexof ("\ n"); IPaddress clientip = (system. net. ipendpoint) acceptsock. remoteendpoint ). address; obtain the Client IP address string strip = clientip. tostring (); socketnamelist. add (socketname); socketiplist. add (strip); updatepersonlistbox (); // update the clientsocketlist in the user list. add (acceptsock); parameterizedthreadstart parstart = new parameterizedthreadstart (recvfile); thread (Th = new thread (parstart) for each client to add the receiving information listener; threadlist. add (th); th. start (object) acceptsock );}}
Receiving File thread
Private void recvfile (Object acceptsock) {bool flag = false; socket recvsocket = (socket) acceptsock; byte [] bytearry = new byte [256]; string Path = "recvvoice "; // create the receiving file folder if (! Directory. exists (PATH) {directory. createdirectory (PATH);} string strnow = datetime. now. tostring ("yyyymmdd-hh-mm-SS"); // receives the file string strpath = "recvvoice \" + strnow + "according to the system time ". wav "; byte [] bufflen = new byte [50]; int COUNT = recvsocket. receive (bufflen); // The length of the received file in bytes. It is used to determine that the receiving file ends. By default, 50 bytes are sent. String strrec = system. text. encoding. default. getstring (bufflen); string FLEN = strrec. substring (0, strrec. indexof ("\ n"); int filelen = 0; try {filelen = convert. toint32 (FLEN);} catch (exception El) {MessageBox. show (El. message);} int reccount = 0; int readlen = 0; memorystream memstream = new memorystream (); try {While (reccount = recvsocket. receive (bytearry)> 0) // cyclically receives the file byte sending {readlen = readlen + reccount; If (readlen> = filelen) // jumps out of the loop sign {memstream. write (bytearry, 0, reccount); flag = true; break;} memstream. write (bytearry, 0, reccount); // write the network stream into the file stream flag = true; // receives the file flag} catch (exception El) {flag = false; memstream. close (); // FS. close (); memstream = NULL; // FS = NULL; MessageBox. show (El. message); recvfile (acceptsock); // use recursion to continue listening to the file to receive the return;} If (FLAG) {filestream FS = new filestream (strpath, filemode. openorcreate); memstream. writeto (FS); FS. close (); FS = NULL; updatelabel (FLAG); IPaddress clientip = (system. net. ipendpoint) recvsocket. remoteendpoint ). address; string strip = clientip. tostring (); int Index = socketiplist. indexof (strip); addvoicetolistbox (socketnamelist [Index] + "*****" + socketiplist [Index], strpath, flag); // set the client user name address, save path of the received file // Add to sound list} flag = false; memstream. close (); memstream = NULL; recvfile (acceptsock); // use recursion to continue listening to files to receive emails}
Local sound sending Function
Private void sendfile (socket acceptsock, string vpath) {socket sendsocket = (socket) acceptsock; byte [] bytearray = new byte [100]; stream ST = file. openread (vpath); byte [] B = new byte [1024]; string strlen = ST. length. tostring (); byte [] bytes = system. text. encoding. default. getbytes (strlen + "\ r \ n"); // String Conversion byte [] sendsocket. send (bytes); // determines the length of the file to be sent. Int COUNT = 0; int sent = 0; int offset = 0; int left = 0; thread. sleep (1000); // The key. While (offset <St. length) {// buffer. copyto (B, 1024); Count = ST. read (B, 0, 1024); offset = offset + count; left = convert. toint32 (St. length)-offset; If (left <= 1024) {byte [] temp = new byte [left]; sent = sendsocket. send (B); // thread. sleep (10); Count = ST. read (B, 0, left); sent = sendsocket. send (temp); break;} sent = sendsocket. send (B);} IPaddress clientip = (system. net. ipendpoint) sendsocket. remoteendpoint ). address; string strip = clientip. tostring (); int Index = socketiplist. indexof (strip); addvoicetolistbox ("server send to *****" + socketnamelist [Index], vpath, true );}
Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666