简介
RT
code
server
package com.kuang;
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.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by lee on 2021/4/1.
*/
public class UdpServer {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Runnable r1 = new Runnable() { // 发送
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
@Override
public void run() {
// TODO Auto-generated method stub
try {
DatagramSocket ds = new DatagramSocket(9999);
String s = null;
System.out.print("输入:");
while ((s = br.readLine()) != null) {
DatagramPacket dp = new DatagramPacket(s.getBytes(), s.getBytes().length,
InetAddress.getByName("127.0.0.1"), 10000);//广播
ds.send(dp);
if(s.equals("quit")) {
ds.close();
break;
}
// System.out.print("输入:");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Runnable r2=new Runnable() { //接收
@Override
public void run() {
// TODO Auto-generated method stub
try {
DatagramSocket ds=new DatagramSocket(10001);
byte[] buff=new byte[1024];
DatagramPacket dp=new DatagramPacket(buff,buff.length);
while(true) {
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
String ip=dp.getAddress().getHostAddress();
int port=dp.getPort();
System.out.println("消息:"+ip+" "+port+" : "+s);
if(s.equals("quit")) {
ds.close();
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
ExecutorService es= Executors.newFixedThreadPool(2);
es.submit(r1);
es.submit(r2);
}//main
}
client
package com.kuang;
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.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by lee on 2021/4/1.
*/
public class UdpClient {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Runnable r1 = new Runnable() { // 发送
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
@Override
public void run() {
// TODO Auto-generated method stub
try {
DatagramSocket ds = new DatagramSocket(10002);
String s = null;
System.out.print("输入:");
while ((s = br.readLine()) != null) {
DatagramPacket dp = new DatagramPacket(s.getBytes(), s.getBytes().length,
InetAddress.getByName("127.0.0.1"), 10001);//广播
ds.send(dp);
if(s.equals("quit")) {
ds.close();
break;
}
// System.out.print("输入:");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Runnable r2=new Runnable() { //接收
@Override
public void run() {
// TODO Auto-generated method stub
try {
DatagramSocket ds=new DatagramSocket(10000);
byte[] buff=new byte[1024];
DatagramPacket dp=new DatagramPacket(buff,buff.length);
while(true) {
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
String ip=dp.getAddress().getHostAddress();
int port=dp.getPort();
System.out.println("消息:"+ip+" "+port+" : "+s);
if(s.equals("quit")) {
ds.close();
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
ExecutorService es= Executors.newFixedThreadPool(2);
es.submit(r1);
es.submit(r2);
}//main
}
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》