qqjava源碼
Ⅰ 求一個類似QQ或者其他的聊天程序源代碼,java的
簡單得很的那種要不要?就像用來應對考試一樣。
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("埠使用中....");
System.out.println("請關掉相關程序並重新運行伺服器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客戶端,開兩個就能聊了……
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Ⅱ 用java可以做qq登錄界面,那個源代碼怎麼寫
源碼:http://wenku..com/view/4b03494ccf84b9d528ea7a26.html
Ⅲ QQ聊天界面的java代碼
請問你的郵箱地址是多少?我有源代碼,可以發給你!
Ⅳ 我在伺服器搭建了一個JAVA伺服器,如何才可以訪問伺服器端,我的QQ源碼如何用
服務端在伺服器上跑起來。資料庫該 架的架起來。
保證埠對外開放,就行了。
Ⅳ 仿qq聊天的java完整代碼
幫你做一個
Ⅵ 誰能給我一個用java寫的山寨QQ的源代碼
這個東西做起來並沒有什麼難度,關鍵點是通信那一塊。
建議消息用伺服器轉發的方式。每個登陸用戶與伺服器採用短連接通信。
其他沒什麼重要的了,你自己努力一下,2天大概就能做的很像樣了。如果做的過程中有問題,可以問我。
如果你想做到T訊那樣,就不現實了,倒不是說功能多,只因為他的通信方式要復雜很多。比如說跨區域網的UDP通信,那是很復雜的。你還要理解網路,理解路由器,才可以哦。
Ⅶ 求JAVA webQQ在線聊天源碼
客戶端源代碼
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class socketClient
{
private DataOutputStream output;
private DataInputStream input;
private String clientName;
public static void main(String[] args)
{
//在main函數中,啟動伺服器的socket
new socketClient().ConnectServer();
}
public void ConnectServer()
{
try
{
Socket socket = new Socket("127.0.0.1",10001);
clientName = socket.getInetAddress().toString();
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
new readServer().start();
new writeServer().start();
}
catch(Exception e) {System.out.println(e.toString());}
}
public class readServer extends Thread
{
private Socket client;
public void run()
{
String msg;
try
{
while(true)
{
msg = input.readUTF();
if(msg!=null)
System.out.println("收到消息:【"+clientName+"】 "+msg);
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}
public class writeServer extends Thread
{
private Socket client;
public void run()
{
try
{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while(true)
{
if(stdIn.ready())
{
userInput = stdIn.readLine();
if(userInput!="exit")
{
output.writeUTF(userInput);
System.out.println("已發送消息給【"+clientName+"】"+userInput);
}
}
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}
}
伺服器源代碼
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class socketServer
{
ArrayList clientList = new ArrayList();
//private DataOutputStream output;
//private DataInputStream input;
//private String clientName;
//private Socket socket;
public static void main(String[] args)
{
//在main函數中,啟動伺服器的socket
new socketServer().OpenServer();
}
public void OpenServer()
{
try
{
ServerSocket server = new ServerSocket(10001);
Socket socket;
while((socket = server.accept())!=null)
{
clientList.add(socket);
//clientName = socket.getInetAddress().toString();
//output = new DataOutputStream(socket.getOutputStream());
//input = new DataInputStream(socket.getInputStream());
new readClient(socket).start();
new writeClient(socket).start();
}
}
catch(Exception e) {System.out.println(e.toString());}
}
public class readClient extends Thread
{
private Socket socket;
public readClient(Socket client)
{socket = client;}
public void run()
{
String msg;
try
{
String clientName = socket.getInetAddress().toString();
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream());
while((msg = input.readUTF())!=null)
{
System.out.println("收到消息:【"+clientName+"】 "+msg);
}
}
catch(Exception e){System.out.println(e.toString());}
}
}
public class writeClient extends Thread
{
private Socket socket;
public writeClient(Socket client)
{socket = client;}
public void run()
{
try{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
String clientName = socket.getInetAddress().toString();
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream());
while(true)
{
if(stdIn.ready())
{
userInput = stdIn.readLine();
if(userInput!="exit")
{
output.writeUTF(userInput);
System.out.println("已發送消息給【"+clientName+"】"+userInput);
}
}
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}
}
Ⅷ 求一個用java編的qq聊天工具要完整代碼!能運行出來的!帶資料庫的
一般都是有償給人做的,你這樣誰給你啊
Ⅸ 關於仿QQ聊天對話框的JAVA代碼
JAVA做QQ的源碼的到是有的。。。不過有8000多行。。。基本功能全齊了
如果LZ連Swing都不會用的話,感覺還是找點簡單的項目先練習練習,這QQ要牽扯到網路鏈接。。。我也在研究中呵呵
Swing很簡單,拖拖拽拽就可以了,設置顯示對話框的屬性為只讀,然後用FileInputStream來讀取
下面的輸入框用FileOutputStream來輸入點擊發送按鈕後重新導入上面的對話框。。。這是最簡單的QQ囧。。。。
Ⅹ 你好,java實現模擬qq登錄,然後發送消息的代碼(根據攔截數據包中信息)
我知道用java去做一個多人聊天室,首先是要有伺服器端,客戶端是用GUI做的界面每個用戶都是一個線程,當多人進行聊天的時候,都是先把數據傳給伺服器,然後伺服器在給每個人都發到自己的界面上,大概的思路就是這樣的
你要我這個代碼么 ,我可以發給你