package com.client;
import com.util.JDBCUtility;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.util.Properties;
import javax.swing.JOptionPane;
public class ChatManager {
private ChatManager() {
}
private static final ChatManager instance = new ChatManager();
public static ChatManager getCM() {
return instance;
}
private MainWIndow window;
public static Socket socket;
private InputStream input;
private OutputStream out;
private BufferedReader reader;
private PrintWriter writer;
public static boolean isConnected = false;
connect t;
public void setWindow(MainWIndow window) {
this.window = window;
}
class connect extends Thread {
public void run() {
try {
String realPath = JDBCUtility.class.getClassLoader().getResource("")
.getFile();
realPath = URLDecoder.decode(realPath, "utf-8");
Properties p = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(
realPath + "/lib/Connection.properties"));
p.load(in);
String ip = p.getProperty("ip");
int prot = Integer.parseInt(p.getProperty("prot"));
socket = new Socket(ip, prot);
showMessage();
} catch (UnknownHostException e) {
System.out.println(4);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "无法连接服务器");
}
}
}
//发送消息
public void sendMessage(String message) {
if (message == null || message.trim().length() == 0) {
return;
}
try {
out = socket.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(out), true);
if (writer != null) {
writer.println(message);
} else {
window.appendText("与服务器断开连接");
}
} catch (IOException e) {
System.out.println(3);
}
}
// 断开与服务端的连接
@SuppressWarnings("deprecation")
public synchronized boolean closeConnection() {
t = new connect();
try {
sendMessage("CLOSE");// 发送断开连接命令给服务器
t.stop();// 停止接受消息线程
// 释放资源
if (reader != null) {
reader.close();
}
if(input!=null){
out.close();
}
if (writer != null) {
writer.close();
}
if(out!=null){
out.close();
}
if (socket != null) {
socket.close();
}
isConnected = false;
return true;
} catch (IOException e1) {
isConnected = true;
return false;
}
}
// 连接服务端
public boolean connectServer() {
try {
t = new connect();
t.start();
isConnected = true;// 已经连接上了
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "连接服务器失败!");
isConnected = false;// 未连接上
return false;
}
}
// 接收消息
public void showMessage() {
try {
input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input, "utf-8"));
String line = "";
while ((line = reader.readLine()) != null) {
if (line != null) {
window.appendText(line + "\r\n");
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "与服务器断开连接");
}
}
}
最近下载更多
fuyouou LV5
2023年6月29日
fantesy LV17
2022年12月5日
Kevin_001 LV6
2022年7月3日
微信网友_6003487859068928 LV5
2022年6月15日
微信网友_5989987974549504 LV5
2022年6月6日
978806986 LV16
2022年6月5日
testuser1234567 LV24
2022年5月31日
yuxinnan LV4
2022年2月5日
谢小饭_ LV8
2021年12月27日
huangzy LV12
2021年11月10日
最近浏览更多
微信网友_7556333962743808 LV1
6月19日
ma406805131 LV19
2024年6月29日
krispeng LV15
2024年4月15日
3334004690 LV11
2024年3月20日
Xiaobaiya11 LV2
2024年3月4日
uni-code_0123 LV1
2024年1月31日
fuyouou LV5
2023年6月29日
CL200228 LV4
2023年5月4日
臧家旺 LV3
2023年3月23日
zb200221
2022年12月5日
暂无贡献等级

