标签:
1 import java.io.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 import java.net.*; 6 import java.util.*; 7 8 class Conn extends Thread{ 9 private JTextArea txt; 10 private Socket st; 11 private String msg = null; 12 private BufferedReader br = null; 13 private PrintStream ps; 14 public Conn(Socket st,JTextArea txt){ 15 this.st = st; 16 this.txt = txt; 17 start(); 18 } 19 public void run(){ 20 try{ 21 br = new BufferedReader(new InputStreamReader(st.getInputStream())); 22 ps = new PrintStream(new DataOutputStream(st.getOutputStream())); 23 }catch(Exception e){ 24 System.err.println("input failed"); 25 } 26 while(true){ 27 try{ 28 msg = br.readLine();//阻塞方法 29 txt.append("从客户端收到信息:"+msg+‘\n‘); 30 txt.append("信息接受时间是:"+new Date()+"\n"); 31 //创建读取文件线程,并将读取到的内容通过服务器发送出去 32 FileThread ft = new FileThread(); 33 Thread t = new Thread(ft); 34 t.start(); 35 while(ft.flag); 36 System.out.println(ft.readData); 37 Server.send(ft.readData); 38 39 }catch(Exception e){ 40 System.err.println("connection closed"); 41 break; 42 } 43 } 44 } 45 public void send(String msg){ 46 ps.println(msg); 47 } 48 49 } 50 51 52 class FileThread implements Runnable{ 53 public String readData = ""; 54 public static boolean flag = true; 55 public void run() { 56 readData = readFileByLines("D:\\Hello.txt"); 57 flag = false; 58 System.out.println(readData); 59 } 60 61 /** 62 * 以行为单位读取文件,常用于读面向行的格式化文件 63 */ 64 public String readFileByLines(String fileName) { 65 File file = new File(fileName); 66 BufferedReader reader = null; 67 String tempString = null; 68 String readData = ""; 69 try { 70 System.out.println("以行为单位读取文件内容,一次读一整行:"); 71 reader = new BufferedReader(new FileReader(file)); 72 int line = 1; 73 74 // 一次读入一行,直到读入null为文件结束 75 while ((tempString = reader.readLine()) != null) { 76 // 显示行号 77 // System.out.println("line " + line + ": " + tempString); 78 // line++; 79 readData = readData.concat(tempString);//字符串拼接推荐函数 80 } 81 reader.close(); 82 } catch (IOException e) { 83 e.printStackTrace(); 84 } finally { 85 if (reader != null) { 86 try { 87 reader.close(); 88 } catch (IOException e1) { 89 } 90 } 91 } 92 93 return readData; 94 } 95 96 } 97 98 99 public class Server extends JFrame{ 100 private JTextArea txt; 101 private ServerSocket ss; 102 public String tempString = null; 103 private static java.util.List<Conn> conns = new ArrayList<Conn>(); 104 public Server(){ 105 txt = new JTextArea(); 106 this.setTitle("服务器"); 107 this.setLayout(new BorderLayout()); 108 this.add(new JScrollPane(txt),BorderLayout.CENTER); 109 this.setSize(500,300); 110 this.setVisible(true); 111 112 run(); 113 } 114 public void run(){ 115 try{ 116 ss = new ServerSocket(3000); 117 }catch(Exception e){ 118 System.err.println("open socket failed"); 119 } 120 txt.append("服务器已经启动!"+"\n"); 121 while(true){ 122 try{ 123 Socket st=ss.accept(); 124 conns.add(new Conn(st,txt)); 125 } 126 catch(IOException ex){ 127 System.err.println(ex); 128 } 129 } 130 } 131 public static void send(String msg){ 132 for(Conn c:conns) 133 c.send(msg); 134 } 135 136 public static void main(String args[]){ 137 138 Server myserver=new Server(); 139 } 140 141 }
另外一种写法:
标签:
原文地址:http://www.cnblogs.com/hixin/p/4388592.html