Network Programming-TCP Program instance (upload file)

Source: Internet
Author: User

 PackagenetTest;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;Importjava.net.InetAddress;ImportJava.net.Socket;Importjava.net.UnknownHostException;//end tag for sending data ... To customize the end tag, first tell the server its own closing tag. //Timestamp Socket method ...  Public classtextclient { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubSocket s =NewSocket (Inetaddress.getlocalhost (), 7899); BufferedReader BUFR=NewBufferedReader (NewFileReader ("Tcpclient.java")); PrintWriter out=NewPrintWriter (S.getoutputstream (),true); LongTime =System.currenttimemillis ();        Out.println (time); String Line=NULL;  while(line = Bufr.readline ())! =NULL){            //System.out.println (line);out.println (line); }        //close the client output streamS.shutdownoutput ();//closes the output stream, which is equivalent to adding an end tag-1//Custom end tag out.println ("over");BufferedReader Bufin =NewBufferedReader (NewInputStreamReader (S.getinputstream ())); String Str=Bufin.readline ();        System.out.println (str);        Bufr.close ();    S.close (); }} PackagenetTest;ImportJava.io.BufferedReader;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTextserver { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubServerSocket SS =NewServerSocket (7899); Socket s=ss.accept (); System.out.println (S.getinetaddress (). Getlocalhost ()+ "..... connected"); BufferedReader Bufin=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); PrintWriter out=NewPrintWriter (NewFileWriter ("Server.java"),true); String Line=NULL;  while(line = Bufin.readline ())! =NULL){            //System.out.println (line);            if("Over". Equals (line)) {                //The custom end tag.                  Break;        } out.println (line); } printwriter PW=NewPrintWriter (S.getoutputstream (),true); Pw.println ("Upload success!!! ");        Out.close ();        S.close ();    Ss.close (); }}

Network Programming-TCP Program instance (upload file)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.