通过键盘录入获取要发送的信息。
将发送和接收分别封装到两个线程中。
package cn.hncu.UDP;
<span style="font-size:32px;color:#ff0000;">//发送方</span>
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class SendDemo {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(9999);
String info="湖南城市学院,Socket通信信息!";
byte buf[] = info.getBytes();//用默认码表
//DatagramPacket类中,有ip地址的构造方法是用来创建发送数据包的
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.5"),10000 );//注意,IP和端口都是接收方的
ds.send(dp);
ds.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<span style="font-size:32px;color:#ff0000;">//接收方</span>
<pre name="code" class="java"><span style="font-size:10px;"><span style="color:#ff0000;">package cn.hncu.UDP;
</span>import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ReceiveDemo {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(10000);
//把数据接收到dp中
byte buf[] = new byte[1024];
//DatagramPacket类中,没有ip地址的构造方法是用来创建接收数据包的
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);//接收之后,所有的数据都在dp里面
//从dp中解析出我们想要的信息
//获取ip
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();//端口
byte data[] = dp.getData();//对方发送的数据
String info = new String(data);
System.out.println(ip+":"+port+"----"+info);
} catch (Exception e) {
e.printStackTrace();
}
}
}</span><span style="font-size:32px;">
</span>
<span style="font-size:32px;color: rgb(255, 0, 0);">//运行放1</span>
<span style="font-size:32px;color: rgb(255, 0, 0);">
</span>
<span style="font-size:10px;">package cn.hncu.UDP;
//11111
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class UDPChat {
public static void main(String[] args) {
try {
DatagramSocket send = new DatagramSocket(10001);
DatagramSocket receive = new DatagramSocket(10002);
new Thread(new Send(send)).start();
new Thread(new Receive(receive)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket send) {
this.ds = send;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = br.readLine()) != null) {
byte buf[] = line.getBytes();
// 要用接收方的ip和接收端口
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("192.168.1.5"), 10004);
ds.send(dp);
if ("over".equals(line)) {
break;
}
}
ds.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Receive implements Runnable {
private DatagramSocket ds;
public Receive(DatagramSocket receive) {
this.ds = receive;
}
@Override
public void run() {
try {
byte buf[] = new byte[1024];// 大小够存储一行就可以
while (true) {
// 接收数据
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
// 解析数据
String ip = dp.getAddress().getHostAddress();
String info = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + "说:" + info);
if ("over".equals(info)) {
System.out.println(ip + "离开聊天室....");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
</span>
//运行方2
<pre name="code" class="java"><span style="font-size:10px;">package cn.hncu.UDP.chat2;
//22222
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UdpChat {
public static void main(String[] args) {
try {
DatagramSocket send = new DatagramSocket(10003);
DatagramSocket receive = new DatagramSocket(10004);
new Thread(new Send(send) ).start();
new Thread(new Receive(receive)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket send) {
this.ds = send;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while( (line=br.readLine())!=null ){
byte buf[] = line.getBytes();
//要用接收方的ip和接收端口
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.5"), 10002);
ds.send(dp);
if("over".equals(line)){
break;
}
}
ds.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(DatagramSocket receive) {
this.ds = receive;
}
@Override
public void run() {
try {
byte buf[] = new byte[1024];//大小够存储一行就可以
while(true){
//接收数据
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
//解析数据
String ip = dp.getAddress().getHostAddress();
String info= new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"说:"+info);
if("over".equals(info)){
System.out.println(ip+"离开聊天室....");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
</span>