socket实例2
第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能
MainWindow.java
package com.jikexueyuan.myjavachatclient.view; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder; import com.jikexueyuan.myjavachatclient.main.ChatManager; public class MainWIndow extends JFrame { /**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextArea txt;
private JTextField ip;
private JTextField send; /**
* Create the frame.
*/
public MainWIndow() {
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(, , , );
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(, , , ));
setContentPane(contentPane); txt = new JTextArea();
txt.setText("Ready..."); ip = new JTextField();
ip.setText("127.0.0.1");
ip.setColumns(); JButton button = new JButton("连接到服务器");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ChatManager.getCM().connect(ip.getText());
}
}); send = new JTextField();
send.setText("你好");
send.setColumns(); JButton button_1 = new JButton("发送");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ChatManager.getCM().send(send.getText());
appendText("我说:"+send.getText());
send.setText("");
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(txt, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(ip, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(send, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, , GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(ip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(txt, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(send, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button_1)))
);
contentPane.setLayout(gl_contentPane);
} public void appendText(String in) {
txt.append("\n"+in);
}
}
StartClient.java
package com.jikexueyuan.myjavachatclient.main; import java.awt.EventQueue; import com.jikexueyuan.myjavachatclient.view.MainWIndow; public class StartClient { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWIndow frame = new MainWIndow();
frame.setVisible(true);
ChatManager.getCM().setWindow(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} }
ChatManager.java
package com.jikexueyuan.myjavachatclient.main; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException; import com.jikexueyuan.myjavachatclient.view.MainWIndow; public class ChatManager { private ChatManager(){}
private static final ChatManager instance = new ChatManager();
public static ChatManager getCM() {
return instance;
} MainWIndow window;
String IP;
Socket socket;
BufferedReader reader;
PrintWriter writer; public void setWindow(MainWIndow window) {
this.window = window;
window.appendText("文本框已经和ChatManager绑定了。");
} public void connect(String ip) {
this.IP = ip;
new Thread(){ @Override
public void run() {
try {
socket = new Socket(IP, );
writer = new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream())); reader = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
window.appendText("收到:"+line);
}
writer.close();
reader.close();
writer = null;
reader = null;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} public void send(String out) {
if (writer != null) {
writer.write(out+"\n");
writer.flush();
}else {
window.appendText("当前的链接已经中断");
}
}
}
将工程运行起来,这里我启动2个客户端
实现了即时通信
socket实例2的更多相关文章
- 网络编程基础【day09】:简单socket实例(二)
本节内容 1.概述 2.socket实例 3.总结 一.概述 之前我们只是介绍了soket的概念和一些逻辑图表,下面我们来看看,socket的客户端和服务端到底是怎么用的? 二.socket实例 2. ...
- java tcp socket实例
java tcp socket实例 2011-04-20 13:58 2364人阅读 评论(1) 收藏 举报 socketjavatcpthreadserverclass package com.ne ...
- python socket实例练习
Web Server是基于Socket编程,又称之为网络编程,socket是网络编程接口,socket可以建立网络连接,读数据,写数据.socket模块定义了一些常量参数,用来指定socket的的地址 ...
- C # socket 实例
同步客户端存储示例 下面的示例程序创建连接到服务器的客户端. 客户端使用一个同步套接字生成,因此,客户端应用程序的执行挂起,直到服务器返回响应. 应用程序将字符串发送到服务器 ...
- py测试一个Socket实例
本实例旨在了解py和socket的一些相关知识. 1.服务器端搭建py监听程序. 在客户端搭建python,linux默认自带了python2.7,先不管安装了. 接着编写socket程序,可以在本地 ...
- python socket实例
1.客户端向服务端发送 #coding:utf-8 '''客户端''' import socket khd=socket.socket() #声明socket类型,同时生产socket连接对象 khd ...
- 简单的php socket 实例
server: <?php set_time_limit(0); $ip = '127.0.0.1'; $port = 8888; // 1. 创建 if( ($sock = socket_cr ...
- socket实例1
第一个例子创建了一个java工程,用来测试Socket的连接功能,通过浏览器可访问,地址为:127.0.0.1:端口号 MyServerSocket.java, package com.jikexue ...
- java Socket实例
可以实现客户端与服务端双向通信,支持多客户端连接,客户端断开连接,服务端不会出现异常 服务端代码: package com.thinkgem.jeesite.modules.socketTest.de ...
随机推荐
- java socker编程
转自http://haohaoxuexi.iteye.com/blog/1979837 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户 ...
- nginx 1.安装
nginx 1.安装 nginx的众多优点这里就不多说了,直接开始吧. 基本依赖 yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...
- python的行与缩进
#coding=utf-8#逻辑行与物理行#以下是3个物理行print "abc"print "789"print "777" #以下是1个 ...
- ASP.NET-FineUI开发实践-9(二)
其实我也不会,老实教人学怕误人子弟,但是抱着毁人不倦的精神还是糊弄糊弄个别小白吧,最起码能加点原创. 下面以表单为例,打开官方项目,版本为FineUI_4.1.1,打开form_compare页,右键 ...
- 解决 iOS webkit 使用CSS动画时闪烁的问题
-webkit-backface-visibility: hidden;
- 富文本编辑器CKEDITOR的使用配置(问题注解)
CKEDITOR是一款非常轻便的富文本编辑器,如上图:参考网上的使用方法,我以.net为例,在aspx页面使用, 准备工作:首先要下载控件包,将解压后的整个文件夹添加到项目根目录. 第一步:引用js, ...
- HTML中常用鼠标样式
语法:cursor : auto | all-scroll | col-resize| crosshair | default | hand | move | help | no-drop | not ...
- tips [终端]
pbcopy 命令:Place standard output in the clipboard. $ pbcopy < ~/.ssh/id_rsa.pub
- UITableView实现分组, 并且点击每个分组后展开
效果图: 简单说下实现思路: 数据传过来之后, 先创建好对应个数的分组头部View, 也就是要在 - (UIView *)tableView:(UITableView *)tableView view ...
- [转]javascript函数定义表达式和函数声明的区别
在javascript中,函数有两种定义写法,函数定义表达式和函数声明,其例子分别如下所示: var test = function(x){ return x; } function test(x){ ...