----------------------------------------网络编程概述----------------------------------------
计算机网络:
是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程:
就是用来实现网络互连的不同计算机上运行的程序间可以进行数据交换。
网络模型:
计算机网络之间以何种规则进行通信,就是网络模型研究问题。
网络模型一般是指:
1.OSI(Open System Interconnection开放系统互连)参考模型
2.TCP/IP参考模型
网络通信三要素:
1.IP地址:InetAddress;网络中设备的标识,不易记忆,可用主机名
2.端口号:用于标识进程的逻辑地址,不同进程的标识
3.传输协议:通讯的规则;常见协议:TCP,UDP
要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机,在TCP/IP协议中,这个标识号就是IP地址。
那么,我们如果获取和操作IP地址呢?为了方便我们对IP地址的获取和操作,java提供了一个类InetAddress 供我们使用
协议UDP和TCP:
UDP:将数据源和目的封装成数据包中,不需要建立连接;每个数据报的大小在限制在64k;因无连接,是不可靠协议;不需要建立连接,速度快.
TCP:建立连接,形成传输数据的通道;在连接中进行大数据量传输;通过三次握手完成连接,是可靠协议;必须建立连接,效率会稍低.
Socket套接字:
网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字。
Socket原理机制:
1.通信的两端都有Socket。
2.网络通信其实就是Socket间的通信。
3.数据在两个Socket间通过IO传输。
InetAddress类:此类表示互联网协议 (IP) 地址。
获取任意主机:getByName(静态方法)
机名:getHostName
主机Ip地址:getHostAddress
- public class Demo {
- public static void main(String[] args) {
- try {
- // 通过主机名获取主机地址
- InetAddress address = InetAddress.getByName("Yang-PC");
- // 主机名
- String hostName = address.getHostName();
- // 主机ip地址
- String ip = address.getHostAddress();
- System.out.println("计算机名:" + hostName + " IP:" + ip);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- }
- }
UDP发送数据:
1.实例化一个:DatagramSocket对象;
2.准备数据:
1).目标机器的IP:InetAddress对象;
2).目标机器的端口号;
3).信息内容:byte[]数组
3.准备数据包:DatagramPacket对象;需要封装:IP、端口、信息内容;
4.发送数据:DatagramSocket对象的send(DatagramPacket p)方法,
- public class Send {
- public static void main(String[] args) throws IOException {
- // 1.准备Socket
- DatagramSocket socket = new DatagramSocket();
- // 2.准备数据
- InetAddress add = InetAddress.getByName("Yang-PC");
- int port = 8888;
- byte[] byteArray = "你好,这里是使用UDP发送的数据!".getBytes();
- // 3.准备数据包:
- DatagramPacket pck = new DatagramPacket(byteArray, byteArray.length,
- add, port);
- // 4.发送数据
- socket.send(pck);
- // 5.释放资源
- socket.close();
- System.out.println("信息已发出!");
- }
- }
- public class Receive {
- public static void main(String[] args) throws IOException {
- DatagramSocket socket = new DatagramSocket(8888);
- byte[] byteArray = new byte[1024];
- DatagramPacket pck = new DatagramPacket(byteArray, byteArray.length);
- // 等待数据到达
- System.out.println("正在等待信息到达......");
- socket.receive(pck);// 程序在此处会阻塞
- System.out.println("有信息达到!");
- // 获取数据
- byte[] byteInfo = pck.getData();
- String str = new String(byteInfo, 0, pck.getLength());
- String ip = pck.getAddress().getHostAddress();
- System.out.println("接收到信息:IP:" + ip + " 内容:" + str);
- socket.close();
- }
- }
TCP协议_发送数据:
1.实例化一个Socket对象(指定目标计算机的IP和端口号);
2.从Socket对象中,获取一个"输出流(字节流)";
3.使用字节流,输出一个byte[]数组;
4.关闭流;
- public class Send {
- public static void main(String[] args) throws IOException {
- //1.实例化一个Socket
- Socket socket = new Socket("192.168.30.241",8888);
- //2.获取输出流
- OutputStream out = socket.getOutputStream();
- //3.输出一个byte[]数组
- out.write("你好,这是使用TCP协议发送的数据......".getBytes());
- //4.关闭流
- out.close();
- socket.close();
- System.out.println("发送端发送完毕!");
- }
- }
- public class Receive {
- public static void main(String[] args) throws IOException {
- //1.实例化一个ServerSocket
- ServerSocket server = new ServerSocket(8888);
- //2.等待连接
- System.out.println("等待连接......");
- Socket socket = server.accept();
- System.out.println("有客户端连接成功!");
- //3.获取输入流
- InputStream in = socket.getInputStream();
- byte[] byteArray = new byte[1024];
- int len = in.read(byteArray);
- String str = new String(byteArray,0,len);
- System.out.println("接收到信息:" + str);
- //4.释放资源
- in.close();
- server.close();
- }
- }
TCP练习_客户端键盘录入服务器写到文本文件:
客户端代码:
1.实例化一个Socket;
2.获取一个输出流;
3.实例化一个Scanner接收控制台数据;
4.循环,接收控制台数据,每接收一条,就发送一条直到接收到886为止;
5.释放资源
- public class Client {
- public static void main(String[] args) throws IOException {
- Socket socket = new Socket("192.168.30.241", 9999);
- OutputStream out = socket.getOutputStream();
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入信息:");
- String str = sc.next();
- out.write(str.getBytes());
- if (str.equals("886")) {
- break;
- }
- }
- System.out.println("客户端结束!");
- out.close();
- socket.close();
- }
- }
- public class Server {
- public static void main(String[] args) throws IOException {
- ServerSocket server = new ServerSocket(9999);
- Socket socket = server.accept();
- // 网络输入流
- InputStream netIn = socket.getInputStream();
- // 文件输出流
- FileOutputStream fileOut = new FileOutputStream("demo08.txt");
- // 读取网络数据
- byte[] byteArray = new byte[1024];
- int len = 0;
- while (true) {
- len = netIn.read(byteArray);
- if (len == -1) {
- break;
- }
- fileOut.write(byteArray, 0, len);
- fileOut.write("\r\n".getBytes());
- String str = new String(byteArray, 0, len);
- if (str.equals("886")) {
- break;
- }
- }
- System.out.println("服务器端结束!");
- fileOut.close();
- netIn.close();
- socket.close();
- server.close();
- }
- }
TCP练习_TCP协议上传文本文件:
- 客户端:
- *
- * 1.实例化一个Socket;
- * 2.获取网络输出流;
- * 3.实例化一个文件输入流(字节流);
- * 4.循环读取文件内容,一次读取一个字节数组;
- * 一次发送一个字节数组;
- * 5.释放资源;
- */
- public class Client {
- public static void main(String[] args) throws IOException {
- Socket socket = new Socket("192.168.30.241", 8888);
- OutputStream netOut = socket.getOutputStream();
- FileInputStream fileIn = new FileInputStream("demo10.txt");
- byte[] byteArray = new byte[1024];
- int len = 0;
- while ((len = fileIn.read(byteArray)) != -1) {
- netOut.write(byteArray, 0, len);
- }
- fileIn.close();
- netOut.close();
- socket.close();
- System.out.println("客户端上传完毕!");
- }
- }
- 服务器端代码:
- *
- * 1.实例化一个ServerSocket;
- * 2.获取一个网络输入流;
- * 3.实例化一个文件输出流;
- * 4.循环读取,一次读取一个字节数组;
- * 一次写入一个字节数组;
- * 5.释放资源;
- */
- public class Server {
- public static void main(String[] args) throws IOException {
- ServerSocket server = new ServerSocket(8888);
- Socket socket = server.accept();
- // 网络输入流
- InputStream netIn = socket.getInputStream();
- // 文件输出流
- FileOutputStream fileOut = new FileOutputStream("demo10_upload.txt");
- // 一次读取一个字节数组
- byte[] byteArray = new byte[1024];
- int len = 0;
- while ((len = netIn.read(byteArray)) != -1) {
- fileOut.write(byteArray, 0, len);
- }
- // 释放资源
- fileOut.close();
- netIn.close();
- socket.close();
- server.close();
- System.out.println("服务器端完毕!");
- }
- }
TCP练习_关于多个客户端上传到一个服务器:
- public class Client {
- public static void main(String[] args) {
- //模拟10个客户端同时连接服务器
- for(int i = 1 ; i <= 10 ;i ++){
- new ClientThread(i).start();
- }
- }
- }
- public class ClientThread extends Thread {
- private int index;
- public ClientThread(int index) {
- this.index = index;
- }
- @Override
- public void run() {
- // 连接服务器
- try {
- Socket socket = new Socket("192.168.30.241", 8888);
- // 网络输出流
- OutputStream netOut = socket.getOutputStream();
- // 文件输入流
- FileInputStream fileIn = new FileInputStream("C:\\ccc\\" + index
- + ".png");
- // 一次读取一个字节数组
- byte[] byteArray = new byte[1024];
- int len = 0;
- while ((len = fileIn.read(byteArray)) != -1) {
- netOut.write(byteArray, 0, len);
- }
- // 释放资源
- fileIn.close();
- netOut.close();
- socket.close();
- System.out.println("文件:" + index + ".png 上传完毕!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class ServerThread extends Thread {
- public int index;
- private Socket socket;
- // 此索引,跟客户端的索引不是一一对应的;
- public ServerThread(int index, Socket socket) {
- this.index = index;
- this.socket = socket;
- }
- @Override
- public void run() {
- // 获取网络输入流
- try {
- InputStream netIn = socket.getInputStream();
- FileOutputStream fileOut = new FileOutputStream(index + ".png");
- byte[] byteArray = new byte[1024];
- int len = 0;
- while ((len = netIn.read(byteArray)) != -1) {
- fileOut.write(byteArray, 0, len);
- }
- fileOut.close();
- netIn.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // 文件的输出流
- }
- }
- public class Server {
- public static void main(String[] args) throws IOException {
- ServerSocket server = new ServerSocket(8888);
- int index = 1;
- while (true) {
- Socket socket = server.accept();
- // 启动线程;
- new ServerThread(index++, socket).start();
- }
- }
- }