UDP 类似发短息 ,寄快递, 对方可收可不收
package JUC.UDP;
import java.io.IOException;
import java.net.*;
public class DatagramSocketTest {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
String msg = "你好";
//要发送的内容
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, InetAddress.getByName("localhost"),9999);
datagramSocket.send(datagramPacket);//发送
datagramSocket.close();
}
}
package JUC.UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class DatagramServerTest {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(9999);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
datagramSocket.receive(datagramPacket);//接收
datagramSocket.close();
}
}
循环发送
package JUC.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSend {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(9090);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = bufferedReader.readLine();
DatagramPacket datagramPacket = new DatagramPacket(data.getBytes(),0,data.length(),new InetSocketAddress("localhost",6666));
datagramSocket.send(datagramPacket);
if (data.equals("bye")){
break;
}
}
datagramSocket.close();
}
}
package JUC.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceive {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(6666);
while (true){
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData();
String s = new String(data, 0, data.length);
System.out.println(s);
if (s.equals("bye")){
break;
}
}
datagramSocket.close();
}
}
多线程实现UDP方式聊天
package JUC.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class UdpSend implements Runnable{
DatagramSocket datagramSocket = null;
BufferedReader bufferedReader = null;
private int toProt;
private int prot;
private String hostname;
@Override
public void run() {
while (true){
String data = null;
try {
data = bufferedReader.readLine();
DatagramPacket datagramPacket = new DatagramPacket(data.getBytes(),0,data.length(),new InetSocketAddress(this.hostname,this.prot));
datagramSocket.send(datagramPacket);
if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
datagramSocket.close();
}
public UdpSend(int toProt, int prot, String hostname) {
this.toProt = toProt;
this.prot = prot;
this.hostname = hostname;
try {
datagramSocket = new DatagramSocket(this.toProt);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
}
package JUC.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceive implements Runnable {
DatagramSocket datagramSocket = null;
private int prot;
private String sayName;
@Override
public void run() {
try {
while (true){
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData();
String s = new String(data, 0, data.length);
System.out.println(this.sayName +":"+s);
if (s.equals("bye")){
break;
}
}
datagramSocket.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public UdpReceive(int prot, String sayName) {
this.prot = prot;
this.sayName = sayName;
try {
datagramSocket = new DatagramSocket(this.prot);
} catch (SocketException e) {
e.printStackTrace();
}
}
}
package JUC.chat;
public class TalkSendTest {
public static void main(String[] args) {
new Thread(new UdpSend(7777,9999,"localhost")).start();
new Thread(new UdpReceive(8888,"tom")).start();
}
}
public class TalkReceiveTest {
public static void main(String[] args) {
new Thread(new UdpSend(5555,8888,"localhost")).start();
new Thread(new UdpReceive(9999,"honey")).start();
}
}
|