网络编程(1)

网络编程

  • 目的:
    • 无线电台
    • 传播交流信息
    • 数据交换
    • 通信
  • javaweb:网页编程 B/S架构
  • 网络编程:TCP/IP C/S架构
  • 网络通信要素
    • 网络通信协议
    • IP和端口号
  • 规则:网络通信的协议:http,ftp,smtp,tcp,udp等
  • TCP/IP四层概念模型
  • 两个主要问题:
    • 如何准确定位到网络上的一台或者多台主机
    • 找到主机之后如何进行通信

IP类

class InetAddress: 此类表示Internet协议IP地址

  • 唯一定位一台网络上的计算机
  • **127.0.0.1:本机 localhost **
  • IP地址分类:
    • ipv4/ipv6
      • ipv4 127.0.0.1 4个字节组成, 0-255 42亿
      • **ipv6 ** 128 位 8个无符号整数
    • 公网(互联网)/私网(局域网)
      • 192.168.xx.xx 专门给组织内使用的
      • ABCD类地址
package Address;

import java.net.InetAddress;
import java.net.UnknownHostException;

/* 测试IP */
public class TestIP {

	public static void main(String[] args) {
		try {
			
			/* 查询本机地址 */
			InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
			System.out.println(inetAddress1);
			
			InetAddress inetAddress3 = InetAddress.getByName("localhost");
			System.out.println(inetAddress3);
			
			InetAddress inetAddress4 = InetAddress.getLocalHost();
			System.out.println(inetAddress4);
			
			/* 查询网站IP地址 */
			InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
			System.out.println(inetAddress2);
			
			/* 常用方法 */
			System.out.println(inetAddress2.getAddress());//字节 一组地址
			System.out.println(inetAddress2.getCanonicalHostName());//规范IP名字
			System.out.println(inetAddress2.getHostAddress());//IP
			System.out.println(inetAddress2.getHostName());//域名或者自己电脑名字
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

端口

端口表示计算机上的一个程序的进程

  • 不同的进程有不同的端口号 用来区分软件

  • 被规定0-65535

  • TCP,UDP : 65535*2 单个协议下,端口号不能冲突

  • 端口分类

    • 共有端口 0-1023

      • HTTP:80
      • HTTPS:443
      • FTP:21
      • Telent: 23
    • 程序注册端口:1024-49151,分配用户或者程序

      • Tomcat:8080
      • MySQL: 3306
      • Oracle: 1521
    • 动态、私有:49152-65535

      netstat -ano #查看所有端口
      netstat -ano|findstr "5900" #查看指定端口
      tasklist|findstr "8696" # 查看指定端口的进程
      

重要通信协议

  • TCP:用户传输协议
    • 连接 打电话
    • 连接 稳定
    • 三次握手:最少需要三次连接来保证稳定连接
    • 四次挥手:最少需要四次连接来保证稳定断开
    • 客户端、服务端
    • 传输完成、释放连接 效率低
  • UDP:用户数据报协议
    • 不连接 发短信
    • 客户端、服务端:没有明确的界限
    • 不管有没有准备好都可以发送

TCP实现聊天

  • Client客户端
    1. 连接服务器Socket
    2. 发送消息
package Address;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/* 客户端 */
public class TCPClient1 {

	public static void main(String[] args) {
		Socket socket = null;
		OutputStream os = null;
		
		try {
			/* 1.要知道服务器的地址、端口号 */
			InetAddress serverIP = InetAddress.getByName("127.0.0.1");
			int port = 9999;
			
			/* 2.创建一个socket连接 */
			socket = new Socket(serverIP,port);
			
			/* 3.发送消息 */
			os = socket.getOutputStream();
			
			os.write("HELLO".getBytes());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				
		}
		
	}
	
}

  • Server服务器
    1. 建立服务的端口ServerSocket
    2. 等待用户的连接accept
    3. 接收用户的消息
package Address;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/* 服务端 */
public class TCPServer1 {

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		Socket socket = null;
		InputStream is = null;
		ByteArrayOutputStream baos = null;
		
		try {
			/* 1.需要有一个地址 */
			serverSocket = new ServerSocket(9999);
			
			/* 等待客户端连接过来 */
			socket = serverSocket.accept();
			
			/* 3.读取客户端消息 */
			is = socket.getInputStream();
			
			/* 管道流 */
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len;
			while((len = is.read(buffer)) != -1) {
				baos.write(buffer,0,len);
			}
			
			System.out.println(baos.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			/* 关闭资源 */
			if(baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(serverSocket != null) {
				try {
					serverSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}		
			
		}
		
	}
}

你可能感兴趣的:(网络,tcp/ip,java)