不会IO流你怎么去玩Socket
netstat指令
netstat -an 可以查看当前的网络情况,包括监听的端口和网络连接的情况
netstat -an | more 分页查看
netstat -anb 显示程序名称
UDP编程
Java里面UDP原理
1,没有服务端和客户端因为他们两个都可以互换
2,DatagramSocket,类似于插口接收数据和发送数据都是通过这个对象完成的,可以指定端口。
3,DatagramPacket,这个是把数据封装到这个对象里面,进行传输
代码块
package udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.nio.charset.StandardCharsets; public class B { public static void main(String[] args) throws IOException { //创建接收端 DatagramSocket datagramSocket = new DatagramSocket(9999); //UDP的数据是封装到一个对象里的,创建一个对象去接受 byte bu[] = new byte[64 * 1024]; DatagramPacket datagramPacket = new DatagramPacket(bu,0,bu.length); //调用send方法准备去接收对象,如果没有对象过来会阻塞 datagramSocket.receive(datagramPacket); //对象过来之后拆包 byte[] data = datagramPacket.getData(); int length = datagramPacket.getLength(); String s = new String(data, 0, length); System.out.println(s); //到准备去回应 byte[] bytes = "enen".getBytes(StandardCharsets.UTF_8); DatagramPacket datagramPacket1 = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),9998); datagramSocket.send(datagramPacket1); //关闭资源 datagramSocket.close(); } }
package udp; import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets; public class A { public static void main(String[] args) throws IOException { //创建发送端 DatagramSocket datagramSocket = new DatagramSocket(9998); //发送数据 byte[] bytes = "hi,hi".getBytes(StandardCharsets.UTF_8); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),9999); //发送数据 datagramSocket.send(datagramPacket); //去拿数据 byte bu[] = new byte[64 * 1024]; DatagramPacket datagramPacket1 = new DatagramPacket(bu,0,bu.length); //调用send方法准备去接收对象,如果没有对象过来会阻塞 datagramSocket.receive(datagramPacket1); //对象过来之后拆包 byte[] data = datagramPacket1.getData(); int length = datagramPacket1.getLength(); String s = new String(data, 0, length); System.out.println(s); //关闭流 datagramSocket.close(); } }
不会IO流你怎么去玩Socket
ps:IO流分字节流和字符流
我们要以内存为参考点[特别容易弄乱]
以内存到[磁盘,网络服务]这种流是输出流
[磁盘,网络服务]到内存这种流是输入流
如图
字节流
OutPutStream 字节输出流,向D盘下面A.txt文件里面写入hi
public class Out { public static void main(String[] args) throws IOException { File file = new File("D:\\A.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write("hi".getBytes(StandardCharsets.UTF_8)); System.out.println("创建成功"); fileOutputStream.close(); } }
InPutStream 字节输入流,我们去读这A.txt文件
public class Out { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream(file); byte bytes[] = new byte[1024]; int leng; while ((leng = fileInputStream.read(bytes,0,bytes.length)) != -1){ System.out.println(new String(bytes,0,leng)); } } }
字符流
Writer 字符输出流
public class Out { public static void main(String[] args) throws IOException { FileWriter fileWriter = new FileWriter(file); fileWriter.write("您好"); } }
Reader 字符输入流
public class Out { public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader(file); char chars[] = new char[1024]; int leng; while ((leng = fileReader.read(chars,0,chars.length)) != -1){ System.out.println(new String(chars,0,leng)); } } }
记得关闭流
#Java学习#