1个回答
展开全部
服务端和客户端代码:
public class Main {
public static void main(String[] args) throws Exception {
UDPServer server = new UDPServer();
server.start(6666, "logs/udpserver.txt");
Thread.sleep(500);
UDPClient client = new UDPClient();
client.start(6666);
for(int i = 0; i < 10; i++) {
client.send("Hello: " + i + "\n");
}
client.stop();
Thread.sleep(500);
server.stop();
}
}
class UDPServer implements Runnable{
private DatagramSocket ds;
private boolean running = false;
private Thread thread = null;
private FileOutputStream fos;
public void start(int port, String logFilename) throws Exception{
if(!running) {
running = true;
ds = new DatagramSocket(new InetSocketAddress("127.0.0.1", 6666));
fos = new FileOutputStream(logFilename);
thread = new Thread(this);
thread.start();
}
}
@Override
public void run() {
while(running) {
try {
byte[] buf = new byte[2048];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.setSoTimeout(500);
try {
ds.receive(dp);
fos.write(dp.getData(), 0, dp.getLength());
} catch (SocketTimeoutException e) {
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
ds.close();
}
public void stop() {
if(running) {
running = false;
thread.interrupt();
}
}
}
class UDPClient{
private DatagramSocket ds;
public void start(int port) throws Exception{
ds = new DatagramSocket(0);
ds.connect(new InetSocketAddress("127.0.0.1", 6666));
}
public void send(String message) {
byte[] data = message.getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length);
try {
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop() {
if (ds != null) {
ds.close();
}
}
}
界面代码你自己写吧
public class Main {
public static void main(String[] args) throws Exception {
UDPServer server = new UDPServer();
server.start(6666, "logs/udpserver.txt");
Thread.sleep(500);
UDPClient client = new UDPClient();
client.start(6666);
for(int i = 0; i < 10; i++) {
client.send("Hello: " + i + "\n");
}
client.stop();
Thread.sleep(500);
server.stop();
}
}
class UDPServer implements Runnable{
private DatagramSocket ds;
private boolean running = false;
private Thread thread = null;
private FileOutputStream fos;
public void start(int port, String logFilename) throws Exception{
if(!running) {
running = true;
ds = new DatagramSocket(new InetSocketAddress("127.0.0.1", 6666));
fos = new FileOutputStream(logFilename);
thread = new Thread(this);
thread.start();
}
}
@Override
public void run() {
while(running) {
try {
byte[] buf = new byte[2048];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.setSoTimeout(500);
try {
ds.receive(dp);
fos.write(dp.getData(), 0, dp.getLength());
} catch (SocketTimeoutException e) {
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
ds.close();
}
public void stop() {
if(running) {
running = false;
thread.interrupt();
}
}
}
class UDPClient{
private DatagramSocket ds;
public void start(int port) throws Exception{
ds = new DatagramSocket(0);
ds.connect(new InetSocketAddress("127.0.0.1", 6666));
}
public void send(String message) {
byte[] data = message.getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length);
try {
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop() {
if (ds != null) {
ds.close();
}
}
}
界面代码你自己写吧
更多追问追答
追问
这位大哥,运行有错啊……能不能把界面一起能过来啊 加分啊
追答
什么错?
server.start(6666, "logs/udpserver.txt");
这个换成你想要输出的文件,默认是在当前是要有一个logs目录 的
Storm代理
2023-05-30 广告
2023-05-30 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询