java聊天程序入門級(jí)開(kāi)發(fā)_第1頁(yè)
java聊天程序入門級(jí)開(kāi)發(fā)_第2頁(yè)
java聊天程序入門級(jí)開(kāi)發(fā)_第3頁(yè)
java聊天程序入門級(jí)開(kāi)發(fā)_第4頁(yè)
java聊天程序入門級(jí)開(kāi)發(fā)_第5頁(yè)
已閱讀5頁(yè),還剩30頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、/*try.中定義的局部變量只能在try .中使用,不能在其他地方使用*/public class Testpublic static void main(String args)tryint i = 10;System.out.println("i = " + i);catch (Exception e)/System.out.println("i = " + i); /error 因?yàn)樵趖ry.中定義的局部變量只能在try.中使用, try.中定義的局部變量不能在catch(.) . 和 finally.中使用finally /System.out.

2、println("i = " + i); /error 原因同上/System.out.println("i = " + i); /總結(jié): try.中定義的局部變量只能在try .中使用,不能在其他地方使用/*錯(cuò)誤的程序*/import java.io.*;import .*;public class TServerpublic static void main(String args)ServerSocket ss = null;Socket s = null;DataInputStream dis = null;tryss = new ServerS

3、ocket(8888);while (true)s = ss.accept();dis = new DataInputStream(s.getInputStream();String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);dis.close(); /這樣寫就錯(cuò)了,因?yàn)楸菊Z(yǔ)句永遠(yuǎn)無(wú)法得到執(zhí)行s.close(); /同上ss.close(); /同上catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exi

4、t(-1);catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);/*錯(cuò)誤的程序*/import java.io.*;import .*;public class TServer_2public static void main(String args)tryServerSocket ss = new ServerSocket(8888);Socket s = null;DataInputStream dis = null;while (true)s = ss.accept();dis

5、= new DataInputStream(s.getInputStream();String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exit(-1);catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);finallytrydos.close(

6、); /error 找到不到doss.close(); /error 同上ss.close(); /error 同上catch (Exception e)/*錯(cuò)誤的程序*/import java.io.*;import .*;public class TServer_3public static void main(String args)ServerSocket ss; /會(huì)導(dǎo)致40行出錯(cuò)Socket s; /會(huì)導(dǎo)致41行出錯(cuò) DataInputStream dis; /會(huì)導(dǎo)致42行出錯(cuò)tryss = new ServerSocket(8888);s = null;dis = null;wh

7、ile (true)s = ss.accept();dis = new DataInputStream(s.getInputStream();String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exit(-1);catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System

8、.exit(-1);finallytrydis.close(); /40行 error 編譯器認(rèn)為dis可能還沒(méi)有被初始化s.close(); /41行 error 同上ss.close(); /42行 error 同上catch (Exception e)/*正確的程序*/import java.io.*;import .*;public class TServer_4public static void main(String args)ServerSocket ss = null; Socket s = null; DataInputStream dis = null; tryss =

9、new ServerSocket(8888);s = null;dis = null;while (true)s = ss.accept();dis = new DataInputStream(s.getInputStream();String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exit(-1);catch (IOException e)Syste

10、m.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);finallytrydis.close(); s.close(); ss.close(); catch (Exception e)/* * 2009年3月27日10:36:30 * 有關(guān)Java虛擬機(jī)異常處理機(jī)制的問(wèn)題 * * */class Apublic void f()System.out.println("AAAA");public class Test public static void main(String args)A aa1 = null; /a

11、a1必須的被賦值,否則會(huì)導(dǎo)致21行出錯(cuò),因?yàn)榫幾g器認(rèn)為16行并一定會(huì)成功給aa1賦值, /不過(guò)令人遺憾的是:編譯器在編譯時(shí)卻檢測(cè)不出空指針異常錯(cuò)誤,以本程序?yàn)槔驗(yàn)?6行并不一定可以成功給aa1賦值,所以理論上講21行的aa1仍然可能是null,但是令人遺憾的是編譯器編譯時(shí)卻檢測(cè)不出這種錯(cuò)誤,它只會(huì)在運(yùn)行時(shí)拋出java.lang.NullPointerException異常tryaa1 = new A(); /16行catch (Exception e)/aa1 = new A();aa1.f(); /21行 編譯器在編譯時(shí)檢測(cè)不出空指針異常錯(cuò)誤,以本程序?yàn)槔?,因?yàn)?6行并不一定可以成功給a

12、a1賦值,所以理論上講本行中的aa1仍然可能是null,但是令人遺憾的是編譯器編譯時(shí)卻檢測(cè)不出這種錯(cuò)誤,它只會(huì)在運(yùn)行時(shí)拋出java.lang.NullPointerException異常/*/ * 下面兩行代碼驗(yàn)證了: /編譯器在編譯時(shí)檢測(cè)不出空指針異常錯(cuò)誤,它只會(huì)在運(yùn)行時(shí)拋出/java.lang.NullPointerException異常/*/A aa2 = null;/aa2.f(); /編譯器編譯時(shí)檢測(cè)不會(huì)出錯(cuò),只會(huì)在運(yùn)行時(shí)拋出java.lang.NullPointerException異常import java.io.*;import .*;public class TServerp

13、ublic static void main(String args)ServerSocket ss = null; /這里的ss必須得初始化,哪怕初始化為null也可以,否則會(huì)導(dǎo)致39行報(bào)錯(cuò), 因?yàn)榫幾g器認(rèn)為14行并不一定會(huì)給ss賦值成功, 不過(guò)很可笑的是Java編譯器認(rèn)為try.中代碼執(zhí)行并不一定成功,所以要求你在try之前必須得對(duì)變量賦初值,但是編譯器卻檢測(cè)不出賦初值為null的錯(cuò)誤,以本程序?yàn)槔?,這里將ss賦為null,按照邏輯,try.中代碼并不一定能給ss賦正確的值,那最終結(jié)果ss仍可能是null,所以理論上講:39行代碼仍然可能是錯(cuò)的,不過(guò)JAva編譯器卻檢測(cè)不出這種錯(cuò)誤,我估計(jì)

14、是因?yàn)锳 aa = null; aa.成員名; 這種方式編譯器編譯時(shí)是檢測(cè)不出這種錯(cuò)誤的,只會(huì)跑出NullPointerException異常 Socket s = null;DataInputStream dis = null; tryss = new ServerSocket(8888); / 14行 while (true) s = ss.accept();dis = new DataInputStream(s.getInputStream(); String str = dis.readUTF(); System.out.println("對(duì)方說(shuō)的是: " + st

15、r);catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exit(-1);catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);finallytrydis.close(); s.close(); ss.close(); /39行 catch (Exception e)一個(gè)發(fā)一個(gè)接import java.io.*;import .*;public class TClientpublic s

16、tatic void main(String args)Socket s = null;DataOutputStream dos = null;trys = new Socket("127.0.0.1", 8888);dos = new DataOutputStream(s.getOutputStream();/dos.writeUTF("哈哈");dos.writeUTF("嘿嘿");catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System

17、.exit(-1);catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);finallytrydos.close();s.close();catch (Exception e)import java.io.*;import .*;public class TServerpublic static void main(String args)ServerSocket ss = null;Socket s = null;DataInputStream dis = null; tryss =

18、new ServerSocket(8888);while (true)s = ss.accept();dis = new DataInputStream(s.getInputStream();String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");System.exit(-1);catch (IOException e)System.out.println("

19、讀寫數(shù)據(jù)出錯(cuò),程序終止");System.exit(-1);finallytrydis.close(); /error 找到不到doss.close(); /error 同上ss.close(); /error 同上catch (Exception e)每人能且只能說(shuō)一句import java.io.*;import .*;public class TClient public static void main(String args) throws ExceptionSocket s = null;DataInputStream dis = null;DataOutputStrea

20、m dos = null;BufferedReader br = null;trys = new Socket("127.0.0.1", 8888);dis = new DataInputStream(s.getInputStream();dos = new DataOutputStream(s.getOutputStream();br = new BufferedReader(new InputStreamReader(System.in);while (true)String str = br.readLine();dos.writeUTF(str);if (str.e

21、qualsIgnoreCase("再見(jiàn)")break;str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);if (str.equalsIgnoreCase("再見(jiàn)")break;catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");catch (Exception

22、e)finallytrydis.close();dos.close();s.close();catch (Exception e)import java.io.*;import .*;public class TServer public static void main(String args) ServerSocket ss = null;Socket s = null;DataInputStream dis = null;DataOutputStream dos = null;BufferedReader br = null;tryss = new ServerSocket(8888);

23、s = ss.accept();dis = new DataInputStream(s.getInputStream();dos = new DataOutputStream(s.getOutputStream();br = new BufferedReader(new InputStreamReader(System.in);while (true)String str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);if (str.equalsIgnoreCase("再見(jiàn)")break;str

24、= br.readLine();dos.writeUTF(str);if (str.equalsIgnoreCase("再見(jiàn)")break;catch (SocketException e)System.out.println("網(wǎng)絡(luò)連接出錯(cuò),程序終止");catch (IOException e)System.out.println("讀寫數(shù)據(jù)出錯(cuò),程序終止");catch (Exception e)finallytrydis.close();dos.close();s.close();ss.close();catch (Excep

25、tion e)任意一方都可以連著說(shuō)import java.io.*;import .*;public class TClientpublic static void main(String args) throws ExceptionSocket s = new Socket("127.0.0.1", 8888);DataInputStream dis = new DataInputStream(s.getInputStream();DataOutputStream dos = new DataOutputStream(s.getOutputStream();new Cli

26、entRead(dis).start();new ClientWriter(dos).start();class ClientRead extends Threadprivate DataInputStream dis = null;public ClientRead(DataInputStream dis)this.dis = dis;public void run()while (true)tryString str = dis.readUTF();System.out.println("對(duì)方說(shuō)的是: " + str);if (str.equalsIgnoreCase(

27、"再見(jiàn)")break;catch (Exception e)class ClientWriter extends Threadprivate DataOutputStream dos = null;public ClientWriter(DataOutputStream dos)this.dos = dos;public void run()BufferedReader br = new BufferedReader(new InputStreamReader(System.in);while (true)tryString str = br.readLine();dos.

28、writeUTF(str);if (str.equalsIgnoreCase("再見(jiàn)")break;catch (Exception e)import java.io.*;import .*;public class TServerpublic static void main(String args) throws ExceptionServerSocket ss = new ServerSocket(8888);Socket s = ss.accept();DataInputStream dis = new DataInputStream(s.getInputStrea

29、m();DataOutputStream dos = new DataOutputStream(s.getOutputStream();new ServerRead(dis).start();new ServerWriter(dos).start();class ServerRead extends Threadprivate DataInputStream dis = null;public ServerRead(DataInputStream dis)this.dis = dis;public void run()while (true)tryString str = dis.readUT

30、F();System.out.println("對(duì)方說(shuō)的是: " + str);if (str.equalsIgnoreCase("再見(jiàn)")break;catch (Exception e)class ServerWriter extends Threadprivate DataOutputStream dos = null;public ServerWriter(DataOutputStream dos)this.dos = dos;public void run()BufferedReader br = new BufferedReader(new

31、InputStreamReader(System.in);while (true)tryString str = br.readLine();dos.writeUTF(str);if (str.equalsIgnoreCase("再見(jiàn)")break;catch (Exception e)具有圖形化的用戶聊天界面import java.awt.*;import java.awt.event.*;import .*;import java.io.*;import javax.swing.JOptionPane;public class TClientpublic static

32、void main(String args)new TCPClient().launch();class TCPClientprivate TextArea ta = null;private TextField tf = null;private Button bn = null;private DataInputStream dis = null;private DataOutputStream dos = null;private Socket s = null;private Frame f = null;public void launch()createUI();connect()

33、; new TCPClientReader().start(); new TCPClientWriter().start(); public void createUI()f = new Frame("客戶端");f.setLocation(300, 200);ta = new TextArea();tf = new TextField();Panel p = new Panel(new BorderLayout();bn = new Button("發(fā)送");p.add(tf, BorderLayout.CENTER);p.add(bn, Border

34、Layout.EAST);f.add(ta, BorderLayout.CENTER);f.add(p, BorderLayout.SOUTH);f.setSize(400, 400);f.setVisible(true);f.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(-1););public void close()trydis.close();dos.close();s.close();catch (Exception e)/System.exit(-1)

35、;public void connect()trys = new Socket("127.0.0.1", 8888); /在機(jī)房可以修改本IPdis = new DataInputStream(s.getInputStream();dos = new DataOutputStream(s.getOutputStream();catch (Exception e)/e.printStackTrace();/System.exit(-1);class TCPClientReader extends Threadpublic void run()while (true)trySt

36、ring str = dis.readUTF();ta.append("對(duì)方說(shuō): " + str + "n");if (str.equals("再見(jiàn)")close();System.exit(-1);catch (Exception e)JOptionPane.showMessageDialog(f, "提示: 客戶端已經(jīng)斷開(kāi)連接");/e.printStackTrace();/System.exit(-1);return;class TCPClientWriter extends Threadpublic voi

37、d run()tf.addActionListener(new TCPClientListener();bn.addActionListener(new TCPClientListener();class TCPClientListener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) /108行tryString str = tf.getText();tf.setText("");ta.append("自己說(shuō): " + str + "n&

38、quot;);dos.writeUTF(str);if (str.equals("再見(jiàn)")close();System.exit(-1);catch (Exception e2) /如果這里的形參也定義為e,將會(huì)和108行的代碼中的形參e命名重復(fù), 因此這里建議改為其他名字e2.printStackTrace();System.exit(-1);import java.awt.*;import java.awt.event.*;import .*;import java.io.*;import javax.swing.*;public class TServerpublic

39、 static void main(String args)new TCPServer().launch();class TCPServerprivate TextArea ta = null;private TextField tf = null;private Button bn = null;private DataInputStream dis = null;private DataOutputStream dos = null;private ServerSocket ss = null;private Socket s = null; private Frame f = null;

40、public void launch()createUI();connect(); /如果客戶端程序沒(méi)有打開(kāi)的話,則程序?qū)⒃?2行停滯不前,即如果客戶端程序沒(méi)有打開(kāi)的話, 31和32行的代碼是不會(huì)執(zhí)行的new TCPServerReader().start(); /31行new TCPServerWriter().start(); /32行public void createUI()f = new Frame();f.setTitle("服務(wù)器端");ta = new TextArea();tf = new TextField();Panel p = new Panel(new BorderLayout();bn = new Button("發(fā)送");p.add(tf, BorderLayout.CENTER);p.add(bn, BorderLayout.EAST);f.add(ta, BorderLayout.CENTER);f.add(p, BorderLayout.SOUTH);f.setSize(400, 400);f.setVisible(true);f.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Sys

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論