JAVA的即時通訊源代碼_第1頁
JAVA的即時通訊源代碼_第2頁
JAVA的即時通訊源代碼_第3頁
JAVA的即時通訊源代碼_第4頁
JAVA的即時通訊源代碼_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、JAVA實現(xiàn)即使通訊的代碼 圖形界面 / /用 Java 編寫的聊天器,可以當 服務(wù)器 或者 是客戶端,一對一,自定義對方 IP 及 端口。 /雖然名為 LANChat ,但不限于局域網(wǎng)。對象甚至可以是某種 “服務(wù)器”。 / 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)在啟動服務(wù)器并等待連接。" + 'n'); try ss = new ServerSocket(iport); catch(IOException ioe) content.append("n無法創(chuàng)建服務(wù),程序?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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論