Java TCP upload Text

Source: Internet
Author: User

Service side:

Package Cn.net.tcp;import Java.io.bufferedreader;import Java.io.filereader;import java.io.filewriter;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.net.ServerSocket; Import Java.net.socket;public class Uploadserver {/** * @param args * @throws ioexception */public static void Main (String [] args) throws IOException {System.out.println ("server start ..."); ServerSocket ss = new ServerSocket (10005); Socket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress (); SYSTEM.OUT.PRINTLN (IP + "... connected"); BufferedReader bufr = new BufferedReader (New InputStreamReader (S.getinputstream ())); PrintWriter pw = new PrintWriter (New FileWriter ("Server.txt"), true); String line = null;while (line = Bufr.readline ())! = null) {pw.println (line);} PrintWriter out = new PrintWriter (S.getoutputstream (), True) out.println ("Upload succeeded");p W.close (); S.close (); Ss.close ();}}

Client:

Package Cn.net.tcp;import Java.io.bufferedreader;import Java.io.filereader;import java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.net.socket;import java.net.UnknownHostException; public class Uploadtxtclient {/** * @param args * @throws ioexception * @throws unknownhostexception */public static void Main (string[] args) throws Unknownhostexception,ioexception {/* * * Upload the text of the client, read local text data, sent to the server, the service end received, the feedback "upload success" word. */SYSTEM.OUT.PRINTLN ("Client initiated ..."); Socket s = new socket ("127.0.0.1", 10005); BufferedReader bufr = new BufferedReader (New FileReader ("Client.txt")); PrintWriter out = new PrintWriter (S.getoutputstream (), true); String line = null;while (line = Bufr.readline ())! = null) {out.println (line);} S.shutdownoutput (); BufferedReader Bufin = new BufferedReader (New InputStreamReader (S.getinputstream ())); String linin = Bufin.readline (); System.out.println (Linin); Bufin.close (); S.close ();}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java TCP upload Text

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.