今天为大家分享简单的飞鸽传书程序的开发与制作,目前系统已经完成了初步功能,后续会进一步完善。整个系统界面漂亮,有完整源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步
开发环境
开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。
系统框架
利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。
程序主要功能
运行方法
1 运行AppStart.java文件
2 程序出现飞鸽传书的面板
系统主要功能点
1发送文字信息,收到方会弹出提醒
2 当发送文件的时候,收到放会弹出提醒,显示文件的名字。点击名字的时候,可以下载相应的文件
实现效果
主要代码
package com.ipmsg;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPService {
public static final int PORT = 30000;
private static final int BUFF_SIZE = 8192;
private static Object lock = new Object();
private ServerSocket serverSocket;
private static boolean isInit = false;
private TCPService() throws IOException {
serverSocket = new ServerSocket(PORT);
SendFileServer server = new SendFileServer();
server.start();
}
public static void init() {
synchronized (lock) {
if (!isInit) {
try {
new TCPService();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class SendFileServer extends Thread {
@Override
public void run() {
try {
while (true) {
Socket socket = serverSocket.accept();
SendFile st = new SendFile(socket);
st.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class SendFile extends Thread {
Socket socket;
SendFile(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
DataInputStream in = null;
DataInputStream fin = null;
DataOutputStream out = null;
try {
int len = 0;
byte[] buff = new byte[BUFF_SIZE];
in = new DataInputStream(socket.getInputStream());
len = in.read(buff);
String filePath = new String(buff, 0, len, "UTF-8");
File file = new File(filePath);
fin = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
out = new DataOutputStream(socket.getOutputStream());
out.writeLong(file.length());
out.flush();
buff = new byte[BUFF_SIZE];
while ((len = fin.read(buff)) > 0) {
out.write(buff, 0, len);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(in != null){
in.close();
in = null;
}
if(fin != null){
fin.close();
fin = null;
}
if(out != null){
out.close();
out = null;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}