UDP
Texting, no connection, need to know the address of the other party.
The sender
package com.zr.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
// No need to connect to the server
public class UdpSendDemo01 {
public static void main(String[] args) throws Exception {
// Create a socket
DatagramSocket socket = new DatagramSocket();
/ / key
String msg = "Hello?";
// To whom
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
/ / send the packagesocket.send(packet); socket.close(); }}Copy the code
The receiving end
package com.zr.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpRecelveDemo01 {
public static void main(String[] args) throws Exception {
// Open the port
DatagramSocket socket = new DatagramSocket(9090);
// Receive packets
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
// block reception
socket.receive(packet);
System.out.println(packet.getAddress());
System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); }}Copy the code
UDP chat implementation
Circular message sending
package com.zr.udpChat;
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 UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
// Prepare data for reading from the console
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost".6666));
socket.send(packet);
if (data.equals("bye")) {break; } } socket.close(); reader.close(); }}Copy the code
package com.zr.udpChat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
// Prepare to receive data
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);// Block to receive packages
socket.receive(packet);
// Disconnect the connection
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
if (receiveData.equals("bye")) {break; } } socket.close(); }}Copy the code
Online consultation: Two people can be the sender and both can be the receiver
package com.zr.udpChat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch(SocketException e) { e.printStackTrace(); }}@Override
public void run(a) {
while (true) {
try {
// Prepare to receive data
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);// Block to receive packages
socket.receive(packet);
// Disconnect the connection
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("bye")) {break; }}catch(IOException e) { e.printStackTrace(); } } socket.close(); }}Copy the code
package com.zr.udpChat;
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 TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
// Prepare data for reading from the console
reader = new BufferedReader(new InputStreamReader(System.in));
} catch(SocketException e) { e.printStackTrace(); }}@Override
public void run(a) {
while (true) {
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if (data.equals("bye")) {break; }}catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
try {
reader.close();
} catch(IOException e) { e.printStackTrace(); }}}Copy the code
package com.zr.udpChat;
public class TalkStudent {
public static void main(String[] args) {
// Start two threads
new Thread(new TalkSend(7777."localhost".9999)).start();
new Thread(new TalkReceive(8888."Teacher")).start(); }}Copy the code
package com.zr.udpChat;
public class TalkTeacher {
public static void main(String[] args) {
// Start two threads
new Thread(new TalkSend(6666."localhost".8888)).start();
new Thread(new TalkReceive(9999."Students")).start(); }}Copy the code
Results: