<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    站長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 文哥哥 閱讀(1298) 評論(1)  編輯  收藏

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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    友情鏈接

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 特级毛片爽www免费版| 亚洲国产aⅴ综合网| 亚洲AV无码一区二区三区久久精品| 日本免费v片一二三区| 又大又硬又粗又黄的视频免费看 | 久久久久亚洲精品中文字幕 | AV无码免费永久在线观看| 亚洲另类少妇17p| 久视频精品免费观看99| 精品亚洲永久免费精品| 久久久久亚洲Av片无码v| 嫩草影院免费观看| 亚洲综合在线成人一区| 99久久国产精品免费一区二区| 日本亚洲精品色婷婷在线影院| 亚洲精品一级无码中文字幕| 亚洲日韩国产精品乱-久| 亚洲最大av无码网址| 永久免费AV无码国产网站| 亚洲精品亚洲人成在线观看麻豆| 国产在线不卡免费播放| 3d成人免费动漫在线观看| 久久国产精品亚洲一区二区| 99视频在线免费观看| 亚洲成a人片在线播放| 亚洲欧美成人av在线观看| 无码日韩精品一区二区免费| 成全视频免费观看在线看| 无码一区二区三区亚洲人妻| 亚洲制服丝袜在线播放| 亚洲国产精品无码专区在线观看| 日产国产精品亚洲系列| 成人性生交大片免费看无遮挡 | 国产色爽免费无码视频| 国产精品亚洲一区二区无码| 亚洲国产成人久久99精品| 亚洲av日韩综合一区在线观看| 亚洲伊人久久综合影院| 精品国产免费观看| 粉色视频成年免费人15次| 亚洲国产中文v高清在线观看|