一、首先先把接受端与发送端的基本代码写好
1.发送端代码
代码语言:txt复制@发送端代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Send_Client {<!-- -->
public static void main(String[] args) throws Exception{<!-- -->
DatagramSocket socket=new DatagramSocket();
while (true){<!-- -->
//直接从控制台中读取数据
BufferedReader data=new BufferedReader(new InputStreamReader(System.in));
String datas = data.readLine();
//将数据转换为可以传输的字节
byte[] send_data = datas.getBytes();
DatagramPacket packet = new DatagramPacket(send_data,0,send_data.length, new InetSocketAddress("127.0.0.1",888));
socket.send(packet);
//这里时设置他们怎么结束,可以自行展开
if(datas.equals("bye")){<!-- -->
break;
}
}
socket.close();
}
}
2.接受端代码
代码语言:txt复制 import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receiver_Client {<!-- -->
public static void main(String[] args)throws Exception {<!-- -->
DatagramSocket socket = new DatagramSocket(888);
while(true){<!-- -->
byte[] buffer =new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
byte[] data = packet.getData();
String real_data = new String(data, 0, data.length);
real_data=real_data.trim();
if(real_data.equals("bye")){<!-- -->
break;
}
System.out.println(real_data);
}
socket.close();
}
}
注意:接受bye时要对字符串进行去空格操作,要不然无法退出,字符串.trim()即可
二、对代码进行改造为多线程
1.发送端线程
代码语言:txt复制import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class TalkSend implements Runnable{<!-- -->
DatagramSocket socket=null;
BufferedReader data=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(this.fromPort);
} catch (SocketException e) {<!-- -->
e.printStackTrace();
}
data=new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {<!-- -->
while (true) {<!-- -->
try {<!-- -->
//直接从控制台中读取数据
String datas = data.readLine();
//将数据转换为可以传输的字节
byte[] send_data = datas.getBytes();
DatagramPacket packet = new DatagramPacket(send_data, 0, send_data.length, new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if (datas.equals("bye")) {<!-- -->
break;
}
}catch (Exception e){<!-- -->
e.printStackTrace();
}
}
socket.close();
}
}
2.接收端线程
代码语言:txt复制import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {<!-- -->
DatagramSocket socket=null;
private int myPort;
private String msg;
public TalkReceive(int myPort,String msg) {<!-- -->
this.myPort = myPort;
this.msg=msg;
try {<!-- -->
socket = new DatagramSocket(this.myPort);
} catch (SocketException e) {<!-- -->
e.printStackTrace();
}
}
@Override
public void run() {<!-- -->
while(true){<!-- -->
try{<!-- -->
byte[] buffer =new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
byte[] data = packet.getData();
String real_data = new String(data, 0, data.length);
real_data=real_data.trim();
if(real_data.equals("bye")){<!-- -->
break;
}
System.out.println(this.msg ":" real_data);
}catch (Exception e){<!-- -->
e.printStackTrace();
}
}
socket.close();
}
}
3.总体的主方法
代码语言:txt复制public class Teacher {<!-- -->
public static void main(String[] args) {<!-- -->
new Thread(new TalkSend(5555,"127.0.0.1",9999)).start();
new Thread(new TalkReceive(5678,"学生")).start();
}
}
代码语言:txt复制public class Student {<!-- -->
public static void main(String[] args) {<!-- -->
new Thread(new TalkSend(7777,"127.0.0.1",5678)).start();
new Thread(new TalkReceive(9999,"老师")).start();
}
}
然后你就可以进行聊天了,想在局域网聊天,只要打开自己电脑对应的端口和另一台主机的端口,就可以进行相互的通信了