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();
}
}
}
}

Socket 聊天工具的更多相关文章

  1. 用Socket做一个局域网聊天工具(转)

    原文:http://www.cnblogs.com/technology/archive/2010/08/15/1799858.html 程序设计成为简单的服务端和客户端之间的通信, 但通过一些方法可 ...

  2. Socket聊天程序——服务端

    写在前面: 昨天在博客记录自己抽空写的一个Socket聊天程序的初始设计,那是这个程序的整体设计,为了完整性,今天把服务端的设计细化记录一下,首页贴出Socket聊天程序的服务端大体设计图,如下图: ...

  3. python 开发简单的聊天工具

    python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...

  4. Java之简单的聊天工具

    今天整理资料的时候,找出自己几年前刚学Java时做过的一个简易的聊天工具,有服务器也有客户端,能发送文字消息和文件,但是用户上线并未存入数据库,而只是简单的缓存在服务器的一个数组中,所以,只要服务器一 ...

  5. 基于Nodejs开发的web即时聊天工具

    由于公司需要开发web即时聊天的功能,开始时我们主要的实施方法是用jquery的ajax定时(10秒)轮询向服务器请求,由于是轮询请求,对 服务器的压力比较大.我们网站上线的时间不长,访问量不是很大, ...

  6. 聊天工具mychat

    python学习,自己写了个简单聊天工具mychat 最近在学习python,自己写了个最最简单的聊天工具mychatv0.1. 第一版,完成基本的聊天功能. GUI用的是自带的TKinter,用到的 ...

  7. Python3 实现简易局域网视频聊天工具

    Python3 实现简易局域网视频聊天工具   1.环境 操作系统为 Ubuntu 16.04 python 3.5opencv-python 3.4.1.15numpy 1.14.5PyAudio ...

  8. python 开发简单的聊天工具-乾颐堂

    python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...

  9. 使用PHP+Swoole实现的网页即时聊天工具:PHPWebIM

    使用PHP+Swoole实现的网页即时聊天工具 全异步非阻塞Server,可以同时支持数百万TCP连接在线 同时支持websocket+comet2种兼容协议,可用于所有种类的浏览器包括IE 拥有完整 ...

随机推荐

  1. platform_driver_probe与platform_driver_register的区别

    Platform Device and Drivers  从<linux/platform_device.h>我们可以了解Platform bus上面的驱动模型接口:platform_de ...

  2. cocos2D v3.x 中action的回调block变化

    cocos2D v2.x中有带参数的回调block: id blk = [CCCallBlockN actionWithBlock:^(CCNode *node){ node.position = o ...

  3. 如何修改SpriteBuilder中的按钮禁用启用状态

    按钮的禁用状态和按钮的User Interaction Enabled属性并不相符. 后者设定的是按钮是否参与用户交互. 你不能直接在SpriteBuilder中编译按钮的禁用启用属性.除非你想给按钮 ...

  4. XMPP客户端库Smack

    原文博客地址:http://blog.csdn.net/chszs/article/details/41576877

  5. 【ROM修改教程】添加高级电源重启菜单(安卓4.0.4官方ROM)

    准备工作: 电脑上安装好JDK.下载smali和baksmali.下载apktools.要修改的ROM.adb工具(可选) 注:由于本教程面向的对象为有一定ROM修改基础的兄弟,所以对于如何使用电脑, ...

  6. Android特效专辑(二)——ViewPager渲染背景颜色渐变(引导页)

    Android特效专辑(二)--ViewPager渲染背景颜色渐变(引导页) 首页:http://blog.csdn.net/qq_26787115/article/details/50439020 ...

  7. git rebase之前需要commit才行

    更新好本地代码后,git fetch, 接着合并,但是git rebase 不行, git status一看,有很多更新的文件. 于是 git add --后,再rebase,还是不行. 注意,reb ...

  8. Prefix tree

    Prefix tree The trie, or prefix tree, is a data structure for storing strings or other sequences in ...

  9. MongoDB下载安装测试及使用

    1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...

  10. gcc如何生成预编译头文件(.gch)

    1 建立comm.h 2 main.c中包含comm.h : #include "comm.h" 3 gcc -o comm.h.gch comm.h(低版本gcc会有bug) 4 ...