发短信:不用连接,需要知道对方地址
文章目录
- UDP发送消息
- 实现互相聊天
- UDP多线程实现聊天
UDP发送消息
UDP传输
我们要用UDP传输数据时,怎么用Socket建立连接呢?
DatagramSocket与DatagramPacket
建立发送端,接收端。
建立数据包。
调用Socket的发送接收方法。
关闭Socket。
发送端与接收端是两个独立的运行程序。
package com.ayv.try03;import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;public class UdpClientDemo01 {public static void main(String[] args) throws Exception {//建立一个socketDatagramSocket socket = new DatagramSocket();//建一个包String msg="hello 服务器";InetAddress localhost = InetAddress.getByName("localhost");int port=9090;//数据,数据的长度起始,要发给谁DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length);//发送包socket.send(packet);//关闭流socket.close();}
}
接收端
package com.ayv.try03;import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class UdpServeDemo01 {public static void main(String[] args) throws Exception {//开放端口DatagramSocket socket = new DatagramSocket(9090);//接收数据包byte[] buffer = new byte[1024];DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);socket.receive(packet);//阻塞接收//输出包裹System.out.println(packet.getAddress().getHostAddress());System.out.println(new String(packet.getData(),0, packet.getLength()));//关闭链接socket.close();}
}
care:端口
实现互相聊天
package com.ayv.chat;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;public class UdpSenderDamo01 {public static void main(String[] args) throws Exception {DatagramSocket socket = new DatagramSocket(8888);//准备数据:控制台读取System.inBufferedReader 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();}
}
package com.ayv.chat;import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class UdpReceive {public static void main(String[] args) throws Exception {DatagramSocket socket = new DatagramSocket(6666);while(true){//准备接收包裹byte[] container = new byte[1024];DatagramPacket packet = new DatagramPacket(container, 0, container.length);socket.receive(packet);//阻塞式接收包裹//输出byte[] data = packet.getData();String receiveDate = new String(data, 0, data.length);System.out.println(receiveDate);//断开链接if(receiveDate.equals("bye")){break;}}socket.close();}
}
UDP多线程实现聊天
通过多线程实现聊天程序,我们就要开启两个线程,一个接收数据,一个发送数据,这样我就可以实现在一个窗口发送和接收数据了
package com.ayv.chat;import sun.jvm.hotspot.debugger.Address;public class TalkSyudent {public static void main(String[] args) {//开启两个线程new Thread(new TalkSend(7777,"localhost",9999)).start();new Thread(new TalkReceive(8888,"老师")).start();}
}
package com.ayv.chat;public class TalkTeacher {public static void main(String[] args) {new Thread(new TalkSend(5555,"localhost",8888)).start();new Thread(new TalkReceive(9999,"学生")).start();}
}
package com.ayv.chat;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class TalkReceive implements Runnable{DatagramSocket socket=null;private int port;private String msgForm;public TalkReceive(int port, String msgForm) {this.port=port;this.msgForm=msgForm;try {socket = new DatagramSocket(port);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void run() {while(true){try {byte[] container = new byte[1024];DatagramPacket packet = new DatagramPacket(container, 0, container.length);socket.receive(packet);byte[] data = packet.getData();String receiveData = new String(data, 0, data.length);System.out.println(msgForm+":"+receiveData);if(receiveData.equals("bye")){break;}} catch (IOException e) {e.printStackTrace();}}socket.close();}
}
package com.ayv.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 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);reader=new BufferedReader(new InputStreamReader(System.in));} catch (Exception e) {e.printStackTrace();}}@Overridepublic void run() {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 (Exception e) {e.printStackTrace();}}socket.close();}
}