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 拥有完整 ...
随机推荐
- node.js 抓取
http://blog.csdn.net/youyudehexie/article/details/11910465 http://www.tuicool.com/articles/z2YbAr ht ...
- android 自定义gallerey并实现预览功能
自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果.的确HorizontalScrollView可以实现Gal ...
- OpenCV stereo matching 代码 matlab实现视差显示
转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/44151213, 来自:shiter编写程序的艺术 基础知识 计算机视觉是一门研究 ...
- Unity Socket TCP
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Net.Sock ...
- objective-c随机数+日期格式显示一例
在原来的代码上有修改,主要为: 将准备随机数方法放到了init中,这样不用手动调用了 setWeek方法已经过时,使用的是setWeekOfYear方法 在此放一份以备以后查找: le.h // // ...
- python 内置标准库socketserver模块的思考
socketserver模块简化了编写网络服务器的任务, 在很大程度上封装了一些操作, 你可以看成是事件驱动型的设计, 这很不错.它定义了两个最基本的类--服务器类 BaseServer, 请求处理类 ...
- Hibernate与Mybatis的比较
Hibernate与Mybatis的比较: Hibernate: 标准的.重量级.全自动化的ORM框架 可以写sql(SQLQuery,sql )也可以不写sql(Query,hql) ORM映射主要 ...
- 鹅厂优文|打通小程序音视频和webRTC
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:腾讯视频云终端技术总监常青, 2008 年毕业加入腾讯,一直从事客户端研发相关工作,先后参与过 PC QQ.手机QQ.QQ物联 等产品 ...
- 超精简易用cocoaPods的安装和使用
cocoaPods 安装和使用 第一步:替换ruby源 $ gem sources -l 查看当前ruby的源 $ gem sources ...
- MvcSiteMapProvider 自定义模板
MvcSiteMapProvider 介绍文字就省了,直接访问官方站点吧. 官方站点:https://github.com/maartenba/MvcSiteMapProvider 默认的模板文件 ...