usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceudp{classProgram {Static voidMain (string[] args) { //server port using TCP to transmit filesTcpListener Listen=NewTcpListener (6666); Listen. Start (); TcpClient Client=listen. AcceptTcpClient (); NetworkStream NS=client. GetStream (); StreamWriter SW=NewStreamWriter (NS); BufferedStream BS=NewBufferedStream (NS); BOOLJudge =client. Connected; if(judge) {Console.foregroundcolor=Consolecolor.yellow; Console.WriteLine ("Client Link succeeded"+"\ n"+"Start transferring files Please wait ..."); } while(client!=NULL) { stringfilepath =@"e:\ practice \ Video Practice \consoleapplication10\consoleapplication10\bin\debug\consoleapplication10.exe"; stringext =path.getextension (filepath); FileStream FS=File.openread (filepath); Sw. WriteLine (fs. Length.tostring ()+ext); Sw. Flush (); byte[] B =New byte[1024x768]; intLength =b.length; while(length = fs. Read (b,0, length)) >0) {Bs. Write (b,0, length); Bs. Flush (); } Console.WriteLine ("byte stream has been sent"); Bs. Close (); Break; } //transmitting files using UDP//IPEndPoint Myip = new IPEndPoint (Ipaddress.parse ("192.168.50.244"), 5000); //UdpClient cs = new UdpClient (MYIP); //FileStream FR = File.openread (@ "e:\c#. txt"); //byte[] b = new BYTE[FR. Length]; //IPEndPoint Aimpoint = new IPEndPoint (Ipaddress.parse ("192.168.50.196"), 5000); //Fr. Read (b, 0, b.length); //cs. Send (b, B.length, Aimpoint); //Console.WriteLine ("Send Complete"); } }}
Client
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net.Sockets;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) { //transfer files using TCP protocol (client side)TcpClient TC =NewTcpClient ("192.168.50.244",6666);//send a request to the serverNetworkStream NS = TC. GetStream ();//get stream after successful requestBufferedStream SR=NewBufferedStream (NS);//turn on buffered streamsStreamReader sread =NewStreamReader (NS);//Open the Read server stream file stringstr = sread. ReadLine ();//read the file size sent by the serviceConsole.WriteLine (str);//Print this file size//string ext =str. Substring (str. LENGTH-4); string[] STRs = str. Split (New string[] {"."},stringsplitoptions.removeemptyentries);//split file size and extension stringFilelength = strs[0];//File Size stringext = strs[1];//Extended NameFileStream FS= File.openwrite (@"d:\ Small software 5."+ext);//the address of the new file LongA =Long. Parse (filelength);//converts a file size in string format to a long type for judging stringQQQ = A.tostring ();//gets the length of the server-side send string intindex =0; byte[] B =New byte[1024x768];//create an array that accepts buffer byte streams intLength =b.length; while(Index <= A/1024x768+ QQQ. Length-1)//Determine the length of the pass (the length of the entire file equals the number of times each incoming plus the length of the string that begins to read more) { if(Index >= QQQ. Length-1)//when the length is greater than the string length is the start of reading (the purpose is to not let the byte stream of the string interfere with the entire file's byte stream){Sr. Read (b,0, length);//read byte stream of B array onceFS. Write (b,0, length);//write a byte stream of B array to the address of the file} index++; } Console.foregroundcolor=consolecolor.red; Console.WriteLine (" Over"); //transmitting files using UDP protocol//IPEndPoint udppoint = new IPEndPoint (Ipaddress.parse ("192.168.50.196"), 5000); //udpclient UDP = new UdpClient (udppoint); //IPEndPoint Sendip = new IPEndPoint (ipaddress.any, 0); //Console.WriteLine (11); //string str = Encoding.Default.GetString (recvdata); //FileStream fs = File.openwrite (@ "d:\ picture. txt"); //Console.WriteLine ("[Email protected]#$%^&* ("); //byte[] RecvData = UDP. Receive (ref Sendip); //Console.WriteLine ("1234567890-"); //FS. Write (recvdata, 0, recvdata.length); //FS. Close (); //Console.WriteLine ("Read and write completed"); } }}
C # uses TCP to send scripts for various files listener&&client