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