package cn.davy.mychat;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Group;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
public class MyChat {
private Text txtServerIP;
private Text txtPort;
private Text txtReceivedMessage;
private Text txtSendingMsg;
private int myPort;
private InetAddress myServerAddress;
private Socket clientSocket = null;
private Socket connectionSocket = null;
private ServerSocket welcomeSocket = null;
private boolean isClient = true;
private BufferedReader inFromClient = null;
private DataOutputStream outToClient = null;
private BufferedReader inFromServer = null;
private DataOutputStream outToServer = null;
private Button btnConnecting;
private Button btnClose;
private Button btnListening;
private Button btnPing;
private Button btnFontSetting;
private Button btnInputColor;
private Button btnSendingColor;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
MyChat window = new MyChat();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(943, 620);
shell.setText("SWT Application");
shell.setLayout(new FormLayout());
Group group = new Group(shell, SWT.NONE);
group.setText("\u4FA6\u542C\u529F\u80FD");
group.setLayout(new FormLayout());
FormData fd_group = new FormData();
fd_group.top = new FormAttachment(0, 10);
fd_group.left = new FormAttachment(0, 10);
fd_group.bottom = new FormAttachment(0, 145);
fd_group.right = new FormAttachment(100, -10);
group.setLayoutData(fd_group);
Label lblNewLabel = new Label(group, SWT.NONE);
FormData fd_lblNewLabel = new FormData();
fd_lblNewLabel.top = new FormAttachment(0, 10);
fd_lblNewLabel.left = new FormAttachment(0, 10);
lblNewLabel.setLayoutData(fd_lblNewLabel);
lblNewLabel.setText("\u5BF9\u65B9IP\u5730\u5740");
Label lblPort = new Label(group, SWT.NONE);
FormData fd_lblPort = new FormData();
fd_lblPort.top = new FormAttachment(lblNewLabel, 21);
fd_lblPort.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
lblPort.setLayoutData(fd_lblPort);
lblPort.setText("\u7AEF\u53E3");
txtServerIP = new Text(group, SWT.BORDER);
txtServerIP.setText("127.0.0.1");
FormData fd_txtServerIP = new FormData();
fd_txtServerIP.bottom = new FormAttachment(lblNewLabel, 0, SWT.BOTTOM);
fd_txtServerIP.left = new FormAttachment(lblNewLabel, 26);
txtServerIP.setLayoutData(fd_txtServerIP);
txtPort = new Text(group, SWT.BORDER);
txtPort.setText("2016");
fd_txtServerIP.right = new FormAttachment(txtPort, 0, SWT.RIGHT);
FormData fd_txtPort = new FormData();
fd_txtPort.right = new FormAttachment(100, -139);
fd_txtPort.left = new FormAttachment(lblPort, 61);
fd_txtPort.top = new FormAttachment(lblPort, 0, SWT.TOP);
txtPort.setLayoutData(fd_txtPort);
Group group_1 = new Group(shell, SWT.NONE);
group_1.setText("\u6536\u53D1\u6570\u636E");
group_1.setLayout(new FormLayout());
FormData fd_group_1 = new FormData();
fd_group_1.top = new FormAttachment(group, 33);
fd_group_1.bottom = new FormAttachment(100, -10);
fd_group_1.left = new FormAttachment(0, 10);
fd_group_1.right = new FormAttachment(100, -10);
btnListening = new Button(group, SWT.NONE);
FormData fd_btnListening = new FormData();
fd_btnListening.right = new FormAttachment(txtServerIP, 117, SWT.RIGHT);
fd_btnListening.top = new FormAttachment(lblNewLabel, -5, SWT.TOP);
fd_btnListening.left = new FormAttachment(txtServerIP, 44);
btnListening.setLayoutData(fd_btnListening);
// 功能一:(服务器端功能)侦听+循环接收
/*
* (1) 创建线程,处理服务器端的侦听与循环接收功能; (2) 读取port,创建服务器端socket (3) 启动侦听 (4)
* 循环接收数据,并显示在控件中
*/
btnListening.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
btnConnecting.setEnabled(false); // 如果启动侦听功能,则不再使用连接功能
isClient = false; // 服务器端功能
myPort = Integer.parseInt(txtPort.getText().trim());
// 启动程序后,自动进入侦听与循环接收阶段
new Thread() {
public void run() {
try {
welcomeSocket = new ServerSocket(myPort);
connectionSocket = welcomeSocket.accept();
// 用于接收数据
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
// 用于发送数据
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
// 循环接收数据
while (true) {
try {
final String messagea = inFromClient.readLine();
display.asyncExec(new Runnable() {
@Override
public void run() {
txtReceivedMessage.append("对方: " + messagea + "\n");
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}.start();
}
});
btnListening.setText("\u4FA6\u542C");
btnConnecting = new Button(group, SWT.NONE);
// 功能二:(客户端功能)连接+循环接收
/*
* (1)
*/
btnConnecting.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 连接服务器,并进入循环接收状态
btnListening.setEnabled(false);
isClient = true; // 客户端功能
try {
myPort = Integer.parseInt(txtPort.getText().trim());
String strServerIP = txtServerIP.getText().trim();
String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
if (strServerIP.matches(regex)) {
myServerAddress = InetAddress.getByName(strServerIP);
clientSocket = new Socket(myServerAddress, myPort); // 创建socket用于连接
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
} catch (Exception e1) {
e1.printStackTrace();
}
new Thread() {
public void run() {
try {
while (true) {
final String message1 = inFromServer.readLine();
display.asyncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txtReceivedMessage.append("对方: " + message1 + "\n");
}
});
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}.start();
}
});
FormData fd_btnConnecting = new FormData();
fd_btnConnecting.top = new FormAttachment(lblPort, -5, SWT.TOP);
fd_btnConnecting.right = new FormAttachment(btnListening, -3, SWT.RIGHT);
fd_btnConnecting.left = new FormAttachment(btnListening, 0, SWT.LEFT);
btnConnecting.setLayoutData(fd_btnConnecting);
btnConnecting.setText("\u8FDE\u63A5");
btnClose = new Button(group, SWT.NONE);
// 功能八:断开连接
btnClose.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
btnListening.setEnabled(true);
btnConnecting.setEnabled(true);
try {
if (clientSocket != null)
clientSocket.close();
if (welcomeSocket != null)
welcomeSocket.close();
if (connectionSocket != null)
connectionSocket.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
FormData fd_btnClose = new FormData();
fd_btnClose.right = new FormAttachment(btnListening, 0, SWT.RIGHT);
fd_btnClose.bottom = new FormAttachment(100, -9);
fd_btnClose.left = new FormAttachment(btnListening, 3, SWT.LEFT);
btnClose.setLayoutData(fd_btnClose);
btnClose.setText("\u65AD\u5F00\u8FDE\u63A5");
group_1.setLayoutData(fd_group_1);
txtReceivedMessage = new Text(group_1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
FormData fd_txtReceivedMessage = new FormData();
fd_txtReceivedMessage.bottom = new FormAttachment(100, -183);
fd_txtReceivedMessage.top = new FormAttachment(0, 27);
fd_txtReceivedMessage.left = new FormAttachment(0, 13);
fd_txtReceivedMessage.right = new FormAttachment(100, -16);
txtReceivedMessage.setLayoutData(fd_txtReceivedMessage);
txtSendingMsg = new Text(group_1, SWT.BORDER);
FormData fd_txtSendingMsg = new FormData();
fd_txtSendingMsg.top = new FormAttachment(txtReceivedMessage, 23);
fd_txtSendingMsg.right = new FormAttachment(txtReceivedMessage, -3, SWT.RIGHT);
fd_txtSendingMsg.left = new FormAttachment(txtReceivedMessage, 0, SWT.LEFT);
fd_txtSendingMsg.bottom = new FormAttachment(100, -79);
txtSendingMsg.setLayoutData(fd_txtSendingMsg);
Button btnSend = new Button(group_1, SWT.NONE);
// 功能三: 发送数据
btnSend.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
String sendingMessage = txtSendingMsg.getText() + "\n";
if (isClient) {
outToServer.writeBytes(sendingMessage);
txtReceivedMessage.append("我: " + sendingMessage);
} else {
outToClient.writeBytes(sendingMessage);
txtReceivedMessage.append("我: " + sendingMessage);
}
txtSendingMsg.setText("");
} catch (Exception e2) {
// TODO: handle exception
}
}
});
FormData fd_btnSend = new FormData();
fd_btnSend.top = new FormAttachment(txtSendingMsg, 24);
fd_btnSend.right = new FormAttachment(txtReceivedMessage, 0, SWT.RIGHT);
btnSend.setLayoutData(fd_btnSend);
btnSend.setText("\u53D1\u9001");
btnPing = new Button(group_1, SWT.NONE);
// 功能四:ping
btnPing.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String serverIP = txtServerIP.getText().trim();
try {
Runtime ce = Runtime.getRuntime();
InputStream in = (InputStream) ce.exec("ping " + serverIP).getInputStream();
BufferedInputStream bin = new BufferedInputStream(in);
byte pingInfo[] = new byte[100];
int n;
while ((n = bin.read(pingInfo, 0, 100)) != -1) {
String s = null;
s = new String(pingInfo, 0, n);
txtReceivedMessage.append(s);
}
txtReceivedMessage.append("Over!\n\n");
} catch (Exception ee) {
System.out.println(ee);
}
}
});
FormData fd_btnPing = new FormData();
fd_btnPing.bottom = new FormAttachment(btnSend, 0, SWT.BOTTOM);
fd_btnPing.left = new FormAttachment(0, 95);
btnPing.setLayoutData(fd_btnPing);
btnPing.setText("Ping");
btnFontSetting = new Button(group_1, SWT.NONE);
// 功能五:字体设置
btnFontSetting.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FontDialog fd = new FontDialog(shell, SWT.NONE);
fd.setText("字体设置");
FontData d = fd.open();
if (d != null) {
txtReceivedMessage.setFont(new Font(display, d));
txtSendingMsg.setFont(new Font(display, d));
}
}
});
FormData fd_btnFontSetting = new FormData();
fd_btnFontSetting.top = new FormAttachment(btnSend, 0, SWT.TOP);
fd_btnFontSetting.left = new FormAttachment(btnPing, 6);
btnFontSetting.setLayoutData(fd_btnFontSetting);
btnFontSetting.setText("\u5B57\u4F53\u8BBE\u7F6E");
btnInputColor = new Button(group_1, SWT.NONE);
// 功能六:接收颜色设置
btnInputColor.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ColorDialog cd = new ColorDialog(shell, SWT.NONE);
cd.setText("接收颜色设置");
RGB rgb = cd.open();
if (rgb != null) {
txtReceivedMessage.setBackground(new Color(display, rgb.red, rgb.green, rgb.blue));
}
}
});
FormData fd_btnInputColor = new FormData();
fd_btnInputColor.bottom = new FormAttachment(btnSend, 0, SWT.BOTTOM);
fd_btnInputColor.left = new FormAttachment(btnFontSetting, 6);
btnInputColor.setLayoutData(fd_btnInputColor);
btnInputColor.setText("\u63A5\u6536\u989C\u8272");
btnSendingColor = new Button(group_1, SWT.NONE);
// 功能七:发送颜色设置
btnSendingColor.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ColorDialog cd = new ColorDialog(shell, SWT.NONE);
cd.setText("发送颜色设置");
RGB rgb = cd.open();
if (rgb != null) {
txtSendingMsg.setBackground(new Color(display, rgb.red, rgb.green, rgb.blue));
}
}
});
btnSendingColor.setText("\u53D1\u9001\u989C\u8272");
FormData fd_btnSendingColor = new FormData();
fd_btnSendingColor.top = new FormAttachment(btnSend, 0, SWT.TOP);
fd_btnSendingColor.left = new FormAttachment(btnInputColor, 6);
btnSendingColor.setLayoutData(fd_btnSendingColor);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}