19.网络编程

1.UDP实现收发

package cn.mldn.sxt;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/*
 * udp实现接收端:
 * 	1.定义接收端new DatagramSocket(6666);
 * 	2.准备包裹,接收数据
 * 	3.接收
 * 	4.关闭
 */
public class Receive02 {
	public static void main(String[] args) throws IOException {
		System.out.println("------------接收端-----------");
		//1.定义接收端new DatagramSocket(6666);
		DatagramSocket receive=new DatagramSocket(6666);
		//2.准备包裹,接收数据
		byte[] car=new byte[1024*60];
		//DatagramPacket(byte[] buf, int length)
		DatagramPacket packet=new DatagramPacket(car,car.length);
		//3.接收
		receive.receive(packet);
		//处理数据
		byte[] arr=packet.getData();
		int len=packet.getLength();
		System.out.println(new String(arr,0,len));
		
		//4.关闭
		receive.close();
	}
}

package cn.mldn.sxt;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/*
 * 面向Socket编程
 * 	tcp和udp不同的协议实现不一样
 * 	
 * 	udp: 非面向连接的 协议简单,开销小,效率高   不安全 一般不超过60k   基于字节数组
 * 	tcp: 面向连接的 效率低 安全 基于3次握手  1:发送请求	2.回应	3.发送数据  基于io流
 * 
 * DatagramSocket
 * DatagramPacket
 * 
 * udp实现发送端:
 * 	1.使用  DatagramSocket(int port) 定义发送端
 * 	2.准备数据
 * 	3.数据打包 DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) 
 * 	4.发送数据 void send(DatagramPacket p)  从此套接字发送数据报包。 
 * 	5.资源关闭 
 */
public class Send01 {
	public static void main(String[] args) throws IOException {
		System.out.println("------------发送端-----------");
		//	1.使用  DatagramSocket(int port) 定义发送端
		DatagramSocket send=new DatagramSocket(8888);
		// 2.准备数据
		byte[] arr="蜡笔小新".getBytes();
		//3.打包
		DatagramPacket data=new DatagramPacket(arr,0, arr.length, new InetSocketAddress("localhost",6666));
		//4.发送数据 
		send.send(data);
		//5.关闭
		send.close();
	}
}

2.生产者消费者案例

package cn.mldn.dbc;

public class JavaDemo {
	/*
	 * * 多线程中,一个线程打印1~52的数字,一个线程打印A~Z字母,控制线程输出结果为12A34B...52Z
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Print p = new Print();
		new Thread(new Nunber(p)).start();
		new Thread(new Zimo(p)).start();
	}

}

class Nunber implements Runnable {
	Print print;

	public Nunber(Print print) {
		this.print = print;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		print.printNumber();
	}

}

class Zimo implements Runnable {
	Print print;

	public Zimo(Print print) {
		this.print = print;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		print.printZimo();
	}

}

class Print {
	boolean flag = false;

	public synchronized void printNumber() {
		
		for (int x = 1; x <= 52; x++) {
			
			System.out.print(x);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (x % 2 == 0) {
				
				flag = true;
				this.notify();
				
			}
			if (flag == true) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			
		}
	}

	public synchronized void printZimo() {
		
		for (int x = 65; x <= 90; x++) {
			if (flag == false) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.print((char) x);
			flag = false;
			this.notify();
			

		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值