UDP网络编程的案例代码
用于描述发送发与接收方之间的通信
普通接收方/发送方代码
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
public class ReveiverTest {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(8888);
byte[] bArr = new byte[50];
DatagramPacket dp = new DatagramPacket(bArr, bArr.length);
ds.receive(dp);
System.out.println("接收到的数据内容为" + new String(bArr, 0, dp.getLength()));
String ss = "我收到了";
byte[] bytes = ss.getBytes();
DatagramPacket dp2 = new DatagramPacket(bytes, bytes.length, dp.getAddress(), dp.getPort());
ds.send(dp2);
System.out.println("回复消息成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != ds) {
ds.close();
}
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SendTest {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
String s = "你好世界";
byte[] bytes = s.getBytes();
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);
ds.send(dp);
byte[] bArr2 = new byte[50];
System.out.println("发送数据成功");
DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length);
ds.receive(dp2);
System.out.println("回复消息接收成功,内容为:" + new String(bArr2, 0, dp2.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}
}
}
多线程方式代码
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ThreadsTest implements Runnable{
@Override
public void run() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(8888);
byte[] bArr = new byte[50];
DatagramPacket dp = new DatagramPacket(bArr, bArr.length);
while (true) {
ds.receive(dp);
System.out.println("接收到的数据内容为" + new String(bArr, 0, dp.getLength()));
String ss = "我收到了";
byte[] bytes = ss.getBytes();
DatagramPacket dp2 = new DatagramPacket(bytes, bytes.length, dp.getAddress(), dp.getPort());
ds.send(dp2);
System.out.println("回复消息成功");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != ds) {
ds.close();
}
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReveiverTest2 {
public static void main(String[] args) {
ThreadsTest threadsTest = new ThreadsTest();
Thread thread = new Thread(threadsTest);
thread.start();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SendTest1 {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
String s = "你好世界";
byte[] bytes = s.getBytes();
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);
ds.send(dp);
byte[] bArr2 = new byte[50];
System.out.println("发送数据成功");
DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length);
ds.receive(dp2);
System.out.println("回复消息接收成功,内容为:" + new String(bArr2, 0, dp2.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}
}
}
|