TCP Two example uppercase server and file upload in Java

Source: Internet
Author: User

Example of an uppercase server:

Package Com.core.net;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.InputStreamReader; Import Java.io.outputstreamwriter;import java.io.printwriter;import java.net.serversocket;import java.net.Socket; Class Transclient{public static void Main (string[] args) throws Exception {socket socket = new socket ("localhost", 10005); Keyboard input BufferedReader reader = new BufferedReader (new InputStreamReader (system.in));//socket input stream BufferedReader Bufin = New BufferedReader (New InputStreamReader (Socket.getinputstream ()))//socket output stream//bufferedwriter bufout = new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())); PrintWriter bufout = new PrintWriter (Socket.getoutputstream (), true); String line = null;while (line = Reader.readline ())!=null) {if (' over '. Equals (line)] Break;bufout.println (line);// Bufout.write (line);//bufout.newline ();//*** should pay attention to "\ r \ n"//bufout.flush (); String str = bufin.readline (); SYSTEM.OUT.PRINTLN ("server:" +str);} Reader.close (); Socket.close ();}} Class TransservEr{public static void Main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (10005);     Socket s = ss.accept ();     String IP = s.getinetaddress (). gethostaddress (); System.out.println (ip+ "... connection"); BufferedReader reader = new BufferedReader (New InputStreamReader (S.getinputstream ()));//BufferedWriter out = new BufferedWriter (New OutputStreamWriter (S.getoutputstream ()));  PrintWriter out = new PrintWriter (S.getoutputstream (), true); String line = null; while (line = Reader.readline ())!=null) {System.out.println (line); Out.println (Line.touppercase ());//Out.write ( Line.touppercase ());//Out.newline ();//Out.flush (); } s.close (); Ss.close ();}} public class Transdemo {public static void main (string[] args) {}}

File Upload instance:

Package Com.core.net;import Java.io.bufferedreader;import Java.io.filereader;import java.io.filewriter;import  Java.io.inputstreamreader;import java.io.printwriter;import java.net.serversocket;import java.net.Socket;/* * Upload file * */class textclient{public static void Main (string[] args) throws Exception {socket s = new socket ("localhost", 10006);// Note that the file path here is very important, looking for a long time error bufferedreader reader = new BufferedReader (New FileReader ("Src/com/core/net/ipdemo.java")); PrintWriter bufout = new PrintWriter (S.getoutputstream (), true); String line = null;while (line = Reader.readline ())!=null) {bufout.println (line);} S.shutdownoutput ();//close data flow, output end flag BufferedReader Bufin = new BufferedReader (New InputStreamReader (S.getinputstream () )); String str = bufin.readline (); System.out.println (str); Reader.close (); S.close ();}} Class Textserver{public static void Main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (10006); Socket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress (); System.out. println (ip+ "... connection"); BufferedReader Bufin = new BufferedReader (New InputStreamReader (S.getinputstream ())); PrintWriter out = new PrintWriter (New FileWriter ("Server.txt"), true); String line = Null;while (line =bufin.readline ())!=null) {out.println (line);} PrintWriter pw = new PrintWriter (S.getoutputstream (), True);p w.println ("Upload succeeded"); Out.close (); S.close (); Ss.close ();}} public class Uploadtext {public static void main (string[] args) {}}

  

TCP Two example uppercase server and file upload in Java

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.