publicclassClientDemo{publicstaticvoidmain(String[] args)throwsIOException{//创建客户端的Socket对象(Socket)//Socket(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号Socket s =newSocket("127.0.0.1",10000);//获取输出流,写数据//OutputStream getOutputStream() 返回此套接字的输出流OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());//释放资源
s.close();}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3.2 TCP接收数据
构造方法
方法名
说明
ServletSocket(int port)
创建绑定到指定端口的服务器套接字
相关方法
方法名
说明
Socket accept()
监听要连接到此的套接字并接受它
注意事项
accept方法是阻塞的,作用就是等待客户端连接
客户端创建对象并连接服务器,此时是通过三次握手协议,保证跟服务器之间的连接
针对客户端来讲,是往外写的,所以是输出流 针对服务器来讲,是往里读的,所以是输入流
read方法也是阻塞的
客户端在关流的时候,还多了一个往服务器写结束标记的动作
最后一步断开连接,通过四次挥手协议保证连接终止
三次握手和四次挥手
三次握手
四次握手
示例代码
publicclassServerDemo{publicstaticvoidmain(String[] args)throwsIOException{//创建服务器端的Socket对象(ServerSocket)//ServerSocket(int port) 创建绑定到指定端口的服务器套接字ServerSocket ss =newServerSocket(10000);//Socket accept() 侦听要连接到此套接字并接受它Socket s = ss.accept();//获取输入流,读数据,并把数据显示在控制台InputStream is = s.getInputStream();byte[] bys =newbyte[1024];int len = is.read(bys);String data =newString(bys,0,len);System.out.println("数据是:"+ data);//释放资源
s.close();
ss.close();}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
4 NIO
4.1 概述
BIO Blocking IO,阻塞型IO
NIO No Blocking IO,非阻塞型IO
阻塞IO的弊端 在等待的过程中,什么事也做不了
非阻塞IO的好处 不需要一直等待,当一切就绪了再去做
4.2 NIO与BIO的区别
区别一 BIO是阻塞的,NIO是非阻塞的
区别二 BIO是面向流的,NIO是面型缓冲区的 BIO中数据传输是单向的,NIO中的缓冲区是双向的
NIO中的三大模块
缓冲区 用来存储数据
通道 用来建立连接和传输数据
选择器 监事通道状态
NIO创建缓冲区对象
方法介绍
方法名
说明
static ByteBuffer allocate(长度)
创建byte类型的缓冲区
static ByteBuffer wrap(byte[] array)
创建一个有内容的byte类型缓冲区
publicclassDemo1{publicstaticvoidmain(String[] args){// method1();// method2();ByteBuffer wrap =ByteBuffer.wrap("aaa".getBytes());for(int i =0; i <3; i++){System.out.println(wrap.get());}}privatestaticvoidmethod2(){byte[] bytes ={97,98,99};ByteBuffer byteBuffer2 =ByteBuffer.wrap(bytes);//缓冲区的长度3//缓冲区里面的内容就是字节数组的内容.for(int i =0; i <3; i++){System.out.println(byteBuffer2.get());}System.out.println(byteBuffer2.get());}privatestaticvoidmethod1(){ByteBuffer byteBuffer1 =ByteBuffer.allocate(5);//getfor(int i =0; i <5; i++){System.out.println(byteBuffer1.get());}System.out.println(byteBuffer1.get());}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
4.5 NIO缓冲区添加数据
publicclassByteBufferDemo2{publicstaticvoidmain(String[] args){// int position() 当前要操作的索引// int limit() 最多能操作到哪个索引// int capacity() 缓冲区的总长度ByteBuffer byteBuffer =ByteBuffer.allocate(10);System.out.println(byteBuffer.position());//0System.out.println(byteBuffer.limit());//10System.out.println(byteBuffer.capacity());//10// put(byte b) 一次添加一个字节// byteBuffer.put((byte) 97);// System.out.println(byteBuffer.position());// System.out.println(byteBuffer.limit());// System.out.println(byteBuffer.capacity());// put(byte[] src) 一次添加一个字节数组// byteBuffer.put("aaa".getBytes());// System.out.println(byteBuffer.position());//3// System.out.println(byteBuffer.limit());//10// System.out.println(byteBuffer.capacity());//10// position(int newPosition) 修改position// byteBuffer.position(1);// limit(int newLimit) 修改limit// byteBuffer.limit(5);// System.out.println(byteBuffer.position());// System.out.println(byteBuffer.limit());// System.out.println(byteBuffer.capacity());// int remaining() 还有多少能操作// boolean hasRemaining() 是否还有能操作的
byteBuffer.put("0123456789".getBytes());System.out.println(byteBuffer.remaining());System.out.println(byteBuffer.hasRemaining());}}