网络编程-UDP



Socket

Socket就是为网络服务提供的一种机制

通信的两端都有Socket

网络通信其实就是Socket间的通信

数据在两个Socket间通过IO传输

 

 

 

UDP

将数据及源和目的的封装成数据包中,不需要建立连接

每个数据包的大小限制在64k

因无连接,是不可靠的协议

不需要建立连接,速度快

 

Udp的使用:

 

例子:通过udp实现网络通讯(实现两台机的对话)

 

发送端步骤:

1.建立udpsocket服务。

2.提供数据,并将数据封装到数据包中

3.通过socket服务的发送功能,将数据包发出去

4.关闭资源

 

接收端步骤:

1.定义udpsocket服务

2.定义一个数据包,用来要存储接收到的字节数据(数据包对象中有更多的功能提取字节数据中的不同数据信息)

3.通过socket服务的reseive方法将收到的数据存储入已定义好的数据包中

4.通过数据包对象特有的功能,将这些不同的数据取出

5.关闭资源

 

 

A机:

 

发送类(UdpSend.java

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpSend implements Runnable {
	private static DatagramSocket ds;

	public UdpSend(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
			InputStreamReader in = new InputStreamReader(System.in);
			BufferedReader buffer = new BufferedReader(in);
			String say = null;
			while ((say = buffer.readLine()) != null) {
				byte[] buf = say.getBytes();
				DatagramPacket dp = new DatagramPacket(buf, buf.length,
						InetAddress.getByName("192.168.144.1"), 10002);
				ds.send(dp);
			}
		} catch (IOException e) {
			System.out.println("发送失败");
		}
	}

}


 

 

接收类(UpdRece.java

 

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

public class UpdRece implements Runnable {
	private static DatagramSocket ds;

	public UpdRece(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
			// 创建udp socket,建立端点
			// ds = new DatagramSocket(10000);
			while (true) {
				// 定义数据包,用于存储数据
				byte[] buf = new byte[1024];
				DatagramPacket dp = new DatagramPacket(buf, buf.length);
				// 通过服务的receive方法将收到数据存入数据包中
				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 + ":" + port + "   say:\n " + data);
				// 关闭资源
				// ds.close();
			}
		} catch (Exception e) {
			System.out.println("接收失败");
		}
	}

}


 

3.入口类

 

import java.net.DatagramSocket;
import java.net.SocketException;

public class IntentClient {

	/**
	 * @param args
	 * @throws SocketException
	 */
	public static void main(String[] args) throws Exception {
		DatagramSocket sendSocket = new DatagramSocket();
		DatagramSocket receSocket = new DatagramSocket(10002); // 接收10002端口的数据
		new Thread(new UdpSend(sendSocket)).start();
		new Thread(new UpdRece(receSocket)).start();

	}

}


 

 

 

B

 

 

发送类(UdpSend.java

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpSend implements Runnable {
	private static DatagramSocket ds;

	public UdpSend(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
			InputStreamReader in = new InputStreamReader(System.in);
			BufferedReader buffer = new BufferedReader(in);
			String say = null;
			while ((say = buffer.readLine()) != null) {
				byte[] buf = say.getBytes();
				DatagramPacket dp = new DatagramPacket(buf, buf.length,
						InetAddress.getByName("192.168.144.1"), 10002);
				ds.send(dp);
			}
		} catch (IOException e) {
			System.out.println("发送失败");
		}
	}

}


 

 

接收类(UpdRece.java

 

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

public class UpdRece implements Runnable {
	private static DatagramSocket ds;

	public UpdRece(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
			// 创建udp socket,建立端点
			// ds = new DatagramSocket(10000);
			while (true) {
				// 定义数据包,用于存储数据
				byte[] buf = new byte[1024];
				DatagramPacket dp = new DatagramPacket(buf, buf.length);
				// 通过服务的receive方法将收到数据存入数据包中
				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 + ":" + port + "   say:\n " + data);
				// 关闭资源
				// ds.close();
			}
		} catch (Exception e) {
			System.out.println("接收失败");
		}
	}

}


 

4.入口类

 

import java.net.DatagramSocket;
import java.net.SocketException;

public class IntentClient {

	/**
	 * @param args
	 * @throws SocketException
	 */
	public static void main(String[] args) throws Exception {
		DatagramSocket sendSocket = new DatagramSocket();
		DatagramSocket receSocket = new DatagramSocket(10002); // 接收10002端口的数据
		new Thread(new UdpSend(sendSocket)).start();
		new Thread(new UpdRece(receSocket)).start();

	}

}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值