static int port = 8080;
public static void main(String[] args) {
//1.建立服务器对象
ServerSocket serverSocket = null;
//2.建立客户端对象
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//3.监听指定端口
serverSocket = new ServerSocket(port);
System.out.println("start server socket....");
while (true){
//4.获取连接,该方法会阻塞
socket = serverSocket.accept();
System.out.println("获取到socket连接...");
//5.读取数据
inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
//read方法会阻塞
while ((length = inputStream.read(buffer)) > 0) {
String msg = new String(buffer, 0, length);
System.out.println("接收到客户端数据:" + msg);
//6.发送回执
outputStream = socket.getOutputStream();
outputStream.write("server get data".getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
socket.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
telnet localhost 8080
/**
* @Author pei.sun
* @Date 2020/7/11 23:24
* @Description 1.buffer-负责存储数据,可以存储不同类型的数据。底层是数组。
* 根据数据类型不同,提供了相应的类型的缓冲区(除了boolean)
* ByteBuffer、CharBuffer、ShortBuffer、IntBuffer
* LongBuffer、FloatBuffer、DoubleBuffer
* 2.缓冲区核心方法
* allocate:分配缓冲区
* put:存储数据到缓冲区
* get:获取缓冲区中的数据
* 3.缓冲区四大核心属性
* capacity:容量,表示缓冲区中最大存储数据容量,一旦声明就不能改变。
* limit:界限,表示缓冲区中可以操作数据大小。limit后的数据不能操作.
* position:位置,表示正在操作数据的位置
* mark:标记,记录当前position位置,通过reset()恢复到mark的位置
* 0<=mark <=position<= limit <= capacity
*/
public class TestBuffer {
public static void main(String[] args) {
System.out.println("=======allocate()=======");
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//写数据
System.out.println("=======put()=======");
String msg = "newio";
buf.put(msg.getBytes());
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//flip 切换读写模式
System.out.println("=======flip()=======");
buf.flip();
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
System.out.println("=======get()=======");
byte[] dst = new byte[buf.limit()];
buf.get(dst);
System.out.println("data:" + new String(dst, 0, dst.length));
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//rewind: 重复读
System.out.println("=======rewind()=======");
buf.rewind();
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//clear:清空缓冲区,实际只改变了指针,实际数据还存在,只是处于“被遗忘状态”
System.out.println("=======clear()=======");
buf.clear();
System.out.println("position:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//mark:存档
System.out.println("=======mark()=======");
buf.put(msg.getBytes());
buf.flip();
byte[] result = new byte[buf.limit()];
buf.get(result, 0, 2);
System.out.println("result:" + new String(result, 0, 2));
System.out.println("position:" + buf.position());
buf.mark();
buf.get(result, 2, 2);
System.out.println("result:" + new String(result, 2, 2));
System.out.println("position:" + buf.position());
System.out.println("=======reset()=======");
buf.reset();
System.out.println("position:" + buf.position());
//缓冲区中是否有剩余数据
System.out.println("=======remaining()=======");
if (buf.hasRemaining()) {
System.out.println("remaining:"+buf.remaining());
}
}
}
本文地址:https://blog.csdn.net/sunpeiv/article/details/107371575
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论