導航:首頁 > 編程語言 > java聊天系統

java聊天系統

發布時間:2025-07-09 10:14:40

❶ 用java怎麼開發聊天軟體

做界面的話可以有b/s,c/s結構兩種。b/s就是瀏覽器/伺服器的方式,用web的方式進行聊天。c/s就是client/server的方式,桌面程序樣子的,一般都是這個方式。

java做桌面界面的一般有awt/swing/swt三種類庫。最流行的是swing,老一點的是awt,如果你想做的界面和操作系統的界面一直(感官一樣),那就用swt,swt是eclipse基金會為了開發eclipse而開發的java利用操作系統原生窗口的一組api,更快速方便。

如果你只是完成任務,就看看swing吧,很好的java圖形庫。另外,java基礎知識也要很好才行。要能夠語音視頻的話,還要學jmf,這個比較難一點。

針對區域網的,那就另加一些socket編程的知識了

❷ 急需一個用java 語言寫的聊天程序

客戶端:
package chatroom;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class client extends JFrame implements ActionListener,Runnable{
JPanel conn,text,send;
JLabel name,sendto;
JComboBox list;
JButton con,snd,clear;
JTextArea talk;
JTextField who,say;
jscrollPane jsp;
Socket client;
InputStream is;
OutputStream os;
PrintStream ps;
BufferedReader br;
String receive,yousay;
Thread th;
DataInputStream dis;
Double tof;

client()
{
super("聊天室客戶端");
this.setSize(800,600);
this.setResizable(false);
conn=new JPanel();
text=new JPanel();
send=new JPanel();
this.getContentPane().add(conn);
conn.setBounds(0, 0, this.getWidth(),50);
name=new JLabel("姓名:");
who=new JTextField();
con=new JButton("連接");
conn.setLayout(null);
conn.add(name);
name.setBounds(30, 10, 50, 25);
conn.add(who);
who.setBounds(80, 10, 150, 25);
conn.add(con);
con.setBounds(250,10, 60, 25);

this.getContentPane().add(text);
text.setBounds(0,50,this.getWidth(),450);
text.setLayout(new BorderLayout());
jsp=new JScrollPane();
talk=new JTextArea();
jsp.getViewport().setView(talk);
text.add(jsp,"Center");
talk.setLineWrap(true);

this.getContentPane().add(send);
send.setLayout(null);
send.setBounds(0, 480, this.getWidth(), 150);
sendto=new JLabel("發送到:");
snd=new JButton("發送");
list=new JComboBox();
say=new JTextField();
clear=new JButton("清空");
send.add(sendto);
sendto.setBounds(30, 525, 50, 25);
send.add(list);
list.setBounds(100, 525, 75, 25);
send.add(say);
say.setEditable(true);
say.setBounds(200, 525, 300, 25);
send.add(snd);
snd.setBounds(520, 525, 100, 25);
send.add(clear);
clear.setBounds(650, 525, 100, 25);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
con.addActionListener(this);
snd.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("連接"))
{
try
{
client=new Socket(InetAddress.getLocalHost(),55555);
talk.append("連接成功...\n");
con.setText("斷開");
is=client.getInputStream();
os=client.getOutputStream();
th=new Thread(this);
String id=who.getText();
byte b[]=id.getBytes();
DataOutputStream dos=new DataOutputStream(os);
int len=b.length;
dos.writeInt(len);
dos.write(b);
th.start();

}catch(Exception e){talk.append("連接失敗\n"+e.toString()+"0000");}
}
else if(ae.getSource()==snd)
{
if(list.getSelectedItem().toString()=="所有人")
{
yousay="@open"+say.getText();
byte b[]=yousay.getBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=b.length;
dos.writeInt(len);
dos.write(b);
}catch(Exception e)
{
System.out.print(e.toString()+"1111");
}
}
else
{
yousay="@whisper"+say.getText();
byte b[]=yousay.getBytes();
byte w[]=list.getSelectedItem().toString().getBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=b.length;
int wlen=w.length;
dos.writeInt(len); //內容
dos.write(b);
dos.writeInt(wlen); //發送對象
dos.write(w);
}catch(Exception e)
{
System.out.print(e.toString()+"AAAA");
}
}
}
else if(ae.getActionCommand().equals("斷開"))
{
try
{
client.close();
talk.append("連接已斷開\n");
con.setText("連接");
}catch(Exception e){System.out.println(e.toString()+"2222");}
}
}
public void run()
{

while(true)
{
try
{
dis=new DataInputStream(is);
tof=dis.readDouble();
if(tof==1.1)
{
dis=new DataInputStream(is);
int number;
list.removeAllItems();
list.addItem("所有人");
number=dis.readInt();
for(int i=0;i<number;i++)
{
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String name=new String(b);
list.addItem(name);
}

}
else if(tof==2.2)
{
try
{
dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
receive=new String(b);
talk.append(receive+"\n");
}catch(Exception e){JOptionPane.showMessageDialog(this, "連接已斷開","消息",JOptionPane.INFORMATION_MESSAGE);break;}
}
}catch(Exception e){JOptionPane.showMessageDialog(this, "連接已斷開","消息",JOptionPane.INFORMATION_MESSAGE);break;}
}
}

public static void main(String[] args) {
client cl=new client();

}

}
服務端:
package chatroom;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.util.Vector;
import java.io.*;
public class server extends JFrame implements Runnable{
JPanel jp1;
JTextArea jta;
JScrollPane jsp;
Socket socket=null;
ServerSocket server;
InputStream is;
OutputStream os;
static int i=0,login;
int no;
static String s="";
Thread sr;
Thread th[]=new Thread[20];
static Vector ol=new Vector();
static public byte w[]=null;
static String from=""; //人名
static String to="";
static String fromtext=""; //內容
static String totext="";
server()
{
super("聊天室服務端");
this.getContentPane().setLayout(new GridLayout(1,1));
this.setSize(400,300);
jp1=new JPanel();
jta=new JTextArea();
jsp=new JScrollPane();
this.getContentPane().add(jp1);
jp1.setLayout(new GridLayout(1,1));
jsp.getViewport().setView(jta);
jp1.add(jsp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}
public static void main(String[] args) {
server sr=new server();
sr.sr=new Thread(sr);
sr.sr.start();
sendtoall sta=new sendtoall();
returnfrom rf=new returnfrom(sr);
returnto rt=new returnto(sr);
sta.start();
rf.start();
rt.start();

}
public void run()
{
try
{
server=new ServerSocket(55555);
while(true)
{
socket=server.accept();
is=socket.getInputStream();
DataInputStream dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String id=new String(b);
record v=new record(id,socket);
ol.addElement(v);
for (int i=0;i<ol.size();i++)
{
if (ol.elementAt(i).equals(v))
{
no=i;
}
}
login=1;
s=id+"已來到聊天室!";
th[no]=new Thread(new receive(this,no));
th[no].start();
}
}catch(Exception e){System.out.println(e.toString()+"aaaa");}

}
}
class receive implements Runnable
{
InputStream is;
OutputStream os;
server sr;
Socket sk;
int i;
String name;
String ip;
receive(server sr,int i)
{
this.sr=sr;
this.i=i;
sk=((record)sr.ol.elementAt(i)).ip;
name=((record)sr.ol.elementAt(i)).name;
ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();
}
public void run()
{
while(true)
{
try
{
is=sk.getInputStream();
DataInputStream dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String abc=new String(b);
sr.jta.append(abc);
if(abc.substring(0,5).equals("@open"))
{

server.s=name+"["+ip.substring(1, ip.length())+"]"+"說:"+abc.substring(5,abc.length());
sr.jta.append(server.s+"\n");
}
else if(abc.substring(0,8).equals("@whisper"))
{
int wlen=dis.readInt();
sr.w=new byte[wlen];
dis.read(sr.w);
server.to=new String(sr.w);
server.from=((record)sr.ol.elementAt(i)).name;
String ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();
// server.s=server.from+"對"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地說:"+abc.substring(8,abc.length());
server.fromtext="你對"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地說:"+abc.substring(8,abc.length());
server.totext=server.from+"["+ip.substring(1, ip.length())+"]"+"對你悄悄地說:"+abc.substring(8,abc.length());
sr.jta.append(server.s+"\n");
}
}catch(Exception e)
{
try
{
DataOutputStream dos=new DataOutputStream(os);
server.ol.removeElementAt(i);
server.s=name+"已離開聊天室.";
server.login=1;
break;
}catch(Exception f){}
}
}
}
}
class sendtoall extends Thread
{
int len,number;
byte b[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
public void run()
{
while(true)
{
try
{
if(server.login==1)
{
number=0;
number=server.ol.size();
dos=new DataOutputStream(os);
for(int i=0;i<server.ol.size();i++)
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
dos=new DataOutputStream(os);
dos.writeDouble(1.1);
dos.writeInt(number);
for (int j=0;j<number;j++)
{
String name=((record)sr.ol.elementAt(j)).name;
byte b[]=name.getBytes();
int len=b.length;
dos.writeInt(len);
dos.write(b);
}
}
server.login=0;

}
else if(!server.s.equals("")&&sr.ol.size()>0)
{
b=server.s.getBytes(); //String類型中 漢字佔一個位元組,但是byte類型中,漢字占兩個位元組
len=b.length;
//len=server.s.length();
//b=new byte[len];
//b=server.s.getBytes();
for(int i=0;i<server.ol.size();i++)
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(len);
dos.write(b);
}
server.s="";

}

}catch(Exception e){System.out.println(e.toString()+"sdasd");}
}
}
}
class returnfrom extends Thread
{
int len,wlen;
byte b[];
byte w[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
//String from="",to="";
returnfrom(server sr)
{
this.sr=sr;
}
public void run()
{
while(true)
{
if(!server.fromtext.equals(""))
{
b=server.fromtext.getBytes();
len=b.length;
sr.jta.append(sr.fromtext);
try
{
for(int i=0;i<server.ol.size();i++)
{
if(((record)sr.ol.elementAt(i)).name.equals(server.from))
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(len);
dos.write(b);
}
}
}catch(Exception e){System.out.println(e.toString()+"wwww");}
server.fromtext="";
server.from="";
}
}

}
}

class returnto extends Thread
{
int len,wlen;
byte b[];
byte w[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
//String from="",to="";
returnto (server sr)
{
this.sr=sr;
}
public void run()
{
while(true)
{
if(!server.totext.equals(""))
{
w=server.totext.getBytes();
wlen=w.length;
try
{
for(int i=0;i<server.ol.size();i++)
{
if(((record)sr.ol.elementAt(i)).name.equals(server.to))
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(wlen);
dos.write(w);
}
}
}catch(Exception e){System.out.println(e.toString()+"wwww");}
server.totext="";
server.to="";
}
}
}
}

class record
{
String name;
Socket ip;
record(String id,Socket socket)
{
name=id;
ip=socket;
}
}

以前學習java時寫的,是個在線聊天室只有群聊,私聊,上下線提示,搞不好裡面還有錯誤- -其他功能自己看著加吧,你要的功能實在是很好很強大.

❸ 怎麼實現java一對一聊天室可以介紹一下嗎

要實現 Java 的一對一聊天室,可以使用 Socket 編程實現基於 TCP 協議的通信。

以下是一些大致的步驟:

❹ java實現聊天室是怎麼做到的

Java 實現聊天室可以分為以下幾個步驟:
建立伺服器端
首先需要建立一個伺服器端,負責接收客戶端的連接請求並處理客戶端發送過來的消息。
建立客戶端
然後需要建立冊陸客戶端,客戶端通過正姿畢網路連接到伺服器端,並向伺服器端發送消息。
實現通信協議
為了實現聊天室功能,需要定義一個通信協議,規定客戶端和伺服器端之間的通信格式,例如消息的頭部和內容等。
實現多線程處理
聊天室通常會有多個用戶同時在線,因此需要使用多線程舉芹來處理多個客戶端的連接請求和消息傳遞。
實現GUI界面(可選)
為了方便用戶使用,可以實現一個GUI界面,讓用戶可以方便地發送和接收消息。
以下是一個簡單的 Java 聊天室的代碼示例:
java
Copy code
// 伺服器端代碼
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
Socket socket = serverSocket.accept();
new Thread(new ServerThread(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerThread implements Runnable {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public ServerThread(Socket socket) {
this.socket = socket;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String msg;
while ((msg = reader.readLine()) != null) {
// 處理客戶端發送過來的消息
System.out.println("接收到消息:" + msg);
// 將消息發送給所有客戶端
for (Socket s : ServerThreadList.getList()) {
if (s != socket) {
PrintWriter w = new PrintWriter(s.getOutputStream());
w.println(msg);
w.flush();
}
}
}
// 關閉連接
socket.close();
ServerThreadList.removeThread(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerThreadList {
private static List
list = new ArrayList<>();
public static void addThread(ServerThread thread) {
list.add(thread);
}
public static void removeThread(ServerThread thread) {
list.remove(thread);
}
public static List
getList() {
return list;
}
}
// 客戶端代碼
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
new Thread(new ClientThread(socket)).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = reader.readLine();
writer.println(msg);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientThread implements Runnable {
private Socket socket;
private BufferedReader reader;
public ClientThread(Socket socket) {
this.socket = socket;
try {
reader

❺ 求教編程做出一個兩人多人之間簡單的聊天程序

製作人: CK.y ,匯成建築企業管理Q:610089144
僅供參考,大家要學會自己製作,很有成就感的啊!! 用 Java 作後台,開發一個 C / S 架構的多人聊天程序。首先,設計用戶界面。

一、界面設計

界面的元件全部使用 Flash CS3 自帶的組件:
首先,放入 TextInput 組件(實例名 input_txt),作為用戶輸入;
再放入 Button 組件(實例名 submit_btn),用於提交輸入的信息;
最後放入 TextArea 組件(實例名 output_txt),顯示聊天信息。

二、組件參數初始化
由於客戶端代碼不是很多,我們這次就寫在動作幀上:
// ************ 組件參數初始化 ************
submit_btn.label = "發送消息";
output_txt.editable = false;

// 設置各組件中字體的大小
input_txt.setStyle("textFormat", new TextFormat(null, 15));
output_txt.setStyle("textFormat", new TextFormat(null, 15));
submit_btn.setStyle("textFormat", new TextFormat(null, 15, null, true));

// 當按下回車或點擊 submit_btn 按鈕後調用事件處理函數
submit_btn.addEventListener(MouseEvent.CLICK, sendMessage);
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

// 事件處理函數
function onKeyDown(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.ENTER) {
sendMessage(null);
}
}

function sendMessage(evt:Event):void {
// 測試:將 input_txt 的內容輸出到 output_txt 中
output_txt.appendText(input_txt.text + "\n");

// 清空 input_txt,並設置焦點到 input_txt
input_txt.text = "";
stage.focus = input_txt;
}

三、編寫客戶端 Socket
1. 首先,Socket 連接非常簡單:
var socket:Socket = new Socket();
socket.connect("127.0.0.1", 8888);
其中 connect() 方法中的兩個參數分別為是主機名和埠號(埠號盡量用 1024 以上)。好了,這樣就連接上了。接下來是讀寫的問題。

2. 向伺服器端寫入字元串:我們在 sendMessage() 方法中進行寫入操作,注意寫出的字串必需以回車(\n)結束:
function sendMessage(evt:Event):void {
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(input_txt.text + "\n");
socket.writeBytes(bytes);
socket.flush();

// 清空 input_txt,並設置焦點到 input_txt
input_txt.text = "";
stage.focus = input_txt;
}

3. 讀取伺服器端寫回的字元串:最後將伺服器發回的字元串輸出到 output_txt 文本域中:
// 當客戶端 socket 收到數據後會調用 readMessage() 函數
socket.addEventListener(ProgressEvent.SOCKET_DATA, readMessage);

function readMessage(evt:ProgressEvent):void {
output_txt.appendText(socket.readUTF() + "\n");
}

四、Flash 客戶端全部腳本
// ************ 組件參數初始化 ************
submit_btn.label = "發送消息";
output_txt.editable = false;

input_txt.setStyle("textFormat", new TextFormat(null, 15));
output_txt.setStyle("textFormat", new TextFormat(null, 15));
submit_btn.setStyle("textFormat", new TextFormat(null, 15, null, true));

submit_btn.addEventListener(MouseEvent.CLICK, sendMessage);
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

function onKeyDown(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.ENTER) {
sendMessage(null);
}
}

function sendMessage(evt:Event):void {
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(input_txt.text + "\n");
socket.writeBytes(bytes);
socket.flush();

input_txt.text = "";
stage.focus = input_txt;
}

// ************ 客戶端 Socket ************
var socket:Socket = new Socket();
socket.connect("127.0.0.1", 8888);
socket.addEventListener(ProgressEvent.SOCKET_DATA, readMessage);

function readMessage(evt:ProgressEvent):void {
output_txt.appendText(socket.readUTF() + "\n");
}

五、編寫 Java 伺服器端 Socket
最後,我們需要編寫 Java 後台伺服器端的代碼。
首先,創建一個 ServerSocket 作為Socket 伺服器。當有客戶端連接後通過 accept() 方法即可得到客戶端的 Socket:

ServerSocket socketServer = new ServerSocket(8888);
System.out.println("伺服器已啟動,等待客戶端連接");

// accept() 方法是阻塞式的,當有客戶端連接成功後才繼續執行
Socket socket = socketServer.accept();
System.out.println("客戶端連接成功");

然後得到與客戶端的輸入流和輸出流(輸入流是客戶端連接到伺服器的管道,輸出流則是伺服器到客戶端的管道):
// 獲得輸入流和輸出流,輸入流為 BufferedReader 類型,輸出流為 DataOutputStream 類型
BufferedReader reader =
new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
DataOutputStream writer = new DataOutputStream(socket.getOutputStream());

拿到輸入輸出流以後,就可以通過輸入流(InputStream)讀取 Flash 客戶端發來的字元串,通過輸出流(OutputStream)向 Flash 客戶端寫字元串:
while (true) {
// readLine() 方法也是阻塞式的,當客戶端有消息發來就讀取,否則就一直等待
String msg = reader.readLine();

// 當客戶端發送的字元串為 null 時,說明客戶端已經關閉,此時退出循環
if (msg == null) {
System.out.println("客戶端已離開");
break;
}

// 將讀入的信息加工後再寫回客戶端
writer.writeUTF("寫回客戶端的" + msg);
}

以上是ServerSocket 與 AS 3 Socket 通信的基本原理。在實際應用中,會有多個客戶端連接這個ServerSocket,因此要創建一個多線程的 Socket 伺服器。
下面簡述一下多線程 Socket 伺服器原理:當socketServer.accept() 之後就需要實例化一個線程對象,在該對象中持有socketServer.accept() 返回的 Socket 對象,然後讓線程跑起來執行讀寫操作。如果再來一個客戶端就再跑一個線程,同樣執行讀寫操作。同時,用一個 List 容器來管理這些對象。
最終伺服器端的代碼如下:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FlashScoket {
private List<Client> clientList = new ArrayList<Client>();

public static void main(String[] args) {
new FlashScoket().runSocket();
}

private void runSocket() {
try {
ServerSocket socketServer = new ServerSocket(8888);
System.out.println("伺服器已啟動,等待客戶端連接");

while (true) {
// accept() 方法是阻塞式的,當有客戶端連接成功後才繼續執行
Socket socket = socketServer.accept();
System.out.println("客戶端連接成功");

// 實例化一個 Client 對象,並啟動該線程
Client client = new Client(socket);
clientList.add(client);
client.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

class Client extends Thread {
private Socket socket;

private BufferedReader reader;
private DataOutputStream writer;

private Client(Socket socket) {
this.socket = socket;
try {
// 獲得輸入流和輸出流,輸入流為 BufferedReader 類型,輸出流為 DataOutputStream 類型
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
writer = new DataOutputStream(socket.getOutputStream());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
try {
while (true) {
// readLine() 方法也是阻塞式的,當客戶端有消息發來就讀取,否則就一直等待
String msg = reader.readLine();

// 當客戶端發送的字元串為 null 時,說明客戶端已經關閉,此時退出循環
if (msg == null) {
clientList.remove(this);
System.out.println("客戶端已離開");
break;
}

// 將讀入的內容寫給每個客戶端
for (Iterator<Client> it = clientList.iterator(); it.hasNext();) {
Client client = it.next();
client.getWriter().writeUTF(msg);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉 socket 及相關資源
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

public DataOutputStream getWriter() {
return writer;
}
}
}

六、補充技術
1. 如何測試?
* 首先要編譯FlashScoket —— javac FlashScoket
* 然後啟動FlashScoket —— java FlashScoket
* 最後將 Flash 發布為 exe 文件格式,同時開啟多個即可。

2. 自動跟蹤到最後一行:當收到新的消息時自動滾動到最後一行,在 readMessage() 方法中加入:
output_txt.verticalScrollBar.scrollPosition = output_txt.verticalScrollBar.maxScrollPosition;

3. 當出現輸入/輸出錯誤並導致發送或載入操作失敗時提示用戶,加入對IO_ERROR 的偵聽:
socket.addEventListener(IOErrorEvent.IO_ERROR, socketIOError);
function socketIOError(evt:IOErrorEvent):void {
output_txt.appendText("伺服器端尚未開啟,請稍後再試\n");
}

4. 在sendMessage() 中加入對空字元串的驗證,如果為空則 return:
if (input_txt.text == "") {
return;
}

5. 在消息前面顯示用戶名:大家可以製作一個登錄頁面,輸入用戶名,假設已將輸入的用戶名存放在 userName 變數中,在寫入時加進去:
var userName:String = "FL 車在臣";
在 sendMessage() 中相應改為:
bytes.writeUTFBytes(userName + " : " + input_txt.text + "\n");

6. 使用output_txt .htmlText 輸出文字,那麼相應的寫入格式可以調整為:
bytes.writeUTFBytes("<font color='#0000FF'>" + userName + " : </font>" + input_txt.text + "\n");

❻ Java即時通訊IM聊天軟體仿微信APP源碼解析

Java即時通訊IM聊天軟體仿微信APP源碼解析

閱讀全文

與java聊天系統相關的資料

熱點內容
javascript轉化為數值 瀏覽:173
l3agent配置文件 瀏覽:521
element3d文字教程 瀏覽:434
pages文件能在word里打開么 瀏覽:53
金鏟鏟配置文件下載失敗客戶端無法啟動 瀏覽:387
c按行讀取word 瀏覽:310
4g網路怎麼變成2g了 瀏覽:197
電腦安裝安卓50 瀏覽:604
網路協議分析pdf 瀏覽:945
手機傳輸到u盤文件容易損壞 瀏覽:175
安卓tcpsocketsend 瀏覽:527
在哪裡下載輕草app 瀏覽:673
java文件名下載 瀏覽:335
深圳華強北蘋果7報價 瀏覽:337
i9300基帶文件 瀏覽:536
18款寶來加裝霧燈如何編程 瀏覽:894
女生送包了兩層紙的蘋果 瀏覽:186
a790e線刷教程 瀏覽:962
win10系統如何復制Pdf文件 瀏覽:800
js滑鼠移動圖顯示數據 瀏覽:203

友情鏈接