文章詳情頁
利用Socket進行Java網(wǎng)絡(luò)編程(三)
瀏覽:3日期:2024-06-21 11:40:10
內(nèi)容: 出自:天極網(wǎng) 郗旻 2002年11月11日 09:28 附:服務(wù)器的實現(xiàn)代碼import java.net.*;import java.io.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class talkServer{ public static void main(String[] args) { try{ file://建立服務(wù)器 ServerSocket server = new ServerSocket(9998); int i=1; for(;;){ Socket incoming = server.accept(); new ServerThread(incoming,i).start(); i++;} }catch (IOException ex){ ex.printStackTrace();} }}class ServerThread extends Thread implements ActionListener{ private int threadNum; private Socket socket; talkServerFrm t; BufferedReader in; PrintWriter out; private boolean talking=true; public ServerThread(Socket s,int c) { threadNum = c;socket = s; }public void actionPerformed(ActionEvent e){ Object source = e.getSource(); try{if(source==t.btnSend) { out.println(t.getTalk());t.clearTalk();}elseif(source==t.btnEnd) { out.println('談話過程被對方終止');out.close();in.close();talking = false; } }catch(IOException ex){ }}public void run(){ try{t=new talkServerFrm(new Integer(threadNum).toString(),this);t.setSize(500,500);t.show();in = new BufferedReader(new InputStreamReader(socket.getInputStream()));out = new PrintWriter(socket.getOutputStream(),true); }catch(Exception e){} new Thread() { public void run(){ try{while(true){ checkInput(); sleep(1000);} }catch (InterruptedException ex){ }catch(IOException ex){ } } }.start(); while(talking) { } t.dispose(); }private void checkInput() throws IOException{ String line; if((line=in.readLine())!=null)t.setPartner(line); file://這是界面類里的方法,file://用來將line的內(nèi)容輸出到用戶界面 }} Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標簽:
Java
相關(guān)文章:
1. JAVA教程 第八講 Java網(wǎng)絡(luò)編程(三)2. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究3. Python實現(xiàn)網(wǎng)絡(luò)聊天室的示例代碼(支持多人聊天與私聊)4. 如何用Python 實現(xiàn)全連接神經(jīng)網(wǎng)絡(luò)(Multi-layer Perceptron)5. 如何自己實現(xiàn)JavaScript的new操作符6. Javascript模擬實現(xiàn)new原理解析7. JavaScript如何實現(xiàn)防止重復的網(wǎng)絡(luò)請求的示例8. Java實現(xiàn)基于http協(xié)議的網(wǎng)絡(luò)文件下載9. python Socket網(wǎng)絡(luò)編程實現(xiàn)C/S模式和P2P10. Java 網(wǎng)絡(luò)編程之 TCP 實現(xiàn)簡單的聊天系統(tǒng)
排行榜
