代码片段(1)
[代码] [Java]代码
001 | import java.io.IOException; |
002 | import java.io.UnsupportedEncodingException; |
003 | import java.net.DatagramPacket; |
004 | import java.net.DatagramSocket; |
005 | import java.net.InetSocketAddress; |
006 | import java.net.SocketException; |
007 | import java.net.URLDecoder; |
008 | import java.net.URLEncoder; |
009 | import java.util.Scanner; |
010 | import java.util.concurrent.ExecutorService; |
011 | import java.util.concurrent.Executors; |
012 |
013 | public class Main { |
014 | |
015 | private DatagramSocket ds; |
016 | private int port; |
017 | |
018 | public Main( int port) throws IOException { |
019 | this .port = port; |
020 | ds = new DatagramSocket(port); |
021 | // ds.setBroadcast(true); |
022 | } |
023 | |
024 | public static void main(String[] args) throws Exception { |
025 | int port = 9876 ; |
026 | Main main = new Main(port); |
027 | main.start(); |
028 | } |
029 | |
030 | void start() throws Exception{ |
031 | startServer(); |
032 | Scanner scanner = new Scanner(System.in); |
033 | String name = "" ; |
034 | while (name.isEmpty()) { |
035 | System.out.print( "请输入名称:" ); |
036 | name = scanner.nextLine(); |
037 | } |
038 | String msg = "" ; |
039 | while (! "exit" .equalsIgnoreCase(msg)) { |
040 | if (!msg.isEmpty()) { |
041 | send(name,msg); |
042 | } |
043 | System.out.print( "输入消息:>" ); |
044 | msg = scanner.nextLine(); |
045 | } |
046 | System.exit( 0 ); |
047 | } |
048 | |
049 | private void send(String name,String msg) throws IOException{ |
050 | String sendMsg = encoder(name)+ "," +encoder(msg); |
051 | try { |
052 | byte [] buf = sendMsg.getBytes( "UTF-8" ); |
053 | DatagramPacket dp = new DatagramPacket(buf,buf.length); |
054 | dp.setSocketAddress( new InetSocketAddress( "255.255.255.255" , port)); |
055 | ds.send(dp); |
056 | } catch (UnsupportedEncodingException e) { |
057 | e.printStackTrace(); |
058 | } |
059 | } |
060 | |
061 | private static String encoder(String str) { |
062 | try { |
063 | return URLEncoder.encode(str, "UTF-8" ); |
064 | } catch (UnsupportedEncodingException e) { |
065 | return "" ; |
066 | } |
067 | } |
068 | |
069 |
070 | private void startServer() throws SocketException { |
071 | ExecutorService threadPool = Executors.newCachedThreadPool(); |
072 | threadPool.submit( new UdpReceive(ds)); |
073 | } |
074 | |
075 | static class UdpReceive implements Runnable { |
076 | private DatagramSocket ds; |
077 | UdpReceive(DatagramSocket ds) { |
078 | this .ds = ds; |
079 | } |
080 | @Override |
081 | public void run() { |
082 | byte [] buf = new byte [ 1024 ]; |
083 | DatagramPacket dp = new DatagramPacket(buf, buf.length); |
084 | while ( true ) { |
085 | try { |
086 | ds.receive(dp); |
087 | String str = new String(dp.getData(),dp.getOffset(),dp.getLength()); |
088 | String[] msgs = str.split( "," ); |
089 | String name = decode(msgs[ 0 ]); |
090 | String msg = decode(msgs[ 1 ]); |
091 | System.out.println(name+ " 说:" +msg+ " (" +dp.getSocketAddress()+ ")" ); |
092 | // System.out.println("收到来自:"+dp.getSocketAddress()+"的消息 "+new String(dp.getData(),dp.getOffset(),dp.getLength())); |
093 | } catch (IOException e) { |
094 | e.printStackTrace(); |
095 | } |
096 | } |
097 | } |
098 | |
099 | private static String decode(String str) { |
100 | try { |
101 | return URLDecoder.decode(str, "UTF-8" ); |
102 | } catch (UnsupportedEncodingException e) { |
103 | return "" ; |
104 | } |
105 | } |
106 | } |
107 | } |