标签:
大写服务器的实例:
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); //键盘输入 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //socket输入流 BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream())); //socket输出流 // 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();//***要注意 "\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) { } }
文件上传实例:
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; /* *将文件上传 * */ class TextClient{ public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 10006); //注意这里的文件路径非常重要,找了好久的错误 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();//关闭数据流,输出结束标志 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); pw.println("上传成功"); out.close(); s.close(); ss.close(); } } public class UploadText { public static void main(String[] args) { } }
标签:
原文地址:http://www.cnblogs.com/lzeffort/p/4716808.html