站长www.software8.co

热转印www.heatpress123.net

Socket 多线程,Java C/S 模式 编程(服务器端)

[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 文哥哥 阅读(1296) 评论(1)  编辑  收藏

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

导航

统计

常用链接

留言簿

随笔档案

文章档案

友情链接

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜