java+socket+对接其他_java swing+socket+mysql实现多个客户端局域网聊天

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, "与服务器断开连接");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值