资源简介

基于Java语言,实现了TCP协议下的QQ聊天和文件传输,主要涉及两个原理:
1. Socket编程实现客户端和服务器之间传递消息
2. 文件传输
实现功能:
1.服务器和客户端一对一聊天
2.用户和用户一对一聊天
3.用户对外群发消息
4.服务器对外群发消息
5.服务器向用户一对一传输文件
6.可视化界面
操作流程参考这里:
https://blog.csdn.net/DebugMakesMeHappy/article/details/103064461

资源截图

代码片段和文件信息

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;


public class Client extends Jframe {

private String sendingMessage;


//获取当前用户姓名
private String ClientName;
private PrintWriter out;


private ArrayList MyFriendsButton = new ArrayList<>();
private ArrayList MyFriends = new ArrayList<>();

//socket
private Socket s = null;

//Socket的输入和输出
private BufferedReader in = null;

//当前客户端是否退出
private boolean KeepingOnline = true;

//是否按下发送按钮
private boolean IsPressedSendButton = false;
private JPanel FriendList;
//一对一聊天标志
private static boolean P2P = false;

public static void main(String[] args) {

Client client = new Client(“客户端“);
client.setVisible(true);
client.start();
}


//启动发送和接受任务
private void start() {
try {


// 建立客户端socket
s = new Socket(“127.0.0.1“ 5432);

// 获得socket输出out,自动刷新
out = new PrintWriter(s.getOutputStream() true);

// 获取输入
in = new BufferedReader(new InputStreamReader(s.getInputStream()));

//服务器分配用户名
ClientName = in.readLine();

//在界面显示
MessageRecords.append(“Hello “ + ClientName + “!\n“);

this.FriendList.updateUI();

//创建接收消息的线程
new Thread(new ClientThread(this)).start();


//用户保持在线,且按下了发送按钮
while (KeepingOnline) {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (IsPressedSendButton) {
//发送消息时附上自己的用户名
//客户端发送的消息包含 用户名+消息文本
out.println(ClientName + sendingMessage);
IsPressedSendButton = false;


}
}


} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


private class ClientThread implements Runnable {
Client client;

ClientThread(Client client) {
this.client = client;
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.out.println(ClientName+“下线“);
client.out.println(ClientName+“quit“);
KeepingOnline=false;

}
});
}

private void receive() {
//接收服务端消息
try {
//接收到的消息
//send(ClientName + “ “ + MessageInformation + “\r“ + receivedMessage);
//包含用户名、时间信息和消息内容
String receivedMessage = in.readLine();


if (receivedMessage != null) {
// 当接收到的信息中包含“IsOnline“,说明有新的用户上线
// 这时处理一下新的用户
// 在通知某用户上线时,后面会发来数条信息,这其中包括
// 1.当前用户个数
// 2.所有用户名称
if (received

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        301  2019-11-12 22:21  TCPTalkingRoom\.classpath

     文件        390  2019-11-14 11:24  TCPTalkingRoom\.project

     文件        598  2019-11-12 22:21  TCPTalkingRoom\.settings\org.eclipse.jdt.core.prefs

     文件         26  2019-11-14 10:11  TCPTalkingRoom\1.txt

     文件       1442  2019-11-14 11:24  TCPTalkingRoom\bin\Client$ClientThread$1.class

     文件       6005  2019-11-14 11:24  TCPTalkingRoom\bin\Client$ClientThread.class

     文件       7768  2019-11-14 11:24  TCPTalkingRoom\bin\Client.class

     文件       2623  2019-11-14 11:24  TCPTalkingRoom\bin\Server$SendMsg.class

     文件       9192  2019-11-14 11:24  TCPTalkingRoom\bin\Server$ServerThread.class

     文件       8139  2019-11-14 11:24  TCPTalkingRoom\bin\Server.class

     文件       1401  2019-11-14 11:24  TCPTalkingRoom\bin\TCPClient.class

     文件       1775  2019-11-14 11:24  TCPTalkingRoom\bin\TCPServer.class

     文件      10478  2019-11-14 11:12  TCPTalkingRoom\src\Client.java

     文件      16647  2019-11-14 11:12  TCPTalkingRoom\src\Server.java

     文件       1356  2019-11-14 00:01  TCPTalkingRoom\src\TCPClient.java

     文件       1516  2019-11-14 09:56  TCPTalkingRoom\src\TCPServer.java

     文件      28462  2019-11-13 23:47  TCPTalkingRoom\timg.jpg

     目录          0  2019-11-12 22:21  TCPTalkingRoom\.settings

     目录          0  2019-11-14 11:24  TCPTalkingRoom\bin

     目录          0  2019-11-13 23:45  TCPTalkingRoom\src

     目录          0  2019-11-14 11:23  TCPTalkingRoom

----------- ---------  ---------- -----  ----

                98119                    21


评论

共有 条评论