基于JAVA源代碼程序設(shè)計(jì)_第1頁
基于JAVA源代碼程序設(shè)計(jì)_第2頁
基于JAVA源代碼程序設(shè)計(jì)_第3頁
基于JAVA源代碼程序設(shè)計(jì)_第4頁
基于JAVA源代碼程序設(shè)計(jì)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、/用 Java 編寫的聊天器,可以當(dāng) 服務(wù)器 或者 是客戶端,一對一,自定義對方 IP 及 端口。/雖然名為 LANChat ,但不限于局域網(wǎng)。對象甚至可以是某種 “服務(wù)器”。/public class LANChatV12public static void main(String argsLoginFrame lf = new LoginFrame("輸入目標(biāo)"lf.show(;/class LoginFrame extends JFrameJButton BOK;JLabel LdesAddr, Lport;JTextField TFdesAddr, TFport;S

2、tring desAddr, port;LoginFrame(LoginFrame(String titlesuper(title;Frame t = this;BOK = new JButton("確定"LdesAddr = new JLabel("目標(biāo) IP"Lport = new JLabel("端口 "TFdesAddr = new JTextField(desAddr,12;TFport = new JTextField(port,12;/TFpassword.setEchoChar('*'BOK.addAc

3、tionListener(new BOKListener(t;setBackground(Color.blue;setBounds(350,250,200,128;setLayout(new FlowLayout(FlowLayout.CENTER,5,7;add(LdesAddr;add(TFdesAddr;add(Lport;add(TFport;add(BOK;setResizable(false;/setVisible(true;addWindowListener(new WindowAdapter(public void windowClosing(WindowEvent eSyst

4、em.exit(0;class BOKListener implements ActionListenerFrame t;BOKListener(BOKListener(Frame tthis.t = t;public void actionPerformed(ActionEvent edesAddr = TFdesAddr.getText(;port = TFport.getText(;t.setVisible(false;Messenger m = new Messenger(desAddr, port;m.start(;/class Messenger extends ThreadStr

5、ing desAddr;String port;int iport;TextArea content, send;JButton Bsend;ChatFrame 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ù)Messen

6、ger(Messenger(String desAddr, String portsuper("LANChatMessenger"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 = " 與 &quo

7、t; + desAddr +" 聊天"flag = true;cbc = true;cf = new ChatFrame(title, content, send, Bsend;cf.show(;this.desAddr = desAddr;this.port = port;tryiport = Integer.parseInt(port;catch(NumberFormatException nfecontent.append("非法的 端口,程序 4 秒后關(guān)閉。n"flag = false;public void run(if(! flagtryTh

8、read.sleep(4000;catch(InterruptedException ieSystem.exit(0;System.exit(0;content.append("正在以客戶端方式進(jìn)行連接.n"docbc = true;tryclient = new Socket(desAddr,iport;catch(Exception econtent.append("錯(cuò)誤,無法連接地址:" + desAddr +":"+ port + 'n'content.append("等待 1 秒再連接,剩余 &qu

9、ot; + (tryTurns -1 + " 次。n" cbc = false;tryThread.sleep(1000;catch(InterruptedException ieSystem.exit(0;-tryTurns;while(tryTurns > 0 && ! cbc;if(cbccontent.append("連接成功,可以開始了。" + "nn"cf.setTitle("與 " + desAddr + ":" + port + " 聊天"

10、;send.requestFocus(;elsecontent.append("客戶端方式失敗,現(xiàn)在啟動(dòng)服務(wù)器并等待連接。" + 'n' tryss = new ServerSocket(iport;catch(IOException ioecontent.append("n無法創(chuàng)建服務(wù),程序?qū)⒃?4 秒后退出。n"tryThread.sleep(4000;catch(InterruptedException ieSystem.exit(0;System.exit(0; try cf.setTitle("等待連接中.在端口:&q

11、uot; + 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(.toString( + "n n" cf.setTitle(&quo

12、t;與 " + client.getInetAddress(.toString( + ":" + port + " 聊天" send.requestFocus(; try osw = new OutputStreamWriter(client.getOutputStream(; isr = new InputStreamReader(client.getInputStream(; br= new BufferedReader(isr; /content.append("nbr createdn" catch(IOExcept

13、ion ioe content.append("創(chuàng)建流錯(cuò)誤,程序?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.readLine(; catch(IOException ioe content.append("流讀取錯(cuò)誤,程序?qū)?/p>

14、在 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 System.exit(0; try line = br.readLine(; catch(IOException ioe content.append("

15、;流讀取錯(cuò)誤,程序?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; BsendListener(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.write(input + 'n', 0, input.length( + 1; osw.flush(;/ 暈 cat

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論