TCP通信
1.0 客户端的实现
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPSocket { public static void main(String[] args) throws IOException { Socket socket=new Socket("127.0.0.1",8888); OutputStream stream = socket.getOutputStream(); stream.write("你好服务器".getBytes()); InputStream inputStream = socket.getInputStream(); byte []bytes=new byte[1024]; int read = inputStream.read(bytes); System.out.println(new String(bytes,0,read)); socket.close(); } }
2.0 服务器代码实现
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket=new ServerSocket( 8888 ); Socket socket = serverSocket.accept(); InputStream stream = socket.getInputStream(); byte bytes[]=new byte[1024]; int read = stream.read(bytes); System.out.println(new String(bytes,0,read)); OutputStream outputStream = socket.getOutputStream(); outputStream.write("谢谢收到!".getBytes()); socket.close(); serverSocket.close(); } }
3.0 TCP通信的综合案例(上传文件从客户端到服务器,并且存储到服务器的硬盘中)
客户端实现
import java.io.*; import java.net.Socket; public class TCPSocket { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("D:\\photos\\白雪公主.jpg"); Socket socket=new Socket("127.0.0.1",8888); OutputStream outputStream = socket.getOutputStream(); byte []bytes=new byte[1024]; int len=0; while((len=fis.read(bytes))!=-1) { outputStream.write(bytes,0,len); } socket.shutdownOutput(); /* *上传完文件,给服务器写一个结束标记 * 禁用此套接字的输出流 * 对于TCP套接字,任何之前的写入的数据都将被发送, * 并且后跟TCP的正常连接终止序列 * */ InputStream inputStream = socket.getInputStream(); byte []bytes1=new byte[1024]; int len1=inputStream.read(bytes1); System.out.println(new String(bytes1,0,len1)); socket.close(); fis.close(); } }
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TCPServer{ public static void main(String[] args) throws IOException { ServerSocket serverSocket=new ServerSocket(8888); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); File file=new File("D:\\upload"); if(!file.exists()) { file.mkdirs(); } FileOutputStream fos=new FileOutputStream(file+"\\白雪公主.jpg") ; byte[] bytes=new byte[1024]; int len=0; while((len=inputStream.read(bytes))!=-1) { fos.write(bytes,0,len); } socket.getOutputStream().write("上传成功!".getBytes()); serverSocket.close(); fos.close(); socket.close(); } }
函数式接口
1.0 概念
2.0利用Lambda表达式的执行延迟(接口作为方法的参数传递)
优化日志的案例(输入编号,输出字符串拼接后的字符串 先字符串拼接后执行 所以可能会浪费资源)
public interface MessageBuilder { public abstract String bulidMessage(); }
public class Main{ public static void main(String[] args) { String msg1="Hello"; String msg2="World"; String msg3="Java"; showlog(1,()->msg1+msg2+msg3 ); } private static void showlog(int grade,MessageBuilder ms) { if(grade==1) { System.out.println(ms.bulidMessage()); } } }
3.0 接口作为方法的返回值传递
数组排序案例
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Comparator; public class Main{ public static ComparatorgetCompare() { return new Comparator () { @Override public int compare(String o1, String o2) { return o2.length()-o1.length();//长度降序 } }; } public static void main(String[] args) { String arr[]={"jin,27","jimin,25","v,25"}; Arrays.sort(arr,(o1,o2)->o2.length()-o1.length()); System.out.println(Arrays.toString(arr)); } }
打印结果:
[jimin,25, jin,27, v,25]
常用的函数式接口
1.0 Supplier接口
练习 找出数组中的最大值 并打印
import java.util.function.Supplier; public class Main{ public static int getMax(Suppliersup) { Integer integer = sup.get(); return integer; } public static void main(String[] args) { int Max = getMax(() -> { int arr[] = {34, 234, 52, 5, 65}; int max = arr[0]; for (int i : arr) { if (i > max) max = i; } return max; }); System.out.println(Max);//234 } }
2.0 Consumer接口
import java.util.function.Consumer; public class Main { public static void conSume(String name, Consumercon) { con.accept(name); } public static void main(String[] args) { conSume("金泰亨",(name)->{ System.out.println(name); }); } }
默认方法 andThen
练习 格式化打印信息 格式 姓名:XX,年龄:XX
import java.util.function.Consumer; public class Main { public static void conSume(String []arr, Consumercon1,Consumer con2) { for (String s : arr) { con1.andThen(con2).accept(s); } } public static void main(String[] args) { String arr[]={"Suga,26","Jimin,25","JK,23"}; conSume(arr,(s)->{ String s1 = s.split(",")[0]; System.out.print("姓名:"+s1+","); },(s)->{ String s2 = s.split(",")[1]; System.out.println("年龄:"+s2); }); } }
打印结果:
姓名:Suga,年龄:26
姓名:Jimin,年龄:25
姓名:JK,年龄:23
3.0 predicate接口
基本使用
import java.util.function.Predicate; public class Main{ public static void CheckString(String s, Predicatepre) { System.out.println(pre.test(s));//false } public static void main(String[] args) { String s="abcde"; CheckString(s,(str)->{ return s.length()>5; }); } }
默认方法 and
import java.util.function.Predicate; public class Main{ public static void checkString(String s, Predicatepre1,Predicate pre2) { boolean test = pre1.and(pre2).test(s); System.out.println(test); } public static void main(String[] args) { String s="abcdef"; checkString(s,(str)-> str.length()>5 ,(str)->str.contains("a") ); } }
默认方法 or
默认方法 negate
4.0 Function接口
练习将String->Integer+10 Integer->String
import java.util.function.Function; public class Main{ public static void Method(String s, Functionfun1,Function fun2) { String apply = fun1.andThen(fun2).apply(s); System.out.println(apply); } public static void main(String[] args) { String s="23"; Method(s,(str)->{ int i = Integer.parseInt(s); return i+10; },(i)->{ return i+""; }); } }