Socket 聊天工具
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 聊天工具的更多相关文章
- 用Socket做一个局域网聊天工具(转)
原文:http://www.cnblogs.com/technology/archive/2010/08/15/1799858.html 程序设计成为简单的服务端和客户端之间的通信, 但通过一些方法可 ...
- Socket聊天程序——服务端
写在前面: 昨天在博客记录自己抽空写的一个Socket聊天程序的初始设计,那是这个程序的整体设计,为了完整性,今天把服务端的设计细化记录一下,首页贴出Socket聊天程序的服务端大体设计图,如下图: ...
- python 开发简单的聊天工具
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...
- Java之简单的聊天工具
今天整理资料的时候,找出自己几年前刚学Java时做过的一个简易的聊天工具,有服务器也有客户端,能发送文字消息和文件,但是用户上线并未存入数据库,而只是简单的缓存在服务器的一个数组中,所以,只要服务器一 ...
- 基于Nodejs开发的web即时聊天工具
由于公司需要开发web即时聊天的功能,开始时我们主要的实施方法是用jquery的ajax定时(10秒)轮询向服务器请求,由于是轮询请求,对 服务器的压力比较大.我们网站上线的时间不长,访问量不是很大, ...
- 聊天工具mychat
python学习,自己写了个简单聊天工具mychat 最近在学习python,自己写了个最最简单的聊天工具mychatv0.1. 第一版,完成基本的聊天功能. GUI用的是自带的TKinter,用到的 ...
- Python3 实现简易局域网视频聊天工具
Python3 实现简易局域网视频聊天工具 1.环境 操作系统为 Ubuntu 16.04 python 3.5opencv-python 3.4.1.15numpy 1.14.5PyAudio ...
- python 开发简单的聊天工具-乾颐堂
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...
- 使用PHP+Swoole实现的网页即时聊天工具:PHPWebIM
使用PHP+Swoole实现的网页即时聊天工具 全异步非阻塞Server,可以同时支持数百万TCP连接在线 同时支持websocket+comet2种兼容协议,可用于所有种类的浏览器包括IE 拥有完整 ...
随机推荐
- 知名IT公司的年度大会合集
很多知名的IT公司都有年度大会,比如说谷歌,微软,Adobe,甲骨文,苹果等等.在这些公司的年度大会上,都会展示一些公司比较前沿的产品.看看这些大会的视频(也可以参会,但是门票可是非常贵的),对我们了 ...
- Oracle Applications DBA 基础(二)
6.OAM及系统管理 2014年9月13日 20:40 参考资料: 1.Oracle Applications System Administrator's Guide - Configuration ...
- Oracle EBS订单的流程(Order->AR)
from:http://blog.csdn.net/pan_tian/article/details/7693447 基本流程 创建订单 路径:Order Management > Orders ...
- "《算法导论》之‘线性表’":基于指针实现的单链表
对于单链表的介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1. 单链表介绍 单向链表(单链表)是链表的一种,它由节点组成,每个节点都包含下一个节点的指针. ...
- 如何让minicom换行
以前在使用minicom验证裸板代码的时候,经常会遇到以下这个问题: 通常一直打数据往后打就只能打一行,然后就不能换行了,遇到这个问题如何解决? 看到屏幕下方提示:CTRL-A Z for hel ...
- HTML中的javascript交互
在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的javasc ...
- mysql-proxy中的admin-lua-script
[root@ecs-7b55 lua]# cat admin.lua --[[ $%BEGINLICENSE%$ Copyright (c) 2008, 2012, Oracle and/or its ...
- iOS中tableView组头部或尾部标题的设置
解决在tableView返回组标题直接返回字符串,带来的不便设置组标题样式的问题解决办法,设置尾部标题和此类似 // 返回组头部view的高度 - (CGFloat)tableView:(UITab ...
- 万水千山ABP - 系统发布后迁移 CodeFirst 数据库[原创]
在项目开发的过程中,常会遇到项目发布后还变更数据库的情况.这时如何方便地进行数据库迁移呢 ? 下面直接列出操作的步骤: 1. 发布修改后的应用: 将最新版本的应用更新到目标机器中.更新的文件当然不包括 ...
- (转) windows下 安装 rabbitMQ 及操作常用命令
该博客转载自:https://blog.csdn.net/gy__my/article/details/78295943 原作者:Eric Li 出处:http://www.cnblogs.com/ ...