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 拥有完整 ...
随机推荐
- iOS中关于UIApplication的详细介绍
UIApplication 什么是UIApplication? UIApplication对象是应用程序的象征.每一个应用都有自己的UIApplication对象,这个对象是系统自动帮我们创建的, 它 ...
- AngularJS进阶(十)AngularJS改变元素显示状态
AngularJS改变元素显示状态 前言 本文描述使用AngularJS提供的ng-show和ng-hide指令实现自动监听某布尔型变量来改变元素显示状态. 控制html元素显示和隐藏有n种方法:ht ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- EBS form 之间跳转实现(form 关闭)
实现 form CUXOMWB 使用 app_navigate.execute 打开 form CUXOEXPRAVA :然后 FROM CUXOEXPRAVA 上点击按钮 跳回from CUXOMW ...
- Globalization Guide for Oracle Applications Release 12
Section 1: Overview Section 2: Installing Section 3: Configuring Section 4: Maintaining Section 5: U ...
- Leetcode_235_Lowest Common Ancestor of a Binary Search Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392713 Given a binary search ...
- web报表工具FineReport常用函数的用法总结(数学和三角函数)
最后一次抛砖引玉了,至此finereport所有的常见函数就分享完了,期待能对大家有些许帮助. ABS ABS(number):返回指定数字的绝对值.绝对值是指没有正负符号的数值. Number:需要 ...
- zookeeper 应用开发
由于zookeeper的client只有zookeeper一个对象,使用也比较简单,所以就不许要文字说明了,在代码中注释下就ok 了. 1.测试用的main方法 package ClientExamp ...
- IT咨询顾问:一次吐血的项目救火
年后的一个合作公司上线了一个子业务系统,对接公司内部的单点系统.我收到该公司的技术咨询:项目启动后没有规律的突然无法登录了,重新启动后,登录一断时间后又无法重新登录,对方技术人员一头雾水不知道什么原因 ...
- nifi1.6.0汉化
1.1 测试机 l Apache NiFi 1.6.0 l HDP 2.6.3 l 集群规模:单节点 l 操作系统:CentOs7 l 以下所有操作均在root用户下执行 1.2 安装环境 ...