package socketpack_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* 网络 -可中断套接字
* @author Visec·Dana
*/
public class InterruptibleSocketTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Thread()
{
public void run(){
JFrame frame=new InterruptibleSocketFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}
);
}
}
class InterruptibleSocketFrame extends JFrame{
private static final long serialVersionUID = 1L;
public InterruptibleSocketFrame(){
setSize(WIDTH,HEIGHT);
setTitle("InterruptibleSocketTest"); JPanel northpJPanel =new JPanel();
add(northpJPanel,BorderLayout.NORTH); messages=new JTextArea();
add(new JScrollPane(messages));
interruptilButton=new JButton("Interruptilbel");
blockingButton=new JButton("Blocking"); northpJPanel.add(interruptilButton);
northpJPanel.add(blockingButton);
interruptilButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event){
interruptilButton.setEnabled(false);
blockingButton.setEnabled(false);
canncelButton.setEnabled(false);
connectThread=new Thread(new Runnable(){
@Override
public void run() {
try{
connectInterruptibly();
} catch (Exception e) {
messages.append("\nInterruptibleSocketTest.connectInterruptibly:\n"+e);
}
}
});
connectThread.start();
}
});
blockingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
interruptilButton.setEnabled(false);
blockingButton.setEnabled(false);
canncelButton.setEnabled(false);
connectThread=new Thread(new Runnable(){
@Override
public void run() {
try {
connectBlocking();
} catch (IOException e) {
messages.append("InterruptibleSocketTest.connectblocking"+e);
}
}
});
connectThread.start();
}
}); canncelButton =new JButton("Cancel");
canncelButton.setEnabled(false);
northpJPanel.add(canncelButton);
canncelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
connectThread.interrupt();
canncelButton.setEnabled(false); }
});
server =new TestServer();
new Thread(server).start(); }
/**
* 连接到服务器 终端I/O流 Interruptible
* @throws IOException
*/
public void connectInterruptibly()throws IOException{
messages.append("Interruptible:\n");
SocketChannel channel=SocketChannel.open(new InetSocketAddress("192.168.0.141", 514));
try {
in=new Scanner(channel);
while(!Thread.currentThread().isInterrupted()){
messages.append("Reading");
if(in.hasNextLine()){
String line=in.nextLine();
messages.append(line);
messages.append("\n");
}
}
}finally{
channel.close();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
messages.append("Channel closed\n");
interruptilButton.setEnabled(true);
blockingButton.setEnabled(true);
}
});
}
}
/**
* 连接到服务器 终端I/O流 Blocking
* @throws IOException
*/
public void connectBlocking()throws IOException{
messages.append("Blocking:\n\n");
Socket socket=new Socket("localhost",8189);
try {
in=new Scanner(socket.getInputStream());
while (!Thread.currentThread().isInterrupted()){
if(in.hasNextLine()){
String line=in.nextLine();
messages.append(line);
messages.append("\n");
}
}
}finally{
socket.close();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
messages.append("Socket closed\n");
interruptilButton.setEnabled(false);
}
});
}
}
/**
* 测试服务器监听8189端口 并返回标示给客户端
*/
class TestServer implements Runnable{
@Override
public void run() {
try {
ServerSocket s=new ServerSocket(8189);
while(true){
Socket incoming=s.accept();
Runnable r=new TestServerHandler(incoming);
Thread t=new Thread(r);
t.start();
}
} catch (Exception e) {
messages.append("\nTestServer.run:\n"+e);
}
} }
class TestServerHandler implements Runnable{
public TestServerHandler(Socket i){
incoming=i;
}
@Override
public void run() {
try {
OutputStream outputStream=incoming.getOutputStream();
PrintWriter out=new PrintWriter(outputStream,true/*autoFulsh*/);
while(counter<=100){
counter++;
if(counter<=10) out.print(counter);
Thread.sleep(100);
}
incoming.close();
messages.append("Closing Server\n");
} catch (Exception e) {
messages.append("\nTestServerHandler.run:\n"+e);
} }
private Socket incoming;
private int counter;
} private Scanner in;
private JTextArea messages=null;
private JButton interruptilButton,blockingButton,canncelButton;
private Thread connectThread;
private TestServer server; public static final int WIDTH=550;
public static final int HEIGHT=400;
}