1 packagesocketpack_2;2 importjava.awt.BorderLayout;3 importjava.awt.EventQueue;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.io.IOException;7 importjava.io.OutputStream;8 importjava.io.PrintWriter;9 importjava.net.InetSocketAddress;10 importjava.net.ServerSocket;11 importjava.net.Socket;12 importjava.nio.channels.SocketChannel;13 importjava.util.Scanner;14 importjavax.swing.JButton;15 importjavax.swing.JFrame;16 importjavax.swing.JPanel;17 importjavax.swing.JScrollPane;18 importjavax.swing.JTextArea;19 /**
20 * 网络 -可中断套接字21 *@authorVisec·Dana22 */
23 public classInterruptibleSocketTest {24 public static voidmain(String[] args) {25 EventQueue.invokeLater(newThread()26 {27 public voidrun(){28 JFrame frame=newInterruptibleSocketFrame();29 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);30 frame.setVisible(true);31
32 }33 }34 );35 }36 }37 class InterruptibleSocketFrame extendsJFrame{38 private static final long serialVersionUID = 1L;39 publicInterruptibleSocketFrame(){40 setSize(WIDTH,HEIGHT);41 setTitle("InterruptibleSocketTest");42
43 JPanel northpJPanel =newJPanel();44 add(northpJPanel,BorderLayout.NORTH);45
46 messages=newJTextArea();47 add(newJScrollPane(messages));48 interruptilButton=new JButton("Interruptilbel");49 blockingButton=new JButton("Blocking");50
51 northpJPanel.add(interruptilButton);52 northpJPanel.add(blockingButton);53 interruptilButton.addActionListener(newActionListener() {54 @Override55 public voidactionPerformed(ActionEvent event){56 interruptilButton.setEnabled(false);57 blockingButton.setEnabled(false);58 canncelButton.setEnabled(false);59 connectThread=new Thread(newRunnable(){60 @Override61 public voidrun() {62 try{63 connectInterruptibly();64 } catch(Exception e) {65 messages.append("\nInterruptibleSocketTest.connectInterruptibly:\n"+e);66 }67 }68 });69 connectThread.start();70 }71 });72 blockingButton.addActionListener(newActionListener() {73 @Override74 public voidactionPerformed(ActionEvent e) {75 interruptilButton.setEnabled(false);76 blockingButton.setEnabled(false);77 canncelButton.setEnabled(false);78 connectThread=new Thread(newRunnable(){79 @Override80 public voidrun() {81 try{82 connectBlocking();83 } catch(IOException e) {84 messages.append("InterruptibleSocketTest.connectblocking"+e);85 }86 }87 });88 connectThread.start();89 }90 });91
92 canncelButton =new JButton("Cancel");93 canncelButton.setEnabled(false);94 northpJPanel.add(canncelButton);95 canncelButton.addActionListener(newActionListener() {96 @Override97 public voidactionPerformed(ActionEvent e) {98 connectThread.interrupt();99 canncelButton.setEnabled(false);100
101 }102 });103 server =newTestServer();104 newThread(server).start();105
106 }107 /**
108 * 连接到服务器 终端I/O流 Interruptible109 *@throwsIOException110 */
111 public void connectInterruptibly()throwsIOException{112 messages.append("Interruptible:\n");113 SocketChannel channel=SocketChannel.open(new InetSocketAddress("192.168.0.141", 514));114 try{115 in=newScanner(channel);116 while(!Thread.currentThread().isInterrupted()){117 messages.append("Reading");118 if(in.hasNextLine()){119 String line=in.nextLine();120 messages.append(line);121 messages.append("\n");122 }123 }124 }finally{125 channel.close();126 EventQueue.invokeLater(newRunnable() {127 @Override128 public voidrun() {129 messages.append("Channel closed\n");130 interruptilButton.setEnabled(true);131 blockingButton.setEnabled(true);132 }133 });134 }135 }136 /**
137 * 连接到服务器 终端I/O流 Blocking138 *@throwsIOException139 */
140 public void connectBlocking()throwsIOException{141 messages.append("Blocking:\n\n");142 Socket socket=new Socket("localhost",8189);143 try{144 in=newScanner(socket.getInputStream());145 while (!Thread.currentThread().isInterrupted()){146 if(in.hasNextLine()){147 String line=in.nextLine();148 messages.append(line);149 messages.append("\n");150 }151 }152 }finally{153 socket.close();154 EventQueue.invokeLater(newRunnable() {155 @Override156 public voidrun() {157 messages.append("Socket closed\n");158 interruptilButton.setEnabled(false);159 }160 });161 }162 }163 /**
164 * 测试服务器监听8189端口 并返回标示给客户端165 */
166 class TestServer implementsRunnable{167 @Override168 public voidrun() {169 try{170 ServerSocket s=new ServerSocket(8189);171 while(true){172 Socket incoming=s.accept();173 Runnable r=newTestServerHandler(incoming);174 Thread t=newThread(r);175 t.start();176 }177 } catch(Exception e) {178 messages.append("\nTestServer.run:\n"+e);179 }180 }181
182 }183 class TestServerHandler implementsRunnable{184 publicTestServerHandler(Socket i){185 incoming=i;186 }187 @Override188 public voidrun() {189 try{190 OutputStream outputStream=incoming.getOutputStream();191 PrintWriter out=new PrintWriter(outputStream,true/*autoFulsh*/);192 while(counter<=100){193 counter++;194 if(counter<=10) out.print(counter);195 Thread.sleep(100);196 }197 incoming.close();198 messages.append("Closing Server\n");199 } catch(Exception e) {200 messages.append("\nTestServerHandler.run:\n"+e);201 }202
203 }204 privateSocket incoming;205 private intcounter;206 }207
208 privateScanner in;209 private JTextArea messages=null;210 privateJButton interruptilButton,blockingButton,canncelButton;211 privateThread connectThread;212 privateTestServer server;213
214 public static final int WIDTH=550;215 public static final int HEIGHT=400;216 }