隨著現(xiàn)在網(wǎng)絡信息越來越發(fā)達,越來越多的行業(yè)都支持在線咨詢,可以更好地幫助客戶解決問題等。下面,將使用Java多線程,基于UDP協(xié)議來實現(xiàn)一個在線咨詢的功能。
1.發(fā)送的線程
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;
//發(fā)送線程
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int formPort; //自身端口
private String toIP; //發(fā)送到的IP
private int toPort; //發(fā)送到的端口
public TalkSend(int formPort, String toIP, int toPort) {
this.formPort = formPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(formPort);
reader = new BufferedReader(new InputStreamReader(System.in)); //獲取控制臺輸入
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
try {
String data = reader.readLine(); //獲取到的數(shù)據(jù)
byte[] datas = data.getBytes(); //轉(zhuǎn)換為需要的字節(jié)數(shù)組
//將數(shù)據(jù)打包成包
DatagramPacket packet = new DatagramPacket(datas,0, datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet); //發(fā)送包
//退出指令
if(packet.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
2.接收的線程
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//接收線程
public class TalkReceive implements Runnable {
DatagramSocket socket = null; //與發(fā)送端的socket為同一個,實現(xiàn)通信
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() {
while (true){
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//接收數(shù)據(jù)
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();
}
}
3.模擬學生端
//模擬學生
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老師")).start();
}
}
4.模擬老師端
//模擬老師
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "學生")).start();
}
}
5.截圖
學生端:
老師端:
6.總結
首先創(chuàng)建一個發(fā)送線程,里面定義要發(fā)送到的IP地址和端口,再創(chuàng)建一個DatagramSocket來進行通信,創(chuàng)建一個BufferReader(new InputStreamReader(System.in))來獲取控制臺的輸入的數(shù)據(jù),將獲取到的數(shù)據(jù)打包進行發(fā)送。之后創(chuàng)建一個接收線程,里面定義自身的端口即可,再創(chuàng)建一個和發(fā)送端一樣的DatagramSocket來進行通信,接收數(shù)據(jù)進行打印輸出即可。最后模擬學生端和老師端,創(chuàng)建這兩個線程來進行通信。學生發(fā)送線程到的端口是老師的接收端口,老師發(fā)送的端口是學生的接收端口,這樣才能互相通信。
以上就是本文關于Java代碼基于UDP多線程實現(xiàn)在線咨詢的全部內(nèi)容,希望能幫助大家對于Java多線程知識的學習和理解,也希望大家多多支持W3Cschool。