站長www.software8.co

          熱轉(zhuǎn)印www.heatpress123.net

          Socket 多線程,Java C/S 模式 編程(服務(wù)器端)

          [java] view plaincopyprint? //Fig. 18.4: ServerGUI.java //Set up a Server that will receive a connection from a client, send //a string to the client, and close the connection. import java.io.*; import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class ServerGUI extends JFrame { private JTextArea displayArea; private JTextField list; private String time=""; public ServerGUI( )throws IOException { super("Server"); Container container=getContentPane(); list=new JTextField(); container.add(list,BorderLayout.NORTH); displayArea=new JTextArea(); container.add(new JScrollPane(displayArea),BorderLayout.CENTER); setSize(300,150); setVisible(true); setAlwaysOnTop(true); } public void setField() { list.setText(time); } public void appendArea(String s) { displayArea.append(s+"\n"); } public void setTime(String t) { time=t; } public String getTime() { return time; } } //ServerUpdate.java import java.util.*; public class Update extends Thread { private ServerGUI g; public Update(ServerGUI gui) { g=gui; } public void run() { try { String time=""; while(true) { Thread.sleep(1000); time=getLocalTime(); g.setTime(time); g.setField(); g.appendArea(time); } } catch(Exception e) { } } public String getLocalTime() { return (Calendar.getInstance().getTime().toString()); } } //Server.java import java.io.*; import java.net.*; public class Server extends Thread { private ServerGUI g; private ServerSocket server; public Server(ServerGUI gui)throws IOException { server=new ServerSocket(24680); g=gui; } public void run() { } public ServerSocket getServerSocket() { return server; } public ServerGUI getServerGUI() { return g; } } //Service.java import java.io.*; import java.net.*; public class Service extends Thread { private ServerGUI g; private Server listen; private String request=""; private Socket con; private ObjectOutputStream out; private ObjectInputStream in; public Service(ServerGUI gui,Server s)throws IOException { g=gui; listen=s; } public void run() { try { con=listen.getServerSocket().accept(); in=new ObjectInputStream(con.getInputStream()); out=new ObjectOutputStream(con.getOutputStream()); out.flush(); Thread.sleep(500); while(true) { Thread.sleep(1000); request=(String)in.readObject(); System.out.println("Server Got Request: " +request+"\n"); if(!request.equals("")) { if(request.equals("get")) { sendData(getTime(g)); } else if(request.equals("close")) { in.close(); out.close(); con.close(); break; } else { System.out.println("Request not Acceptable!.....\n"); } } else continue; } } catch(Exception e) { } } public String getTime(ServerGUI gui) { return g.getTime(); } public void sendData(String s) { try { String str=s; out.writeObject(str); out.flush(); } catch(IOException e) { } } } //Fig. 18.4: ServerRun.java //Set up a Server that will receive a connection from a client, send //a string to the client, and close the connection. import java.io.*; //www.heatpress123.net import javax.swing.*; public class ServerRun { public static void main(String args[])throws IOException { ServerGUI app=new ServerGUI(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Update update=new Update(app); update.start(); Server server=new Server(app); server.start(); Service service1=new Service(app,server); service1.start(); Service service2=new Service(app,server); service2.start(); } }

          posted on 2012-09-24 06:31 文哥哥 閱讀(1300) 評論(1)  編輯  收藏

          <2012年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          友情鏈接

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 江口县| 古丈县| 门源| 上栗县| 柳林县| 兖州市| 志丹县| 当涂县| 南昌市| 城固县| 灵丘县| 遂川县| 茌平县| 临武县| 鹿邑县| 黑水县| 垫江县| 邻水| 南丹县| 九江县| 松原市| 黄平县| 慈溪市| 南宫市| 墨竹工卡县| 康定县| 望江县| 贵德县| 民县| 芒康县| 平乡县| 济宁市| 江达县| 高尔夫| 赣榆县| 汕头市| 宜良县| 晋宁县| 息烽县| 新化县| 忻城县|