1. 誰的是基於java的聊天軟體開發
android 系統的所有APP 基本都是基於JAVA開發的,IOS 系統的APP是基於Objective-C。都是面向對象的編程語言。
2. 怎麼樣用JAVA做個聊天軟體
/**
* 基於UDP協議的聊天程序
*
* 2007.9.18
* */
//導入包
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
public class Chat extends JFrame implements ActionListener
{
//廣播地址或者對方的地址
public static final String sendIP = "127.0.0.1";
//發送埠9527
public static final int sendPort = 8000;
JPanel p = new JPanel();
List lst = new List(); //消息顯示
JTextField txtIP = new JTextField(18); //填寫IP地址
JTextField txtMSG = new JTextField(20); //填寫發送消息
JLabel lblIP = new JLabel("IP地址:");
JLabel lblMSG = new JLabel("消息:");
JButton btnSend = new JButton("發送");
byte [] buf;
//定義DatagramSocket的對象必須進行異常處理
//發送和接收數據報包的套接字
DatagramSocket ds = null;
//=============構造函數=====================
public Chat()
{
CreateInterFace();
//注冊消息框監聽器
txtMSG.addActionListener(this);
btnSend.addActionListener(this);
try
{
//埠:9527
ds =new DatagramSocket(sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
//============接受消息============
//匿名類
new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];
//表示接受數據報包
while(true)
{
try
{
DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);
ds.receive(dp);
lst.add("【消息來自】◆" + dp.getAddress().getHostAddress() + "◆"+"【說】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);
}
catch(Exception e)
{
if(ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
//關閉窗體事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.out.println("test");
int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
ds.close();//關閉ds對象//關閉數據報套接字
}
}
});
}
//界面設計布局
public void CreateInterFace()
{
this.add(lst,BorderLayout.CENTER);
this.add(p,BorderLayout.SOUTH);
p.add(lblIP);
p.add(txtIP);
p.add(lblMSG);
p.add(txtMSG);
p.add(btnSend);
txtIP.setText(sendIP);
//背景顏色
lst.setBackground(Color.yellow);
//JAVA默認風格
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setSize(600,500);
this.setTitle("〓聊天室〓");
this.setResizable(false);//不能改變窗體大小
this.setLocationRelativeTo(null);//窗體居中
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
txtMSG.requestFocus();//消息框得到焦點
}
//===============================Main函數===============================
public static void main(String[]args)
{
new Chat();
}
//================================發送消息===============================
//消息框回車發送消息事件
public void actionPerformed(ActionEvent e)
{
//得到文本內容
buf = txtMSG.getText().getBytes();
//判斷消息框是否為空
if (txtMSG.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"發送消息不能為空","提示",JOptionPane.WARNING_MESSAGE);
}
else{
try
{
InetAddress address = InetAddress.getByName(sendIP);
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
txtMSG.setText("");//清空消息框
//點發送按鈕發送消息事件
if(e.getSource()==btnSend)
{
buf = txtMSG.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
txtMSG.setText("");//清空消息框
txtMSG.requestFocus();
}
}
}
3. 跪求基於Java的即時聊天系統
/**伺服器端*/
import java.io.*;
import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Server extends JFrame implements Runnable ...{
private ServerSocket server;
private Socket connection;
private OutputStream output;
private InputStream input;
private Thread outThread;
private JTextArea display;
private JTextField text1;
private JButton startButton;
public static void main(String args[]) ...{
Server s = new Server();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Server() ...{
super("Server");
startButton = new JButton("Start the server");
text1 = new JTextField(20);
display = new JTextArea(7, 30);
display.setEditable(false);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(startButton,BorderLayout.NORTH);
container.add(new JScrollPane(display),BorderLayout.CENTER);
container.add(text1,BorderLayout.SOUTH);
/**//*
* 給開始按鈕添加監聽器。
*/
startButton.addActionListener(new ActionListener()...{
public void actionPerformed(ActionEvent e) ...{
display.setText("啟動伺服器... ");
startButton.setEnabled(false);
try ...{
//埠設為5000,最大連接請求為100個
server = new ServerSocket(5000, 100);
connection = server.accept();
output = connection.getOutputStream();
input = connection.getInputStream();
output.write("連接成功! ".getBytes());
outThread = new Thread(Server.this);
outThread.start();
} catch (IOException ee) ...{
}
}
});
/**//*
/*給文本域添加鍵盤監聽器,按回車發送信息。
*/
text1.addKeyListener(new KeyAdapter()...{
public void keyPressed(KeyEvent ke) ...{
if(ke.getKeyCode() == KeyEvent.VK_ENTER)...{
byte writeBytes[] = new byte[50];
String s = "Server: " + text1.getText() + "";
text1.setText("");
writeBytes = s.getBytes();
display.append(s+" ");
try ...{
output.write(writeBytes);
} catch (IOException ee) ...{
}
if (s.trim().equals("Server: exit")) ...{
outThread.stop();
quit();
}
}
}
});
setSize(300, 400);
setResizable(false);
setVisible(true);
}
public void run() ...{
while (true) ...{
byte readBytes[] = new byte[50];
try ...{
input.read(readBytes);//讀去對方發送的消息
} catch (IOException e) ...{
}
String s = new String(readBytes);
display.append(s+" ");
if (s.trim().equals("Client: exit"))
break;
}
quit();
}
public void quit() ...{
try ...{
output.close();
input.close();
connection.close();
} catch (IOException e) ...{
}
startButton.setEnabled(true);
}
}
/**//*
*客戶端
*/
import java.io.*;
import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Client extends JFrame implements Runnable ...{
private Socket client;
private OutputStream output;
private InputStream input;
private Thread outThread;
private JTextArea display;
private JTextField text1;
private JButton startButton;
private JMenu loginMenu = new JMenu("登錄");
private JMenuItem register = new JMenuItem("注冊");
private JMenuItem login = new JMenuItem("登錄");
private JMenuBar bar = new JMenuBar();
private Register registerDlg ;
private Login loginDlg;
private RandomAccessFile file;
public static void main(String args[]) ...{
Client c = new Client();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Client() ...{
super("Client");
startButton = new JButton("Connect to server");
text1 = new JTextField(20);
display = new JTextArea(7, 30);
display.setEditable(false);
loginMenu.add(register);
loginMenu.add(login);
bar.add(loginMenu);
setJMenuBar(bar);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(startButton,BorderLayout.NORTH);
container.add(new JScrollPane(display),BorderLayout.CENTER);
container.add(text1,BorderLayout.SOUTH);
try ...{
file = new RandomAccessFile(new File("E://login.txt"),"rw");
} catch (IOException e1) ...{
e1.printStackTrace();
}
registerDlg = new Register(this,file);
loginDlg = new Login(this,file);
startButton.addActionListener(new ActionListener()...{
public void actionPerformed(ActionEvent e) ...{
display.setText("連接伺服器...");
startButton.setEnabled(false);
try ...{
client = new Socket("127.0.0.1", 5000);
output = client.getOutputStream();
input = client.getInputStream();
outThread = new Thread(Client.this);
outThread.start();
} catch (IOException ee) ...{
}
}
});
text1.addKeyListener(new KeyAdapter()...{
public void keyPressed(KeyEvent ke) ...{
if(ke.getKeyCode() == KeyEvent.VK_ENTER)...{
byte writeBytes[] = new byte[50];
String s = loginDlg.getLoginName()+": " + text1.getText() + "";
text1.setText("");
writeBytes = s.getBytes();
display.append(s+" ");
try ...{
output.write(writeBytes);
} catch (IOException ee) ...{
}
if (s.trim().equals(loginDlg.getLoginName()+": exit")) ...{
outThread.stop();
quit();
}
}
}
});
register.addActionListener(new ActionListener()...{
public void actionPerformed(ActionEvent e) ...{
registerDlg.setVisible(true);
}
});
login.addActionListener(new ActionListener()...{
public void actionPerformed(ActionEvent e) ...{
loginDlg.setVisible(true);
}
});
setSize(300, 400);
setResizable(false);
setVisible(true);
}
public void run() ...{
while (true) ...{
byte readBytes[] = new byte[1024];
try ...{
input.read(readBytes);
} catch (IOException e) ...{
}
String s = new String(readBytes);
display.append(s+" ");
if (s.trim().equals("Server: exit"))
break;
}
quit();
}
public void quit() ...{
try ...{
output.close();
input.close();
client.close();
} catch (IOException e) ...{
}
startButton.setEnabled(true);
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Login extends JDialog...{
private JTextField textField;
private JButton loginButton;
private RandomAccessFile file;//保存注冊信息的文件
private String loginName = "guest";//保存登錄者的名字,為登陸為guest;
public Login(JFrame f,RandomAccessFile file)...{
super(f,"登陸",false);
this.file = file;
JPanel panel = new JPanel();
panel.add(new JLabel("昵稱:"));
textField = new JTextField(10);
panel.add(textField);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panel,BorderLayout.NORTH);
loginButton = new JButton("登陸");
container.add(loginButton,BorderLayout.SOUTH);
setVisible(false);
setBounds(100,200,200,200);
loginButton.addActionListener(new LoginListener());
}
public String getLoginName()...{
return loginName;
}
/**//*
* 登錄監聽器,當單擊登陸按鈕時,觸發該事件
* 從文件中讀取並查找是否注冊過,如果沒有找
* 到則彈出未注冊警告。否則彈出歡迎對話框表
* 示歡迎
*/
private class LoginListener implements ActionListener...{
public void actionPerformed(ActionEvent e) ...{
boolean flag = false;
try ...{
String name = textField.getText().trim();
textField.setText("");
file.seek(0);
while(file.getFilePointer() < file.length())...{
String nik = file.readUTF();
if(nik.equals(name))...{
flag = true;
loginName = name;
break;
}
}
if(!flag)...{
String warning="沒有找到你的賬號請先注冊!";
JOptionPane.showMessageDialog(Login.this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}else...{
String welcome="歡迎來聊天!";
JOptionPane.showMessageDialog(Login.this,welcome,"歡迎",JOptionPane.WARNING_MESSAGE);
}
Login.this.setVisible(false);
} catch (IOException e1) ...{
e1.printStackTrace();
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Register extends JDialog...{
private String nickName;
private JTextField textField;
private JButton registerButton;
private RandomAccessFile file;////保存注冊信息的文件
public Register(JFrame f,RandomAccessFile file)...{
super(f,"注冊",false);
this.file = file;
JPanel panel = new JPanel();
panel.add(new JLabel("昵稱:"));
textField = new JTextField(10);
panel.add(textField);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panel,BorderLayout.NORTH);
registerButton = new JButton("注冊");
container.add(registerButton,BorderLayout.SOUTH);
setVisible(false);
setBounds(100,200,200,200);
registerButton.addActionListener(new RegisterListener());
}
/**//*
* 注冊監聽器,當單擊登注冊按鈕時,觸發該事件
* 並向文件中寫入注冊信息。
*/
private class RegisterListener implements ActionListener...{
public void actionPerformed(ActionEvent e) ...{
try ...{
file.seek(file.length());
String str = textField.getText();
textField.setText("");
file.writeUTF(str);
Register.this.setVisible(false);
} catch (IOException e1) ...{
e1.printStackTrace();
}
}
}
}
4. java的一個簡單聊天室的系統結構,大概的跟我說一下。
定義要求:⑴完善的支持中文。由於Java編譯器版本及運行環境的差異等原因,在Java語言的中文處理中常出現亂碼等現象,表現在中文顯示與網路傳輸不正常、中文不能輸入等方面。在這個聊天室系統中我們將徹底解決中文的兼容性問題。⑵具有健壯性。即聊天室系統能夠處理各種異常,能夠識別和控制客戶端的各種行為,能夠返回清理不正常退出後所分配的系統資源,能夠踢出超時連接用戶以減輕伺服器負載等。雖然Java語言本身能夠自動收集處理無用的對象,但我們仍然需要作一定的清理工作。⑶廣泛的適應性。因為我們不能要求聊天用戶必須使用某種瀏覽器或操作系統,因此所編寫的Java程序,尤其是Applet,必須能在各種平台的各個版本的瀏覽器上都能正常運行。考慮到網路用戶的使用情況,我們定的標準是能適應以下版本的瀏覽器:Netscape 3.x,Netscape Communicator 4.x,Internet Explorer 3.x、4.x、5.x中英文版。設計實現:基於Java的Web聊天室系統包括聊天伺服器和客戶端兩部分。聊天伺服器是一個Java Application,與Web伺服器程序運行在同一機器上。客戶端部分即是一個含Java Applet的HTML頁面,它由Web伺服器傳送給客戶端瀏覽器,交由瀏覽器的Java虛擬機(VM)解釋執行。該Applet初始化後與聊天伺服器進行連接,聊天伺服器對於每個連接請求產生一個連接線程(Connection Thread),來維護和管理與該客戶端的會話。客戶端的發言被傳送到伺服器端後由其向其他客戶進行廣播(Broadcast),達到相互聊天的目的。 在聊天伺服器中,我們使用哈希表(Hashtable)來存儲所有的連接線程。主線程為ChatServer,對於每個新的客戶連接請求產生一個Connection線程。同時我們還運行了一個檢查線程CheckActiveTimer,它相當於一個定時器,每隔一定時間就掃描所有的客戶連接線程(即掃描Hashtable),檢查每個客戶連接是否超時(例如很長時間沒有發言或者死機),並給出警告或直接踢出(Kick)用戶。
5. 基於java的網路聊天系統
我有 自己寫好的JAR包 QQ傳給你 QQ:419000662
請查收~~~
收到請給分呵呵~~~
6. 誰能幫我注釋一下下面的java代碼,是基於java的聊天系統的server的代碼
import mybean.Logininfor;
import setting.Embody;
public class serve {
private ServerSocket serversocket;
private Socket socket = new Socket();
private work work;
private ArrayList arraylist = null;
MyQQServerMain serverMain;
public static serve serve = new serve();
private Vector clientsfind = null;
public static Hashtable<String, Socket> socketsHashtable = new Hashtable<String, Socket>();
public ArrayList getArrayList() {
return this.arraylist;
}
/**
* 獲取伺服器IP
*/
public static InetAddress getLocalHost() throws UnknownHostException {
InetAddress IP = InetAddress.getLocalHost();
return IP;
}
/**
* 啟動伺服器
*/
public void start() {
try {
serversocket = new ServerSocket(setting.Command.server_port);
serverMain = new MyQQServerMain();
//這里讓那個面板置為可見,可見上面是啟動的一個面板啊
serverMain.setVisible(true);
//在內容顯示的地方列印出來服務啟動的基本信息
serverMain.getJTextArea().append(
serve.getTime()+" " + "伺服器啟動" + "\n" + "伺服器IP是:"
+ (getLocalHost().getHostAddress()) + "\n");
socket.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "此埠被佔用,系統將退出"); // 出錯,列印出錯信息
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 啟動線程
*/
public void startThread() {
while (true)// 死循環 一直監聽有沒有客戶端和伺服器建立連接
{
try {
socket = serversocket.accept();
serverMain.getJTextArea().append(
serve.getTime()+" "+ "客戶端開始連接伺服器:" + "\n");
serverMain.getJTextArea().selectAll();
work = new work(serve, socket);
// 對象構造Work
work.start();// 啟動線程;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我看了下,這真沒什麼好注釋的了說的多詳細啊。
7. 高手進,java實現聊天功能
。。。這個我以前也遇到過、不過我是用C#寫的、不過後來也有java的、你可以、使用Swing做的簡單界面,及使用Socket套接字實現簡單聊天 。。。。。。但是、我不知道你問的是C/S模式還是B/S 模式?
其中、B/S模式可以用Servlet來實現,思路是通過Context上下文綁定參數實現
而C/S模式的,是通過RMI遠程調用的方法實現的。。。先給你個C/S模式的核心代碼。。。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;public class Server extends JFrame
{
public static void main(String [] args)
{
Server server=new Server();
//設定框架的關閉方式
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//顯示框架
server.setVisible(true);
server.pack();
CreateServer cs=new CreateServer(server);
}
// 設定框架的寬度和高度
private static final int WIDTH=450;
private static final int HEIGHT=450;
// 聊天信息框
JTextArea mainArea=new JTextArea(12,35);
// 發送信息的填寫框
JTextArea sendArea=new JTextArea(5,30);
// 構造函數
public Server()
{
//定位框架
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();//獲取電腦當前解析度
int width=screenSize.width;
int height=screenSize.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
//設置窗口顯示位置
setLocation(x,y);
//設置框架大小
setSize(WIDTH,HEIGHT);
//設置標題
setTitle("小新新聊天伺服器");
//設置窗口的自定義大小
setResizable(false);
//在內容表格上創建3個面板並加入到內容表格
Container con=this.getContentPane();
JPanel labelPanel=new LabelPanel();
con.add(labelPanel,BorderLayout.NORTH);
JPanel contentPanel=new ContentPanel();
con.add(contentPanel,BorderLayout.CENTER);
JPanel sendPanel=new SendPanel();
con.add(sendPanel,BorderLayout.SOUTH);
}
//聊天窗口的標題面板
class LabelPanel extends JPanel
{
public LabelPanel()
{
Font font=new Font("Dialog",Font.BOLD,18);
JLabel label=new JLabel("歡迎使用小新新聊天伺服器");
label.setFont(font);
this.add(label);
}
}
// 聊天信息查看面板
//該面板內的區域為不可編輯區域
class ContentPanel extends JPanel
{
public ContentPanel()
{
FlowLayout fl=new FlowLayout(FlowLayout.CENTER);
this.setLayout(fl);
mainArea.setLineWrap(true);
mainArea.setEditable(false);
JScrollPane scrollPanel=new JScrollPane(mainArea);
this.add(scrollPanel);
}
}
// 填寫發送信息的面板
class SendPanel extends JPanel
{
public SendPanel()
{
//面板的組件之間水平分隔15像素,垂直間距10像素
FlowLayout layout=new FlowLayout(FlowLayout.LEFT,15,10);
this.setLayout(layout);
sendArea.setLineWrap(true);
JScrollPane scrollPanel=new JScrollPane(sendArea);
this.add(scrollPanel);
JButton send=new JButton("發送");
this.add(send);
//對發送按鈕注冊動作監聽器
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String msg=sendArea.getText();
if(!msg.equals(""))
{
mainArea.append("【伺服器】:"+msg+"\n");
sendArea.setText("");
CreateServer.sendMsg(msg);
}
else
{
return;
}
}
});
}
}
}
//創建伺服器ServerSocket的類
class CreateServer extends Thread
{
private Server server;
private static BufferedReader in=null;//存儲客戶端發送到伺服器的數據
private static PrintWriter out=null;//存儲伺服器發送到客戶端的數據
private Socket socket=null;//等待客戶端連接socket
private ServerSocket ss=null;//開啟伺服器socket連接
//構造函數
public CreateServer(Server s)
{
this.server=s;
try
{
ss=new ServerSocket(2345);
System.out.println("伺服器成功啟動...!");
socket=ss.accept();//等待客戶端請求
//獲取輸入到伺服器的數據
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//獲取輸出到客戶端的數據
out=new PrintWriter(socket.getOutputStream(),true);
out.println("你好!");
}
catch(Exception r)
{
r.printStackTrace();
}
this.start();//啟動線程
}
//實現信息發送到客戶端的發送方法
public static void sendMsg(String s)
{
try
{
out.println("【伺服器】:"+s+"\n");
}
catch(Exception e)
{
System.out.println("發送信息失敗...!!");
e.printStackTrace();
}
}
// 線程Thread類的run方法實現對客戶端發送來的數據監聽
//線程啟動後開始該方法,執行線程體
public void run()
{
String msg="";
while(true)
{
try
{
msg=in.readLine();
//Thread.sleep(500);//線程睡眠
}
catch (SocketException ex)
{
ex.printStackTrace();
break;
}
catch(IOException r)
{
r.printStackTrace();
break;
}
//若從客戶端獲取的信息不為空對象也不為空串
//則把信息顯示在聊天信息文本域
if(msg!=null && msg.trim()!="")
{
server.mainArea.append(msg+"\n");
}
}
}
}
8. java聊天軟體設計論文
java聊天軟體設計論文
摘 要
隨著互聯網的快速發展,網路聊天工具已經作為一種重要的信息交流工具,受到越來越多的網民的青睞。目前,出現了很多非常不錯的聊天工具,其中應用比較廣泛的有Netmeeting、騰訊QQ、MSN-Messager等等。該系統開發主要包括一個網路聊天伺服器程序和一個網路聊天客戶程序兩個方面。前者通過Socket套接字建立伺服器,伺服器能讀取、轉發客戶端發來信息,並能刷新用戶列表。後者通過與伺服器建立連接,來進行客戶端與客戶端的信息交流。其中用到了區域網通信機制的原理,通過直接繼承Thread類來建立多線程。開發中利用了計算機網路編程的基本理論知識,如TCP/IP協議、客戶端/伺服器端模式(Client/Server模式)、網路編程的設計方法等。在網路編程中對信息的讀取、發送,是利用流來實現信息的交換,其中介紹了對實現一個系統的信息流的分析,包含了一些基本的軟體工程的方法。經過分析這些情況,該區域網聊天工具採用Eclipse為基本開發環境和java語言進行編寫,首先可在短時間內建立系統應用原型,然後,對初始原型系統進行不斷修正和改進,直到形成可行系統
關鍵詞:區域網 聊天 socket java
1 緒論
隨著互聯網逐步普及,人們的生活和工作也越來越離不開信息網路的支持, 而聊天室是人們最常見, 最直接的網上交流的方式。本聊天系統以聊天交流為主,為廣大用戶提供一個藉助網路進行人際交往的平台,也是網路與現實最貼近的實用型網站。本文所介紹的網路聊天系統是基於開放的JAVA應用程序開發設計的,其主要特性是能動態、實時的完成信息的傳遞,且具有高效的交互性,更有效的處理客戶請求,易於維護和更新,其運行所需環境及其工作流程和各個功能控制項的工作原理將在本文依次介紹,並且文中提供了部分程序源代碼。
2 規劃設計
2.1 課題來源
根據當前網路的需求,網路聊天越來越受各種網民所青睞。因此開發網路聊天是相當有必要,而且在網站內增加聊天功能,它不僅可以提高網站的訪問量,同時可以留著訪客,更重要的是讓訪客透過聊天室實時的互相交流。而本人也學習過JAVA語言,對網路編程也較有興趣,為了更好的考驗自己對JAVA語言的掌握程度,本人就決定以《基於JAVA聊天設計與實現》為畢業設計,希望通過這一次的能進一步提高本人的網路開發編程的能力。
2.2需求分析
聊天系統不外乎兩個方面,伺服器端和客戶端。簡單分析一下兩個方面所要完成的任務,對設計這個程序來說,等於完成了一半。首先來看一下伺服器端的任務:
1.伺服器端應當建立一個ServerSocket,並且不斷進行偵聽是否有客戶端連接或者斷開連接(包括判斷沒有響應的連接超時)。
2.伺服器端應當是一個信息發送中心,所有客戶端的信息都傳到伺服器端,由伺服器端根據要求分發信息。
以上就是伺服器端最主要的兩個任務。不難看出,伺服器端的任務並不復雜。
客戶端應該完成的工作包括:
1.與伺服器端建立通信通道,向伺服器端發送信息。
2.接收來自伺服器的信息。
相對伺服器而言,客戶端的任務更加簡單,有了以上的簡單分析,可以知道,解決上述四個問題,即完成了該聊天系統的核心。
3 系統分析與設計方案
3.1 聊天系統的總體設計要點
聊天系統的設計跟普通網站設計有著許多不同的地方,普通網站設計所考慮的因素,例如,普通網站需要對布局進入大量美化以及動畫設計等等,而聊天室只要提供滿足訪客雙方直接實時聊天即可。因此,在設計聊天系統的過程中,必須要考慮好以下幾個設計要點:
1、實現思想
在Internet上的聊天程序一般都是以伺服器提供服務端連接響應,使用者通過客戶端程序登錄到伺服器,就可以與登錄在同一伺服器上的用戶交談,這是一個面向連接的通信過程。因此,程序要在TCP/IP環境下,實現伺服器端和客戶端兩部分程序。
2、伺服器端工作流程
伺服器端通過socket()系統調用創建一個Socket數組後(即設定了接受連接客戶的最大數目),與指定的本地埠綁定bind(),就可以在埠進行偵聽listen()。如果有客戶端連接請求,則在數組中選擇一個空Socket,將客戶端地址賦給這個Socket。然後登錄成功的客戶就可以在伺服器上聊天了。
3、客戶端工作流程
客戶端程序相對簡單,只需要建立一個Socket與伺服器端連接,成功後通過這個Socket來發送和接收數據就可以了。