网络编程

网络编程

网络编程的概念:

通过使用套接字来达到进程间通信目的编程就是网络编程。

什么是网络?

通过多台计算机构建的一个大网

  1. 为什么需要网络?

    多台计算机通过网络连接,进行通信,数据交互、数据共享。

    去除地域限制、拉近人与人之间的距离 。

  2. 网络通信的基石:

    三大基石

    ip:标示计算机

    协议:通信规则

    端口:定位应用程序

网络编程的3要素
ip(ipv4、ipv6)

网络中设备的标识,是计算机的唯一标识。

为了方便我们对IP地址的获取和操作,java提供了一个类InetAddress 供我们使用。

协议:

通讯的规则

常见协议:TCP,UDP

端口:

用于标识进程的逻辑地址,不同进程的标识

端口占2个字节从0-65536共有65536个、其中0-1024之间的端口是系统保留端口 
下面列举几种服务常用的端口号:
TOMCAT,默认端口号为8080
HTTP协议代理服务器常用端口号:80
MYSQL数据库连接的默认端口号是3306
FTP(文件传输)协议代理服务器常用端口号:21
SSH(安全登录)、SCP(文件传输)、端口号重定向,默认的端口号为22/tcp
InetAdderess
public static void main(String[] args) throws UnknownHostException {	
		//返回本地主机的地址(InetAddress对象)。
		InetAddress in = InetAddress.getLocalHost();//InetAddress没有对外提供构造器
		//获取此IP地址的主机名。
		System.out.println("主机名"+in.getHostName());
		//返回文本表示中的IP地址字符串。
		System.out.println("本地ip地址"+in.getHostAddress());
		//返回此InetAddress 对象的原始IP地址。
		System.out.println("本地ip地址"+Arrays.toString(in.getAddress()));
		//根据主机的名称,根据系统上配置的名称服务返回其IP地址数组。
		InetAddress[] ips = InetAddress.getAllByName("www.taobao.com");
		for(InetAddress ip:ips) {
			System.out.println(ip.getHostName()+"=="+ip.getHostAddress());
		}
	}
运行结果:
主机名DESKTOP-J1EMKR3
本地ip地址172.20.10.8
本地ip地址[-84, 20, 10, 8]
www.taobao.com==183.216.168.219
www.taobao.com==183.216.168.220

Tcp(安全)

tcp:

1: 客户端和服务器开启是有先后顺序的,先启动服务器,再启动客户端 

2:socket对象: 

    1: 客户端称之为socket对象 

    2:服务器称之为serverSocket对象 

    3:客户端在创建时需要指定ip以及端口 

    4:服务器创建时需要指定监听端口 

    5:数据的收发都是通过流完成的 所有数据信息都可以通过socket获取输入/输出流 

    getInputStream/getOutputStream 

	6: 对于服务器而言,获取客户端的socket对象 accept方法 (阻塞方法) 

3:c/s模式下的通信方式: 

client: 

//创建socket对象(ip,端口) 

//获取流对象(根据需求创建输入输出流) 

//发送/接受数据信息 

//关闭 

server: 

//创建serversocket对象(端口) 

//通过accept方法获取客户端socket对象 

//获取流对象(你要干嘛?) 

//发送/接受数据信息 

//关闭 
Server:
public class Server {
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(5418);
		Socket client = server.accept();
		InputStream in = client.getInputStream();		
		byte[] bts = new byte[1024];
		int length = in.read(bts);
		System.out.println(new String(bts,0,length));
		in.close();
		client.close();
		server.close();
	}
}

client:
public class Client {
	public static void main(String[] args) throws IOException {
		Socket client = new Socket("192.168.101.168",5418);	
		OutputStream out = client.getOutputStream();
		String str = "vegee天下第一";
		out.write(str.getBytes());
		out.close();
	}
}

Udp(不安全)
UDP发送信息: 基于发送方和接受方 都是通过DatagrameSocket对象去指定 
1: 启动发送方不会报错、不安全,没有三次握手协议,速率会更快一些
sender:
public class Send {
	public static void main(String[] args) throws IOException {	
		//1.创建发送方对象
		DatagramSocket sender = new DatagramSocket(10001);
		//2:构建发送方的数据包对象 :buf发送数据的字节数组 length发送数据的长度 ip接收方的ip port接收方的port端口号
		String str = "udp协议demo";
		byte[] buf = str.getBytes();
		int length = buf.length;
		int port = 10086;
		InetAddress ip = InetAddress.getLocalHost();	
		//3.发送消息
		sender.send(new DatagramPacket(buf,length,ip,port));
		//4.关闭
		sender.close();
	}
}

reciever:
public class Reciever {
	public static void main(String[] args) throws IOException {
		//1.创建接收方对象
		DatagramSocket rec = new DatagramSocket(10086);
		//2.创建接收数据包对象
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);
		//3:接受发送方发送数据信息
		rec.receive(dp);//会将数据信息填充在DatagramPacket对象中 阻塞方法
		
		 //4:分析数据
		InetAddress ip = dp.getAddress();	
		byte[] rbuf = dp.getData();
		int length = dp.getLength();
		String str = new String(rbuf,0,length);
		int port = rec.getPort();
		System.out.println(ip.getHostName()+"  "+"data:"+str+"端口号:"+port);
		//5.关闭
		rec.close();
	}
}
Url和UrlConnection
public class Test {
	public static void main(String[] args) throws IOException {
			URL url = new URL("https://www.sina.com.cn/");
			InputStream is = url.openStream();
			byte[] buf = new byte[1024];
			int length = is.read(buf);
			System.out.println(new String(buf,0,length));
	}
}
public class Test2 {
	public static void main(String[] args) throws IOException {
		//创建URL对象
		URL url = new URL("https://www.sina.com.cn/");
		//通过资源对象获取到连接该资源的链接对象
		URLConnection conn = url.openConnection();
		//打开链接 
		conn.connect();//建立连接
		//获取连接的信息
		System.out.println(conn.getDate());
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
		StringBuilder sb = new StringBuilder(); 
		String str = ""; 
		while((str=reader.readLine())!=null) { 
			sb.append(str); 
			sb.append("\r\n"); 
			}
		System.out.println(sb.toString()); 
		}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值