感谢大家对IT十八掌大数据的支持,今天的作业如下:
参照视频,编写出聊天程序。
知识点分析:
Socket + GUI + IO = 简版QQ
------------------------------
1.GUI
2.Socket
ServerSocket : port + accept
Socket sock = accept();
//更新窗口
//接收client的数据
new Thread(){
run(){
sock..getInputStream();
....
//更新窗口
}
}.start()
3.在分线程中完成ServerSocket推送消息给所有客户端。
List<OutputStream> ... = ;
4.推送联系人列表给所有客户端。
-------------------------------------------------------------------------------------------
项目源码:
package com.it18zhang;
public class App {
public static void main(String[] args) {
//创建窗口
MyWindow w = new MyWindow();
//消息服务器
MessageServer server = new MessageServer(w);
//启动消息服务器
server.start(1234);
}
}
package com.it18zhang;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 消息接收线程
*/
public class MessageReceiverThread extends Thread {
private MyWindow window ;
private Socket sock ;
public MessageReceiverThread(MyWindow w , Socket s){
this.window = w ;
this.sock = s ;
}
public void run() {
while(true){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream(), "gbk"));
String line = null;
while((line = br.readLine()) != null){
window.updateHistory(SocketUtil.getAddr(sock),line);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.it18zhang;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class MessageServer {
//客户端集合
private List<String> clients = new ArrayList<String>();
private MyWindow window ;
public MessageServer(MyWindow window) {
this.window = window;
}
//启动
public void start(int port){
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("服务器启动了!!");
InetAddress remAddr ;
int remPort ;
while(true){
final Socket sock = ss.accept();
new MessageReceiverThread(window, sock).start();
remAddr = sock.getInetAddress();
remPort = sock.getPort() ;
//192.168.12.2:8080
clients.add(SocketUtil.getAddr(sock));
//刷新列表
window.refreshContacts(clients);
}
}
catch (Exception e) {
}
}
}
package com.it18zhang;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* 窗体
*/
public class MyWindow extends JFrame {
private static final long serialVersionUID = -6944831795769317874L;
// 历史记录
private JTextArea taHistory;
// 内容
private JTextArea taContent;
// 发送按钮
private JButton btnSend;
// 联系人列表
private JList listContacts;
private JTable table;
public MyWindow() {
ini();
}
// 初始化
private void ini() {
//
this.setSize(600, 400);
this.setLocation(200, 200);
// 绝对布局
this.setLayout(null);
//联系人表格
table = new JTable();
table.setBounds(500, 0, 100, 400);
this.add(table);
//聊天记录区
taHistory = new JTextArea();
taHistory.setEditable(false);
taHistory.setBounds(0, 0, 480, 300);
this.add(taHistory);
this.setVisible(true);
// 事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
}
/**
* 刷新联系人
*/
public void refreshContacts(final List<String> clients) {
//
Set<String> set = new HashSet<String>(clients);
final List<String> newclients = new ArrayList<String>(set);
//
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 1;
}
public int getRowCount() {
return newclients.size();
}
public Object getValueAt(int row, int col) {
return newclients.get(row);
}
};
table.setModel(dataModel);
}
/**
* 更新聊天记录
*/
public void updateHistory(String addr, String line) {
//原文
String old = taHistory.getText();
StringBuilder builder = new StringBuilder();
builder.append(old);
builder.append("\r\n");
builder.append(addr);
builder.append("\r\n");
builder.append(line);
builder.append("\r\n");
//更新聊天区域
taHistory.setText(builder.toString());
//server.pushMessagae(builder.toString());
}
}
package com.it18zhang;
import java.net.Socket;
/**
* 工具类
*/
public class SocketUtil {
/**
* 返回地址串
*/
public static String getAddr(Socket sock){
String ip = sock.getInetAddress().getHostName() ;
String ip0 = ip.substring(ip.lastIndexOf(".") + 1);
return ip0 + ":" + sock.getPort() ;
}
}