/*
UDP:
1.将数据及源和目的封装成数据包中,不需要建立连接
2.每个数据包的大小限制在64k内
3.因无连接,是不可靠协议
3.不需要建立连接,速度快
TCP:
1.建立连接,形成传输数据的通道
2.在连接中进行大数据传输
3.通过三次握手完成连接,是可靠协议
4.必须建立连接,效率会稍低
Socket:
Socket就是为网络服务提供的一种机制
通信的两端都有Socket
网络通信其实就是Socket间的通信
数据在两个Socket间通过IO传输
*/
/*
需求:通过udp传输方式,将一段文字数据发送出去。
思路:
1.建立udp的socket服务。
2.提供数据,并将数据封装到数据包中。
3.通过socket服务的发送功能,将数据包发出去。
4.关闭资源。
*/
import java.io.IOException;
import java.net.*;
class UdpSend{
public static void main(String[] args){
send();
}
public static void send() {
//创建udp的socket服务,通过DatagramSocket对象。
DatagramSocket ds = null;
try {
ds = new DatagramSocket();//可以自定义端口发送,默认是随机端口
} catch (SocketException e) {
e.printStackTrace();
}
//确定数据,并封装成数据包。 DatagramPacket(byte[] buf,int length,InetAddress address,int port)
byte[] data = "udp 哥们来了".getBytes();
DatagramPacket dp = null;
try {
dp = new DatagramPacket(data,data.length,InetAddress.getByName("192.168.1.254"),1000);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
//通过socket服务,将已有的数据包发送出去,通过send方法。
try {
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
//关闭资源。
ds.close();
}
}
//发送端和接收端是两个独立的
/*
需求:
定义一个应用程序,用于接收udp协议传输的数据并处理。
思路:
1.定义udp的socket服务。通常都会监听一个端口,其实就是给这哥接收网络应用程序定义标识。
方便于明确哪些数据过来该应用程序可以处理。
2.定义一个数据包,因为要存储接收到的字节数据。
因为数据包对象中有更多功能可以提取字节数据中的不同信息。
3.通过socket服务的receive方法将受到的数据存入已定义好的数据包中。
4.通过数据包的特有功能,将这些不同的数据取出。
5.关闭资源。
*/
import java.net.*;
class UdpReceive{
public static void main(String[] args){
receive();
}
public static void receive(){
//创建udp的socket服务,建立端点。
DatagramSocket ds = null;
try {
ds = new DatagramSocket(1000);
} catch (SocketException e) {
e.printStackTrace();
}
//定义数据包,用于存储数据。
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
//通过socket服务的receive方法将收到的数据存入数据包中。
try {
ds.receive(dp);//阻塞式方法
} catch (IOException e) {
e.printStackTrace();
}
//通过数据包的方法获取其中的数据。
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+"::"+data+"::"+port);//port是系统随机产生的。
//关闭资源
ds.close();
}
}
/*---------------------------------------------------------------------*/
/*
udp-键盘录入方式数据
*/
import java.net.*;
import java.io.*;
class UdpSend{
public static void main(String[] args){
send();
}
public static void send(){
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while((line = br.readLine()) != null)
{
if("886".equals(line))
break;
byte[] buffer = line.getBytes();
DatagramPacket dp = null;
try {
dp = new DatagramPacket(buffer,buffer.length,InetAddress.getByName("192.168.1.254"),10001);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
try {
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
ds.close();
}
}
import java.io.IOException;
import java.net.*;
class UdpReceive{
public static void main(String[] args){
receive();
}
public static void receive(){
DatagramSocket ds = null;
try {
ds = new DatagramSocket(10001);
} catch (SocketException e) {
e.printStackTrace();
}
while(true){
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
try {
ds.receive(dp);
} catch (IOException e) {
e.printStackTrace();
}
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
}
/*-----------------------------------------------------------------------------*/
/*
多线程实现UDP聊天
编写一个聊天程序。
有收数据的部分,有发数据的部分。
这两部分需要同时执行,这就需要用到多线程技术。
因为收和发动作是不一致的,所以要定义两个run方法。
而且这两个方法要封装到不同的类中。
*/
import java.net.*;
import java.io.*;
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line))
break;
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer,buffer.length,InetAddress.getByName("192.168.1.255"),10025);
ds.send(dp);
}
}catch (Exception e){
throw new RuntimeException("发送端失败!");
}
}
}
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
while(true){
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}catch (Exception e){
throw new RuntimeException("接收端失败!");
}
}
}
class ChatDemo{
public static void main(String[] args){
DatagramSocket sendSocket = null;
try {
sendSocket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
DatagramSocket receiveSocket = null;
try {
receiveSocket = new DatagramSocket(10025);
} catch (SocketException e) {
e.printStackTrace();
}
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receiveSocket)).start();
}
}