文章詳情頁
利用Socket進行Java網(wǎng)絡編程(三)
瀏覽:5日期:2024-06-21 11:40:10
內(nèi)容: 出自:天極網(wǎng) 郗旻 2002年11月11日 09:28 附:服務器的實現(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://建立服務器 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. php網(wǎng)絡安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究2. JavaScript如何實現(xiàn)防止重復的網(wǎng)絡請求的示例3. IDEA 工程里 new不出來Vue文件的圖文解決方案4. Python用SSH連接到網(wǎng)絡設(shè)備5. python網(wǎng)絡編程之五子棋游戲6. Java 根據(jù)url下載網(wǎng)絡資源7. JAVA教程 第八講 Java網(wǎng)絡編程(三)8. 基于Java判斷網(wǎng)絡是否正常代碼實例9. Python實現(xiàn)socket庫網(wǎng)絡通信套接字10. selenium-python如何捕獲網(wǎng)絡流量的響應
排行榜
