




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、JAVA實現(xiàn)即使通訊的代碼 圖形界面 / /用 Java 編寫的聊天器,可以當 服務器 或者 是客戶端,一對一,自定義對方 IP 及 端口。 /雖然名為 LANChat ,但不限于局域網(wǎng)。對象甚至可以是某種 “服務器”。 / import java.io.*; import .*; import java.awt.*; import javax.swing.*; import java.awt.event.*; / public class LANChatV12 public static void main(String args) LoginFrame lf = new Logi
2、nFrame("輸入目標"); lf.show(); / class LoginFrame extends JFrame JButton BOK; JLabel LdesAddr, Lport; JTextField TFdesAddr, TFport; String desAddr, port; LoginFrame() LoginFrame(String title) super(title); Frame t = this; BOK = new JButton("確定"); LdesAddr = new JLabel("目標 IP&quo
3、t;); Lport = new JLabel("端口 "); TFdesAddr = new JTextField(desAddr,12); TFport = new JTextField(port,12); /TFpassword.setEchoChar('*'); BOK.addActionListener(new BOKListener(t); setBackground(Color.blue); setBounds(350,250,200,128); setLayout(new FlowLayout(FlowLayout.CENTER,5,7);
4、add(LdesAddr); add(TFdesAddr); add(Lport); add(TFport); add(BOK); setResizable(false); /setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); class BOKListener implements ActionListener Frame t; BOKListener() BOKListener(Frame t) this.t =
5、 t; public void actionPerformed(ActionEvent e) desAddr = TFdesAddr.getText(); port = TFport.getText(); t.setVisible(false); Messenger m = new Messenger(desAddr, port); m.start(); / class Messenger extends Thread String desAddr; String port; int iport; TextArea content, send; JButton Bsend; ChatFrame
6、 cf; String title; Socket client; ServerSocket ss; OutputStreamWriter osw = null; InputStreamReader isr = null; BufferedReader br; String line; boolean flag;/端口號是否正確 boolean cbc; /can be client ? int tryTurns = 3; /客戶方式嘗試的次數(shù) Messenger() Messenger(String desAddr, String port) super("LANChatMesse
7、nger"); content = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); send = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); Bsend = new JButton("發(fā)送"); Bsend.setEnabled(false); title = "與 " + desAddr +" 聊天" flag = true; cbc = true; c
8、f = new ChatFrame(title, content, send, Bsend); cf.show(); this.desAddr = desAddr; this.port = port; try iport = Integer.parseInt(port); catch(NumberFormatException nfe) content.append("非法的 端口,程序 4 秒后關(guān)閉。n"); flag = false; public void run() if(! flag) try Thread.sleep(4000); catch(Interrupt
9、edException ie) System.exit(0); System.exit(0); content.append("正在以客戶端方式進行連接.n"); do cbc = true; try client = new Socket(desAddr,iport); catch(Exception e) content.append("錯誤,無法連接地址:" + desAddr +":"+ port + 'n'); content.append("等待 1 秒再連接,剩余 " + (tryTu
10、rns -1) + " 次。n"); cbc = false; try Thread.sleep(1000); catch(InterruptedException ie) System.exit(0); -tryTurns; while(tryTurns > 0) && ! cbc); if(cbc) content.append("連接成功,可以開始了。" + "nn"); cf.setTitle("與 " + desAddr + ":" + port + "
11、 聊天"); send.requestFocus(); else content.append("客戶端方式失敗,現(xiàn)在啟動服務器并等待連接。" + 'n'); try ss = new ServerSocket(iport); catch(IOException ioe) content.append("n無法創(chuàng)建服務,程序?qū)⒃?4 秒后退出。n"); try Thread.sleep(4000); catch(InterruptedException ie) System.exit(0); System.exit(0); tr
12、y cf.setTitle("等待連接中.在端口:" + port); client = ss.accept(); catch(Exception e) content.append("nss.accept() 方法失敗,程序?qū)⒃?4 秒后退出。n"); try Thread.sleep(4000); catch(InterruptedException ie) System.exit(0); System.exit(0); content.append("連接成功,可以開始。" + client.getInetAddress().t
13、oString() + "nn"); cf.setTitle("與 " + client.getInetAddress().toString() + ":" + port + " 聊天"); send.requestFocus(); try osw = new OutputStreamWriter(client.getOutputStream(); isr = new InputStreamReader(client.getInputStream(); br= new BufferedReader(isr); /c
14、ontent.append("nbr createdn"); catch(IOException ioe) content.append("創(chuàng)建流錯誤,程序?qū)⒃?4 秒后退出。n"); try Thread.sleep(4000); catch(InterruptedException ie) System.exit(0); System.exit(0); Bsend.setEnabled(true); Bsend.addActionListener(new BsendListener(content, send, osw); try line = br
15、.readLine(); catch(IOException ioe) content.append("流讀取錯誤,程序?qū)⒃?4 秒后退出。n"); try Thread.sleep(4000); catch(InterruptedException ie) System.exit(0); System.exit(0); while(true) content.append("他說 : " + line + 'n'); try Thread.sleep(1000); catch(InterruptedException ie) Syste
16、m.exit(0); try line = br.readLine(); catch(IOException ioe) content.append("流讀取錯誤,程序?qū)⒃?4 秒后退出。n"); try Thread.sleep(4000); catch(InterruptedException ie) System.exit(0); System.exit(0); class BsendListener implements ActionListener TextArea content; TextArea send; OutputStreamWriter osw; B
17、sendListener(TextArea content, TextArea send, OutputStreamWriter osw) this.content = content; this.send = send; this.osw = osw; public void actionPerformed(ActionEvent e) String input; input = send.getText(); if(input.length() > 0) content.append("我說 : " + input + 'n'); try osw.
18、write(input + 'n', 0, input.length() + 1); osw.flush();/ 暈 catch(Exception ee) content.append("不能發(fā)送 "" + input + "" , 發(fā)生了錯誤 : " +ee.getMessage() + 'n'); send.setText(""); send.requestFocus(); / class ChatFrame extends JFrame TextArea content; TextArea send; JButton Bsend; ChatFrame() ChatFrame(String frameTitle,TextArea content, TextArea send, JButton Bsend) super(frameTitle); this.content = content; this.send = send; this.Bsend = Bsend; this.send.setEditable(true); setLayout(null); setBounds(300
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國衍生品行業(yè)市場發(fā)展監(jiān)測及投資方向研究報告
- 中國聚乙烯土工膜專用料行業(yè)調(diào)查報告
- 2025年中國無線音頻設備行業(yè)發(fā)展監(jiān)測及投資規(guī)劃建議報告
- 2025年中國機夾行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告
- 2025-2030年中國摩托車大踏板行業(yè)深度研究分析報告
- 中國不銹鋼波紋管補償器市場供需現(xiàn)狀及投資戰(zhàn)略研究報告
- 中國熱鍍鋅鋼行業(yè)市場調(diào)查報告
- 2025年中國果蔬行業(yè)市場調(diào)查研究及投資前景預測報告
- 中國自動搬運機械手行業(yè)市場規(guī)模及未來投資方向研究報告
- 管子內(nèi)壁清理機行業(yè)深度研究分析報告(2024-2030版)
- 智慧型陸基式漁業(yè)產(chǎn)業(yè)園項目可行性研究報告模板-備案拿地
- 基本藥物培訓課件資料
- 吉林省長春市2024-2025學年八年級下學期期末測試數(shù)學試卷(含答案)
- 2025秋三年級上冊語文上課課件 9 犟龜
- 電子文件長期保存技術(shù)-洞察及研究
- 廣告安裝培訓課件
- 石灰廠中控室管理制度
- 《黨政機關(guān)厲行節(jié)約反對浪費條例》培訓課件
- 太陽能安裝工程管理制度
- 【數(shù)學 北京版】2025年高考招生統(tǒng)一考試高考真題數(shù)學試卷(真題+答案)
-
評論
0/150
提交評論