![基于JAVA源代碼程序設(shè)計(jì)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/17/9adca223-f404-4791-b126-3dbf4552c1e2/9adca223-f404-4791-b126-3dbf4552c1e21.gif)
![基于JAVA源代碼程序設(shè)計(jì)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/17/9adca223-f404-4791-b126-3dbf4552c1e2/9adca223-f404-4791-b126-3dbf4552c1e22.gif)
![基于JAVA源代碼程序設(shè)計(jì)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/17/9adca223-f404-4791-b126-3dbf4552c1e2/9adca223-f404-4791-b126-3dbf4552c1e23.gif)
![基于JAVA源代碼程序設(shè)計(jì)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/17/9adca223-f404-4791-b126-3dbf4552c1e2/9adca223-f404-4791-b126-3dbf4552c1e24.gif)
![基于JAVA源代碼程序設(shè)計(jì)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/17/9adca223-f404-4791-b126-3dbf4552c1e2/9adca223-f404-4791-b126-3dbf4552c1e25.gif)
版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度廣告活動(dòng)策劃與執(zhí)行代理合同范本
- 2025年度中小企業(yè)信用貸款合同范本及證明書格式
- 2025年度海運(yùn)集裝箱清潔與消毒服務(wù)合同
- 2025年度糧食行業(yè)市場拓展與品牌推廣合同
- 2025年度家暴離婚財(cái)產(chǎn)分割專項(xiàng)法律服務(wù)合同
- 金華浙江金華蘭溪市人民政府辦公室招聘勞務(wù)派遣工作人員筆試歷年參考題庫附帶答案詳解
- 貴州2025年貴州開放大學(xué)(貴州職業(yè)技術(shù)學(xué)院)招聘41人筆試歷年參考題庫附帶答案詳解
- 衢州浙江衢州江山市峽口中心幼兒園招聘幼兒園專任教師筆試歷年參考題庫附帶答案詳解
- 珠海廣東珠海市澳深度合作區(qū)頌琴小學(xué)招聘臨聘專任教師7人筆試歷年參考題庫附帶答案詳解
- 湖南2025年湖南農(nóng)業(yè)大學(xué)-岳麓山實(shí)驗(yàn)室博士后招聘筆試歷年參考題庫附帶答案詳解
- PID圖(工藝儀表流程圖)基礎(chǔ)知識培訓(xùn)課件
- 《澳大利亞特有動(dòng)物》課件
- 社會工作綜合能力上(初級)課件
- 《數(shù)據(jù)結(jié)構(gòu)》課件(完整版)
- 2023年春節(jié)后建筑施工復(fù)工復(fù)產(chǎn)專項(xiàng)方案
- 污水處理廠化驗(yàn)管理手冊
- 出納收入支出記賬表Excel模板
- 叉車操作規(guī)程
- 2021年春新青島版(五四制)科學(xué)四年級下冊全冊教學(xué)課件
- 土建工程技術(shù)標(biāo)范本(DOC167頁)
- 惡性腫瘤化療后重度骨髓抑制病人的護(hù)理論文
評論
0/150
提交評論