网络的相关概念:
概念:两台设备之间通过网络实现数据传输
网络通信:将数据通过网络从一台设备传输到另一台设备
java.net包下提供了一系列的类或接口,供程序员使用,完成网络通信
概念:用于唯一标识网络中的每台计算机
查看ip地址:ipconfig
ip地址的表示形式:点分十进制 xx.xx.xx.xx
每一个十进制数的范围:0~255
ip地址的组成=网络地址+主机地址,比如:192.168.16.69,192.168.16为网络地址(例如家庭地址中的北京市海淀区朝阳小区),69为主机地址(相当于门牌号)
ilPv6是互联网工程任务组设计的用于替代IPv4的下一代IP协议,其地址数量号称可以为全世界的每一粒沙子编上一个地址
由于IPv4最大的问题在于网络地址资源有限,严重制约了互联网的应用和发展。IPv6的使用,不仅能解决网络地址资源数量的问题,而且也解决了多种接入设备连入互联网的障碍
ip定位主机,端口定位服务,访问要ip+端口
协议(tcp/ip)
TCP/IP中文译名传输控制协议/因特网互联协议,又叫网络通讯协议,是Internet国际互联网络的基础
三次握手举例:
UDP举例:
不进行握手确认,直接发送数据,类似于发短信
package com.hsp.net; import java.net.InetAddress; import java.net.UnknownHostException; /** * Description * User: * Date: * Time: * InetAddress类方法的使用 */ public class test01 { public static void main(String[] args) throws UnknownHostException { //获取本机的InetAddress对象 InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost);//LAPTOP-2UERK2QG/192.168.205.1 //根据指定的主机名获取对象 InetAddress localhost2 = InetAddress.getByName("LAPTOP-2UERK2QG"); //根据域名获取对象 InetAddress localHost3 = InetAddress.getByName("www.baidu.com"); //通过InetAddress对象获取对应的主机名地址等信息 //获取主机名 String hostName = localHost.getHostName(); System.out.println("主机名:"+hostName); //获取ip地址 String hostAddress = localHost.getHostAddress(); System.out.println("ip地址:"+hostAddress); } }
基本介绍:
package com.hsp.net.socket; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; /**服务端 * Description * User: * Date: * Time: */ public class SSocketTCP01Server { public static void main(String[] args) throws IOException { // 1.在本季的9999端口监听,等待连接 //前提是9999端口没有被占用 //细节:这个ServerSocket可以通过accept()返回多个Socket[高并发] ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端,在9999端口监听,等待连接"); //2.当没有客户端连接9999端口时,程序会阻塞,等待连接 //如果有客户端连接,则会返回socket对象,程序继续 Socket socket = serverSocket.accept(); System.out.println("服务端 socket ="+socket.getClass()); //3.通过socket.getInputStream()读取客户端写入到数据通道的数据,显示 InputStream in = socket.getInputStream(); byte[] bt = new byte[1024]; int readLen = 0; while((readLen= in.read(bt)) != -1){ System.out.println(new String(bt,0,readLen)); } //关闭流和socket in.close(); socket.close(); serverSocket.close(); } }
package com.hsp.net.socket; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /**客户端 * Description * User: * Date: * Time: */ public class SocketTCP01Client { public static void main(String[] args) throws IOException { //1.连接服务器(ip,端口) //连接本机的9999端口 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("客户端:"+socket.getClass()); //2.连接上后,生成Socket,通过 //socket.getOutputStream() OutputStream out = socket.getOutputStream(); //3.通过输出流,写入数据到数据通道 out.write("hello server".getBytes()); //4.关闭流对象和socket,必须关闭 out.close(); socket.close(); System.out.println("客户端退出了"); } }
package com.hsp.net.socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * Description * User: * Date: * Time: */ public class SSocketTCP02Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端等待连接中..."); Socket socket = serverSocket.accept(); System.out.println("服务端 端口号9999 收到连接"); System.out.println("服务端向用户端发送信息"); OutputStream out = socket.getOutputStream(); out.write("hello client".getBytes()); socket.shutdownOutput(); InputStream in = socket.getInputStream(); System.out.println("服务端接收来自用户端的信息"); byte[] bt = new byte[1024]; int length = 0; while((length=in.read(bt))!=-1){ System.out.println(new String(bt,0,length)); } socket.shutdownInput(); in.close(); out.close(); socket.close(); serverSocket.close(); System.out.println("服务端退出"); } }
package com.hsp.net.socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /** * Description * User: * Date: * Time: */ public class SocketTCP02Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9999); System.out.println("用户端:"+socket); OutputStream out = socket.getOutputStream(); System.out.println("用户端向服务端发送信息"); out.write("hello serve".getBytes()); socket.shutdownOutput(); InputStream in = socket.getInputStream(); System.out.println("用户端接收来自服务端的信息"); byte[] bt = new byte[1024]; int length = 0; while((length=in.read(bt))!=-1){ System.out.println(new String(bt,0,length)); } socket.shutdownInput(); in.close(); out.close(); socket.close(); System.out.println("用户端退出"); } }
这里需要现将字节流利用转换流转换成字符流,服务端接收到字节流,在转换成字符流进行输出
package com.hsp.net.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Description * User: * Date: * Time: */ public class SSocketTCP03Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端等待连接中..."); Socket socket = serverSocket.accept(); System.out.println("服务端 端口号9999 收到连接"); System.out.println("服务端向用户端发送信息"); OutputStream out = socket.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(out)); bufferedWriter.write("hello client:字符流"); bufferedWriter.flush();//如果使用的字符流,需要手动刷新,否则数据不会写入数据通道 bufferedWriter.newLine();//插入一个换行符,表示写入的内容结束,注意,要求对方使用readLine() socket.shutdownOutput(); InputStream in = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); System.out.println("服务端接收来自用户端的信息"); String s = bufferedReader.readLine(); System.out.println(s); socket.shutdownInput(); in.close(); out.close(); socket.close(); serverSocket.close(); System.out.println("服务端退出"); } }
package com.hsp.net.socket; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /** * Description * User: * Date: * Time: */ public class SocketTCP03Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9999); System.out.println("用户端:"+socket); OutputStream out = socket.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(out)); bufferedWriter.write("hello serve:字符流"); bufferedWriter.flush(); bufferedWriter.newLine(); System.out.println("用户端向服务端发送信息"); socket.shutdownOutput(); InputStream in = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); System.out.println("用户端接收来自服务端的信息"); String s = bufferedReader.readLine(); System.out.println(s); socket.shutdownInput(); in.close(); out.close(); socket.close(); System.out.println("用户端退出"); } }
StreamUtils的作用:将输入流转换成byte[],即可以把文件的内容读入到byte[]
示意图:
使用BufferedInputStream和BufferedOutputStream字节流
package com.hsp.net.socket; import com.hsp.net.StreamUtils; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.stream.Stream; /** * Description * User: * Date: * Time: */ public class SSocketTCP04Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); Socket socket = serverSocket.accept(); //接收用户端传递的图片数组,并将其输出在src目录下 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); byte[] bytes = StreamUtils.streamTobytes(bis); socket.shutdownInput(); String filepath = "src/test.png"; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath)); bos.write(bytes); //服务端向用户端发送信息 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bw.write("图片发送成功"); bw.flush(); bw.newLine(); socket.shutdownOutput(); bw.close(); bos.close(); bis.close(); socket.close(); serverSocket.close(); } }
package com.hsp.net.socket; import com.hsp.net.StreamUtils; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; /** * Description * User: * Date: * Time: */ public class SocketTCP04Client { public static void main(String[] args) throws Exception { Socket socket = new Socket(InetAddress.getLocalHost(),9999); // //将图片转换成字节数组 String filepath = "E:\\test.png"; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filepath)); byte[] bytes = StreamUtils.streamTobytes(bis); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); bos.write(bytes); socket.shutdownOutput(); //用户端接收服务端发送的消息 // BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); InputStream inputStream = socket.getInputStream(); System.out.println(StreamUtils.streamToString(inputStream)); socket.shutdownInput(); // br.close(); inputStream.close(); bos.close(); bis.close(); socket.close(); } }
(1)Listening表示某个端口在监听
(2)如果有一个外部程序(客户端)连接到该端口,就会显示一条连接信息
package com.hsp.net.homework; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Description * User: * Date: * Time: */ public class homework01Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端等待接收数据..."); Socket socket = serverSocket.accept(); BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = bf.readLine(); // socket.shutdownInput(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); if("name".equals(s)){ bw.write("我是novo,是你自己的名字"); bw.flush(); bw.newLine(); // socket.shutdownOutput(); }else if("hobby".equals(s)){ bw.write("编写java程序"); bw.flush(); bw.newLine(); // socket.shutdownOutput(); }else{ bw.write("不清楚"); bw.flush(); bw.newLine(); // socket.shutdownOutput(); } //关闭资源 bw.close(); bf.close(); socket.close(); serverSocket.close(); System.out.println("服务器端退出"); } }
package com.hsp.net.homework; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.util.Scanner; /** * Description * User: * Date: * Time: */ public class homework02Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9999); //向Server发送数据 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); Scanner sc = new Scanner(System.in); String next = sc.next(); bw.write(next); bw.newLine(); bw.flush(); // socket.shutdownOutput(); //接收来自服务端的信息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = br.readLine(); // socket.shutdownInput(); System.out.println("服务器说:"+s); //关闭资源 br.close(); bw.close(); socket.close(); System.out.println("客户端退出"); } }
package com.hsp.net.homework; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * Description * User: * Date: * Time:接收端 */ public class homework02Receiver { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(9999); byte[] bt = new byte[1024]; DatagramPacket packet = new DatagramPacket(bt,bt.length); System.out.println("Receiver等待接收数据..."); socket.receive(packet); int length = packet.getLength(); byte[] data = packet.getData(); String s = new String(data, 0, length); byte[] bytes1 = "红楼梦...".getBytes(); byte[] bytes2 = "what".getBytes(); if("四大名著是哪些".equals(s)){ System.out.println("进入"); DatagramPacket packet1 = new DatagramPacket(bytes1,bytes1.length, InetAddress.getByName("192.168.10.1"),9998); socket.send(packet1); }else{ DatagramPacket packet1 = new DatagramPacket(bytes2,bytes2.length, InetAddress.getByName("192.168.10.1"),9998); socket.send(packet1); } //关闭资源 socket.close(); System.out.println("Receiver关闭"); } }
package com.hsp.net.homework; import java.io.IOException; import java.net.*; import java.util.Scanner; /** * Description * User: * Date: * Time: */ public class homework02Sender { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(9998); Scanner sc = new Scanner(System.in); String next = sc.next(); DatagramPacket packet = new DatagramPacket(next.getBytes(),next.getBytes().length, InetAddress.getByName("192.168.10.1"),9999); socket.send(packet); //接收来自Receiver的数据 byte[] bt1 = new byte[1024]; DatagramPacket packet1 = new DatagramPacket(bt1,bt1.length); socket.receive(packet1); int length = packet1.getLength(); byte[] data = packet1.getData(); String s = new String(data, 0, length); System.out.println("Receiver说:"+s); //关闭资源 socket.close(); System.out.println("Sender关闭"); } }
package com.hsp.net; import java.io.*; /** * Description * User: * Date: * Time: */ public class StreamUtils { public static byte[] streamTobytes(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bt = new byte[1024]; int l=0; while((l=in.read(bt))!=-1){ bos.write(bt,0,l); } return bos.toByteArray(); } public static String streamToString(InputStream is) throws Exception{ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder builder = new StringBuilder(); String line; while((line=reader.readLine())!=null){ builder.append(line+"\r\n"); } return builder.toString(); } }
package com.hsp.net.homework; import com.hsp.net.StreamUtils; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Description * User: * Date: * Time: */ public class homework03Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端等待连接..."); Socket socket = serverSocket.accept(); //服务端接收客户端传递的文件名 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = br.readLine(); socket.shutdownInput(); String filepath = "src/"+s; String defaultpath = "src/aa.txt"; File file = new File(filepath); BufferedInputStream bis = null; //将文件发送给客户端(先默认存在) if(file.exists()){ bis = new BufferedInputStream(new FileInputStream(filepath)); }else{ bis = new BufferedInputStream(new FileInputStream(defaultpath)); } byte[] bytes = StreamUtils.streamTobytes(bis); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); bos.write(bytes); socket.shutdownOutput(); //关闭资源 bis.close(); socket.close(); serverSocket.close(); System.out.println("服务端关闭"); } }
package com.hsp.net.homework; import com.hsp.net.StreamUtils; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; /** * Description * User: * Date: * Time: */ public class homework03Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9999); //输入文件名,并发给服务端 Scanner sc = new Scanner(System.in); String next = sc.next(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bw.write(next); bw.newLine(); bw.flush(); socket.shutdownOutput(); //接收服务端传来的数据,并将文件转换成bytes存储在f盘下 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); byte[] bytes = StreamUtils.streamTobytes(bis); String filepath = "F:\\test.png"; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath)); bos.write(bytes); System.out.println("文件保存成功"); socket.shutdownInput(); //关闭资源 bos.close(); socket.close(); System.out.println("用户端关闭"); } }