黑马程序员-网络编程 udp传输

------- android培训java培训、期待与您交流! ----------
IP地址:网络中设备的标识。
本地回环地址:127.0.0.1 主机名:localhost
端口:用于标识进程的逻辑地址,有效端口:0-65535,其中0-1024系统使用或是保留端口
传输协议:通讯规则 常见协议:TCP/IP UDP


OSI参考模型和TCP/IP参考模型:


InetAddress:表示IP地址。
InetAddress i = InetAddress.getByName(String host);
InetAddress[] ias = InetAddress.getAllByName(String host);
i.getHostAddress():获取IP地址
i.getHostName():获取主机名。需要在网络上和对应IP地址有记录。


UDP:User Datagram Protocol 是OSI参考模型中的一种无连接的传输层通信协议。
1.需要将数据、源和目的封装成数据包
2.每个数据包的大小限制在64k内
3.是不可靠的协议,提供面向事务的简单不可靠信息传送服务
4.不需要建立连接,速度很快
TCP:Transmission Control Protocol 是一种面向连接的传输层通信协议。
1.需要建立连接,形成传输数据的通道
2.通过三次握手完成连接确认,因此是可靠的协议
3.可以进行大数据的传输
4.因为必须建立连接,效率较低




Socket:为网络服务提供的一种机制
通信两端都有Socket,网络通信实际就是Socket间的通信
数据在Socket间通过IO流进行传输


udp传输协议传输数据:
import java.net.*;
/*
定义一个socket服务,用于发送udp协议传输的数据
1.创建udp服务。通过DatagramSocket对象
2.确定数据,并封装成数据包
3.通过socket服务的send方法将已有数据包发送出去
4.关闭资源
*/
class UdpSend 
{
	public static void main(String[] args) throws Exception
	{
		//1.创建udp服务。通过DatagramSocket对象
		DatagramSocket ds = new DatagramSocket();


		//2.确定数据,并封装成数据包


		byte[] buf = "udp is coming".getBytes();
		DatagramPacket dp = 
			new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.100"),10000);


		//3.通过socket服务的send方法将已有数据包发送出去
		ds.send(dp);


		//4.关闭资源
		ds.close();
	}
}

/*
 定义一个应用程序,用于接收udp协议传输的数据包
 1.定义udpsocket服务,通常会监听一个端口,就是给应用程序定义数字标识。
 2.定义一个数据包,存储接收到的字节数据。用数据包对象中的方法提取字节数据的不同数据信息
 3.通过socket服务的receive收到的数据存入定义好的数据包中
 4.提取接收的数据包中的不同数据。
 5.关闭资源
*/


class UdpReceive
{
	public static void main(String[] args)throws Exception
	{
		DatagramSocket ds = new DatagramSocket(10000);


		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);


		ds.receive(dp);//阻塞式方法 


		String ip = dp.getAddress().getHostAddress();


		String data = new String(dp.getData(),0,dp.getLength());


		int port = dp.getPort();


		System.out.println(ip+":"+data+":"+port);
	}
}

dos窗口udp聊天:需要两个dos窗口同时开启
import java.net.*;
import java.io.*;
class  UdpSend
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket ds = new DatagramSocket();


		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(System.in));


		String line = null;


		while((line=bufr.readLine())!=null)
		{
			if("886".equals(line))
				break;
			byte[] buf = line.getBytes();
			DatagramPacket dp = 
				new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.100"),10001);
			ds.send(dp);
		}
		ds.close();
	}
}


class  UdpReceive
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket ds = new DatagramSocket(10001);


		while(true)
		{
			byte[] buf = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf,buf.length);


			ds.receive(dp);
			
			String ip = dp.getAddress().getHostAddress();
			String data = new String(buf,0,dp.getLength());
			int port = dp.getPort();


			System.out.println(ip+":"+data+":"+port);
		}
	}
}

多线程udp传输通信:
import java.net.*;
import java.io.*;
class Send implements Runnable
{
	private DatagramSocket ds;
	Send(DatagramSocket ds)
	{
		this.ds = ds;
	}
	public void run()
	{
		try
		{
			BufferedReader bufr = 
				new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while((line=bufr.readLine())!=null)
			{
				if("886".equals(line))
					break;
				byte[] buf = line.getBytes();
				DatagramPacket dp = 
					new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.100"),10002);
				ds.send(dp);
			}
			bufr.close();
			ds.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException("");
		}
	}
}
class Receive implements Runnable
{
	private DatagramSocket ds;
	Receive(DatagramSocket ds)
	{
		this.ds = ds;
	}
	public void run()
	{
		try
		{
			while(true)
			{
				byte[] buf = new byte[1024];
				DatagramPacket dp = new DatagramPacket(buf,buf.length);


				ds.receive(dp);
				String ip = dp.getAddress().getHostAddress();
				String data = new String(dp.getData(),0,dp.getLength());
				int port = dp.getPort();


				System.out.println(ip+":"+data+":"+port);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException("");
		}
		
	}
}
class  UdpThreadDemo
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket dsSend = new DatagramSocket();
		DatagramSocket dsRece = new DatagramSocket(10002);


		new Thread(new Send(dsSend)).start();
		new Thread(new Receive(dsRece)).start();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值