需求:向本地发送信息,自己发送,自己接收
1、发送Send
- 创建DatagramSocket, 随机端口号
- 创建DatagramPacket, 指定数据, 长度, 地址, 端口
- 使用DatagramSocket发送DatagramPacket
- 关闭DatagramSocket
package com.soar.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Demo1_Send {
/*
* 1.发送Send
*/
public static void main(String[] args) throws Exception {
String str = "Hello Soar_Sir!"; //向自己发送一句字符串
DatagramSocket socket = new DatagramSocket(); //创建Socket相当于创建码头
DatagramPacket packet = //创建Packet相当于集装箱,参数分别代表数据, 长度, 地址, 端口
new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),6666);
socket.send(packet); //发货,将数据发出去
socket.close(); //关闭码头
}
}
2、接收Receive
- 创建DatagramSocket, 指定端口号
- 创建DatagramPacket, 指定数组, 长度
- 使用DatagramSocket接收DatagramPacket
- 关闭DatagramSocket
- 从DatagramPacket中获取数据
package com.soar.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Demo1_Receive {
/*
* 2.接收Receive
*/
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666); //创建Socket,相当于创建码头
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //创建Packet,相当于创建集装箱
socket.receive(packet); //接货,接收数据
byte[] arr = packet.getData(); //获取所有数据,包括空的和无效的数据
int len = packet.getLength(); //获取有效的直接个数
System.out.println(new String(arr,0,len));
socket.close();
}
}
3、找到这两个类所在的bin目录
按shift+鼠标右键
注意,一定在bin目录下打开!!!切记不能在com目录下打开
第一步:start (再次打开另一个dos界面)
第二步:java com.soar.socker.Demo1_Receive(包名不能忘了写,先接收再发送)
第三步:java com.soar.socker.Demo1_Send(先接收再发送)
第四步:在接收方看到发送的Socket信息
需求:将以上内容进行优化,使其可以不断从键盘录入字符串并将其发送给本地(自己)
Demo2_Send.java
package com.soar.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Demo2_Send {
/*
* 1.发送Send
* 创建DatagramSocket, 随机端口号
* 创建DatagramPacket, 指定数据, 长度, 地址, 端口
* 使用DatagramSocket发送DatagramPacket
* 关闭DatagramSocket
*/
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
DatagramSocket socket = new DatagramSocket(); //创建Socket相当于创建码头
while(true){
String line = sc.nextLine();
if("quit".equals(line)){
break;
}
DatagramPacket packet = //创建Packet相当于集装箱
new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("127.0.0.1"),6666);
socket.send(packet); //发货,将数据发出去
}
socket.close(); //关闭码头
}
}
Demo2_Receive.java
package com.soar.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Demo2_Receive {
/*
* 2.接收Receive
* 创建DatagramSocket, 指定端口号
* 创建DatagramPacket, 指定数组, 长度
* 使用DatagramSocket接收DatagramPacket
* 关闭DatagramSocket
* 从DatagramPacket中获取数据
*/
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666); //创建Socket,相当于创建码头
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //创建Packet,相当于创建集装箱
while(true){
socket.receive(packet); //接货,接收数据
byte[] arr = packet.getData(); //获取所有数据,包括空的和无效的数据
int len = packet.getLength(); //获取有效的直接个数
String ip = packet.getAddress().getHostAddress(); //获取ip地址
int port = packet.getPort(); //获取端口号
System.out.println(ip + ":" + port + ":" +new String(arr,0,len));
}
}
}
使用多线程将其优化,使发送和接收在同一个界面
package com.soar.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Demo3_MoreThread {
/**
* 多线程优化Receive和Send
*/
public static void main(String[] args) {
new Receive().start();
new Send().start();
}
}
class Receive extends Thread {
public void run() {
try {
DatagramSocket socket = new DatagramSocket(6666); //创建Socket相当于创建码头
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);//创建Packet相当于创建集装箱
while(true) {
socket.receive(packet); //接货,接收数据
byte[] arr = packet.getData(); //获取数据
int len = packet.getLength(); //获取有效的字节个数
String ip = packet.getAddress().getHostAddress(); //获取ip地址
int port = packet.getPort(); //获取端口号
System.out.println(ip + ":" + port + ":" + new String(arr,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Send extends Thread {
public void run() {
try {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
DatagramSocket socket = new DatagramSocket(); //创建Socket相当于创建码头
while(true) {
String line = sc.nextLine(); //获取键盘录入的字符串
if("quit".equals(line)) {
break;
}
DatagramPacket packet = //创建Packet相当于集装箱
new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
socket.send(packet); //发货,将数据发出去
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}