歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

JAVA 基於TCP協議編寫的Socket聊天室程序

使用Socket套接字進行編程,完成的是基於TCP可靠服務實現服務器與客戶端的雙通信。

  1. package com.han;  
  2. import java.awt.Container;  
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.awt.event.WindowAdapter;  
  6. import java.awt.event.WindowEvent;  
  7. import java.io.*;  
  8. import java.net.*;  
  9. import javax.swing.JDialog;  
  10. import javax.swing.JScrollPane;  
  11. import javax.swing.JTextArea;  
  12. import javax.swing.JTextField;  
  13. /** 
  14.  * 本程序實現了一個TCP程序的服務器編程部分。 
  15.  * 使用Socket套接字進行編程,完成的是基於TCP可靠服務實現與客戶端的雙通信。 
  16.  * 客戶端的編程見本包中的類Client 
  17.  * @author HAN 
  18.  * 
  19.  */  
  20. @SuppressWarnings("serial")  
  21. public class Server extends JDialog{  
  22.     private BufferedReader reader;  
  23.     private PrintWriter writer;  
  24.     private ServerSocket server;  
  25.     private Socket socket;  
  26.     private JTextArea ta=new JTextArea();  
  27.     private JScrollPane sp=new JScrollPane(ta);  
  28.     private JTextField tf=new JTextField();  
  29.     Container cc;  
  30.     public Server(String title) {  
  31.         setTitle(title);  
  32.         addWindowListener(new WindowAdapter() {  
  33.             public void windowClosing (WindowEvent we) {  
  34.                 dispose();  //撤銷dialog一切相關資源   
  35.                 System.exit(0); //正常退出程序   
  36.             }  
  37.         });  
  38.         cc=getContentPane();  
  39.         setLayout(null);  
  40.         ta.setLineWrap(true);  
  41.         ta.setEditable(false);  
  42.         sp.setBounds(0,0,300,342);  
  43.         tf.setBounds(0,342,300,25);  
  44.         cc.add(sp);  
  45.         cc.add(tf);  
  46.         tf.addActionListener(new ActionListener(){  
  47.             public void actionPerformed(ActionEvent e){  
  48.                 try {  
  49.                     writer=new PrintWriter(socket.getOutputStream(),true);  
  50.                 } catch (IOException e1) {  
  51.                     // TODO Auto-generated catch block   
  52.                     e1.printStackTrace();  
  53.                 }  
  54.                 writer.println(tf.getText());  
  55.                 ta.append("User1:"+tf.getText()+'\n');  
  56.                 tf.setText("");  
  57.             }  
  58.         });  
  59.     }  
  60.     void getserver(){  
  61.         try{  
  62.             server=new ServerSocket(8998);  
  63.             ta.append("服務器套接字已經創建成功\n");  
  64.             while(true){  
  65.                 ta.append("等待客戶機的連接\n");  
  66.                 socket=server.accept();  
  67.                 ta.append("客戶機已連接\n");  
  68.                 reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  69.                 getClientMessage();  
  70.             }  
  71.         }catch(Exception e){  
  72.             e.printStackTrace();  
  73.         }  
  74.     }  
  75.     private void getClientMessage(){  
  76.         try {  
  77.             while(true){  
  78.                 String news=reader.readLine();  
  79.                 if(news!=null){  
  80.                     ta.append("User2:"+news+"\n");  
  81.                 }else{  
  82.                     ta.append("User2(客戶端) 已斷開鏈接\n");  
  83.                     break;  
  84.                 }  
  85.             }  
  86.         } catch (IOException e) {  
  87.             // TODO Auto-generated catch block   
  88.             e.printStackTrace();  
  89.         }  
  90.         try{  
  91.             if(reader!=null){  
  92.                 reader.close();  
  93.             }  
  94.         } catch (IOException e) {  
  95.             // TODO Auto-generated catch block   
  96.             e.printStackTrace();  
  97.         }  
  98.         if(socket!=null){  
  99.             try {  
  100.                 socket.close();  
  101.             } catch (IOException e) {  
  102.                 // TODO Auto-generated catch block   
  103.                 e.printStackTrace();  
  104.             }  
  105.         }  
  106.     }  
  107.     public static void main(String[] args) {  
  108.         Server user1=new Server("User1");  
  109.         user1.setBounds(0,0,300,400);  
  110.         user1.setResizable(false);  
  111.         user1.setVisible(true);  
  112.         user1.getserver();    
  113.     }  
  114. }  

 
  1. package com.han;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import java.io.*;  
  5. import java.net.*;  
  6. import javax.swing.*;  
  7. /** 
  8.  * 本程序實現了一個TCP程序的客戶端編程部分。 
  9.  * 使用Socket套接字進行編程,完成的是基於TCP可靠服務實現與服務器的雙通信。 
  10.  * 服務器的編程見本包中的類Server 
  11.  * 可以在不同的平台與不同的機器上運行,只是要把代碼中寫的IP地址修改為運行服務器程序Server的機器的IP地址。 
  12.  * @author HAN 
  13.  * 
  14.  */  
  15. @SuppressWarnings("serial")  
  16. public class Client extends JDialog{  
  17.     private BufferedReader reader;  
  18.     private PrintWriter writer;  
  19.     private Socket socket;  
  20.     private JTextArea ta=new JTextArea();  
  21.     private JScrollPane sp=new JScrollPane(ta);  
  22.     private JTextField tf=new JTextField();  
  23.     Container cc;  
  24.     public Client(String title) {  
  25.         setTitle(title);  
  26.         addWindowListener(new WindowAdapter() {  
  27.             public void windowClosing (WindowEvent we) {  
  28.                 dispose();  //撤銷dialog一切相關資源   
  29.                 System.exit(0); //正常退出程序   
  30.             }  
  31.         });  
  32.         cc=getContentPane();  
  33.         setLayout(null);  
  34.         ta.setLineWrap(true);  
  35.         ta.setEditable(false);  
  36.         sp.setBounds(0,0,300,342);  
  37.         tf.setBounds(0,342,300,25);  
  38.         cc.add(sp);  
  39.         cc.add(tf);  
  40.         tf.addActionListener(new ActionListener(){  
  41.             public void actionPerformed(ActionEvent e){  
  42.                 writer.println(tf.getText());  
  43.                 ta.append("User2:"+tf.getText()+'\n');  
  44.                 tf.setText("");  
  45.             }  
  46.         });  
  47.     }  
  48.     private void connect(){  
  49.         ta.append("嘗試連接\n");  
  50.         try {  
  51.             socket=new Socket("192.168.1.3",8998);  
  52.         } catch (UnknownHostException e) {  
  53.             // TODO Auto-generated catch block   
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block   
  57.             e.printStackTrace();  
  58.         }  
  59.         try {  
  60.             writer=new PrintWriter(socket.getOutputStream(),true);  
  61.         } catch (IOException e) {  
  62.             // TODO Auto-generated catch block   
  63.             e.printStackTrace();  
  64.         }  
  65.         ta.append("完成連接\n");  
  66.     }  
  67.       
  68.     private void getClientMessage(){  
  69.         try {  
  70.             reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  71.             while(true){  
  72.                 String news=reader.readLine();  
  73.                 if(news!=null){  
  74.                     ta.append("User1:"+news+"\n");  
  75.                 }else{  
  76.                     ta.append("User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n");  
  77.                     break;  
  78.                 }  
  79.             }  
  80.         } catch (IOException e) {  
  81.             // TODO Auto-generated catch block   
  82.             ta.append("User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n");  
  83.             e.printStackTrace();  
  84.         }  
  85.         try{  
  86.             if(reader!=null){  
  87.                 reader.close();  
  88.             }  
  89.         } catch (IOException e) {  
  90.             // TODO Auto-generated catch block   
  91.             e.printStackTrace();  
  92.         }  
  93.         if(socket!=null){  
  94.             try {  
  95.                 socket.close();  
  96.             } catch (IOException e) {  
  97.                 // TODO Auto-generated catch block   
  98.                 e.printStackTrace();  
  99.             }  
  100.         }  
  101.     }  
  102.     public static void main(String[] args) {  
  103.         Client user2=new Client("User2");  
  104.         user2.setBounds(0,0,300,400);  
  105.         user2.setVisible(true);  
  106.         user2.setResizable(false);  
  107.         user2.connect();  
  108.         user2.getClientMessage();  
  109.     }  
  110. }  
Copyright © Linux教程網 All Rights Reserved