?? liaotiaoxitong.txt
字號(hào):
服務(wù)器端源代碼(ChatServer.java)如下:
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: Java Applet實(shí)現(xiàn)網(wǎng)絡(luò)聊天室</p>
* <p>Description: Java Applet是通過(guò)瀏覽器面向用戶(hù)的Java小應(yīng)用程序</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: cumt</p>
* @author yanglinsen
* @version 1.0
*/
public class ChatServer {
/*
* m_threads是一個(gè)Vector靜態(tài)變量,它維護(hù)所有Server方的
* ServerThread實(shí)例,通過(guò)該變量能向所有連入Internet
* 的Applet廣播信息
*/
//Chat Server的主方法入口。
//該方法監(jiān)聽(tīng)Chat Applet的請(qǐng)求,并為新連接的
//Applet創(chuàng)建一個(gè)服務(wù)線(xiàn)程
public static void main(String args[])
{
ServerSocket socket=null;
Vector m_threads=new Vector();
System.out.println("服務(wù)器已啟動(dòng),正在等待客戶(hù)的請(qǐng)求...");
try
{
//設(shè)置Server監(jiān)聽(tīng)端口號(hào)為8000, 這個(gè)數(shù)字必須
//和程序ChatClient中的port參數(shù)一致。
socket=new ServerSocket(8000);
}
catch(Exception e)
{
System.out.println("服務(wù)接口建立失敗!");
return;
}
try
{
int nid=0;
while(true)
{
//監(jiān)聽(tīng)是否有新Chat Applet連接到Server,
//線(xiàn)程運(yùn)行到該語(yǔ)句會(huì)封鎖,直到有新的連接產(chǎn)生。
Socket s=socket.accept();
//創(chuàng)建一個(gè)新的ServerThread.
ServerThread st=new ServerThread(s,m_threads);
//為該線(xiàn)程設(shè)置一個(gè)ID號(hào)。
st.setID(nid++);
//將該線(xiàn)程加入到m_threads Vector中。
m_threads.addElement(st);
//啟動(dòng)服務(wù)線(xiàn)程。
new Thread(st). start();
//通知所有Chat Applet有一個(gè)新的網(wǎng)友加入。
for(int i=0;i<m_threads.size();i++)
{
ServerThread st1=(ServerThread)m_threads.elementAt(i);
st1.write("<服務(wù)器>歡迎 "+st.getID()+"號(hào)朋友進(jìn)入聊天室!");
}
System.out.println("接受"+st.getID()+"號(hào)客戶(hù)請(qǐng)求");
System.out.println("繼續(xù)等待其他客戶(hù)的請(qǐng)求...\n");
}
}
catch(Exception e)
{
System.out.println("服務(wù)器已關(guān)閉...");
}
}
}
/*
* 監(jiān)聽(tīng)線(xiàn)程,監(jiān)聽(tīng)對(duì)應(yīng)的Chat Applet是否有信息傳來(lái)。
*/
class ServerThread implements Runnable
{
Vector m_threads;
Socket m_socket=null;
DataInputStream m_in=null;
DataOutputStream m_out=null;
int m_nid;
//初始化該線(xiàn)程。
public ServerThread(Socket s,Vector threads)
{
m_socket=s;
m_threads=threads;
try
{
m_in=new DataInputStream(m_socket.getInputStream());
m_out=new DataOutputStream(m_socket.getOutputStream());
}
catch(Exception e)
{
}
}
public void run() //線(xiàn)程的執(zhí)行體。
{
System.out.println("等待進(jìn)程正在運(yùn)行");
try
{
while(true)
{
//監(jiān)聽(tīng)對(duì)應(yīng)的Applet是否傳來(lái)消息
//程序陷入到m_in.readUTF()中,直到有信息傳來(lái)才返回。
String s=m_in.readUTF();
if (s==null)
break;
//如果Chat Applet傳來(lái)的信息為"leave",
//則通知所有其他的的Chat Applet自己退出了。
if (s.trim().equals ("leave"))
for (int i=0;i<m_threads.size();i++)
{
ServerThread st=(ServerThread)m_threads.elementAt(i);
st.write("*各位朋友, "+getID()+"號(hào)朋友離開(kāi)聊天室"+"!*");
}
else
//向所有Chat Applet廣播該信息。
for(int i=0;i<m_threads.size();i++)
{
ServerThread st=(ServerThread)m_threads.elementAt(i);
st.write("<"+getID()+"朋友說(shuō)>"+s);
}
}
}
catch(Exception e)
{ e.printStackTrace();
}
//從m_threads Vector中刪除該線(xiàn)程,表示該線(xiàn)程已經(jīng)離開(kāi)聊天室。
m_threads.removeElement(this);
try
{ m_socket.close();
}
catch (Exception e){}
}
//將msg送回對(duì)應(yīng)的Applet
public void write (String msg)
{
synchronized(m_out)
{
try
{
m_out.writeUTF(msg);
}
catch(IOException e){}
}
}
public int getID() //獲得該線(xiàn)程的ID.
{
return m_nid;
}
public void setID(int nid) // //設(shè)置線(xiàn)程的ID.
{
m_nid=nid;
}
}
客戶(hù)端源代碼(ChatClientApplet.java)如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: Java Applet實(shí)現(xiàn)網(wǎng)絡(luò)聊天室</p>
* <p>Description: Java Applet是通過(guò)瀏覽器面向用戶(hù)的Java小應(yīng)用程序</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: cumt</p>
* @author yanglinsen
* @version 1.0
*/
public class ChatClientApplet extends Applet implements Runnable {
private boolean isStandalone = false;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
DataInputStream m_in; //消息輸入流
DataOutputStream m_out; //消息輸出流
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JLabel jLabel3 = new JLabel();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public ChatClientApplet() {
}
//Initialize the applet
public void init() {
m_in=null;
m_out=null;
try{
URL url=getCodeBase(); //獲取applet 的URL 值。
//獲取服務(wù)器IP地址
InetAddress inetaddr=InetAddress.getByName(url.getHost());
Socket m_socket;
//屏幕顯示服務(wù)器IP地址、通訊協(xié)議
System.out.println("服務(wù)器:"+inetaddr+" "+url.getHost()+" "+url.getProtocol());
m_socket=new Socket(inetaddr,8000); //創(chuàng)建與服務(wù)器IP地址連接的套接口
//在套接口上建立輸入流
m_in=new DataInputStream(m_socket.getInputStream());
//在套接口上建立輸出流
m_out=new DataOutputStream(m_socket.getOutputStream());
}
catch (Exception e)
{
System.out.println("Error:"+e);
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
jLabel1.setFont(new java.awt.Font("DialogInput", 1, 20));
jLabel1.setForeground(Color.red);
jLabel1.setToolTipText("作者:楊林森 Email:yanglinsen@126.com");
jLabel1.setText("一對(duì)多聊天程序Java Applet客戶(hù)端");
jLabel1.setBounds(new Rectangle(81, 22, 358, 38));
this.setBackground(UIManager.getColor("ComboBox.selectionBackground"));
this.setLayout(null);
jLabel2.setFont(new java.awt.Font("Dialog", 1, 16));
jLabel2.setForeground(Color.blue);
jLabel2.setText("輸入發(fā)送消息:");
jLabel2.setBounds(new Rectangle(35, 78, 124, 37));
jButton1.setBackground(UIManager.getColor("OptionPane.questionDialog.titlePane.background"));
jButton1.setBounds(new Rectangle(413, 77, 70, 39));
jButton1.setFont(new java.awt.Font("Dialog", 1, 16));
jButton1.setForeground(Color.blue);
jButton1.setToolTipText("點(diǎn)擊發(fā)送按鈕即可發(fā)送文本框中輸入的消息");
jButton1.setText("發(fā)送");
jButton1.addActionListener(new ChatClientApplet_jButton1_actionAdapter(this));
jTextField1.setBackground(new Color(182, 231, 223));
jTextField1.setFont(new java.awt.Font("Dialog", 0, 17));
jTextField1.setToolTipText("此文本框用來(lái)輸入發(fā)送的消息");
jTextField1.setBounds(new Rectangle(153, 80, 248, 35));
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.getViewport().setBackground(UIManager.getColor("MenuItem.acceleratorForeground"));
jScrollPane1.setBounds(new Rectangle(54, 130, 410, 231));
jTextArea1.setBackground(UIManager.getColor("Desktop.background"));
jTextArea1.setFont(new java.awt.Font("Dialog", 0, 18));
jTextArea1.setForeground(Color.black);
jLabel3.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel3.setForeground(Color.blue);
jLabel3.setText("如想離開(kāi)聊天室,可在發(fā)送框中輸入字符串leave并發(fā)送");
jLabel3.setBounds(new Rectangle(62, 367, 394, 24));
this.add(jLabel1, null);
this.add(jLabel2, null);
this.add(jTextField1, null);
this.add(jButton1, null);
this.add(jScrollPane1, null);
this.add(jLabel3, null);
jScrollPane1.getViewport().add(jTextArea1, null);
//啟動(dòng)監(jiān)聽(tīng)線(xiàn)程
new Thread(this).start();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void run()
{
try
{
while(true)
{
//監(jiān)聽(tīng)服務(wù)者發(fā)來(lái)的消息,線(xiàn)程將阻塞在該語(yǔ)句中,直到消息到來(lái)。
String s=m_in.readUTF(); //讀一個(gè)UTF格式字符串。
if(s!=null)
//將消息顯示在信息顯示窗口中。
jTextArea1.append(s+"\n");
}
}
catch(Exception e)
{
jTextArea1.append("Network problem or Sever down.\n");
jTextArea1.setVisible(false);
}
}
public void stop()
{
try
{
m_out.writeUTF("leave");
//如想離開(kāi)聊天室,可在發(fā)送框中輸入字符串leave
}
catch(IOException e){}
}
void jButton1_actionPerformed(ActionEvent e){
String b = jTextField1.getText();
jTextField1.setText("");
//將用戶(hù)輸入的消息發(fā)送給 Chat Server
try{
m_out.writeUTF(b); //向服務(wù)者發(fā)送一UTF格式字符串。
}
catch(IOException g){}
}
}
class ChatClientApplet_jButton1_actionAdapter implements java.awt.event.ActionListener {
ChatClientApplet adaptee;
ChatClientApplet_jButton1_actionAdapter(ChatClientApplet adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -