package com.sie.it.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
public class ClientChat extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField inputText;
ClientHandler client;
public JButton loginButton;
public JButton quitButton;
public JTextField portField;
public JTextField hostField;
public JTextArea area;
public JScrollBar scroll;
public ClientChat() {
super("socket client");
loginButton = new JButton(new ConnectionAction());
loginButton.setText("Connect");
quitButton = new JButton(new DisConnectionAction());
quitButton.setText("Disconnect");
inputText = new JTextField(30);
inputText.setAction(new BroadcastAction());
area = new JTextArea(10, 50);
area.setLineWrap(true);
area.setEditable(false);
scroll = new JScrollBar();
scroll.add(area);
hostField = new JTextField(10);
hostField.setText("localhost");
portField = new JTextField(10);
portField.setText("8090");
JPanel h = new JPanel();
h.setLayout(new BoxLayout(h, BoxLayout.LINE_AXIS));
h.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel nameLabel = new JLabel("host: ");
JLabel serverLabel = new JLabel("port: ");
h.add(nameLabel);
h.add(Box.createRigidArea(new Dimension(10, 0)));
h.add(hostField);
h.add(Box.createRigidArea(new Dimension(10, 0)));
h.add(Box.createHorizontalGlue());
h.add(Box.createRigidArea(new Dimension(10, 0)));
h.add(serverLabel);
h.add(Box.createRigidArea(new Dimension(10, 0)));
h.add(portField);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.setBorder(new EmptyBorder(10, 10, 10, 10));
JPanel left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
left.add(area);
left.add(Box.createRigidArea(new Dimension(0, 5)));
left.add(Box.createHorizontalGlue());
left.add(inputText);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
right.add(loginButton);
right.add(Box.createRigidArea(new Dimension(0, 5)));
right.add(quitButton);
right.add(Box.createHorizontalGlue());
right.add(Box.createRigidArea(new Dimension(0, 25)));
p.add(left);
p.add(Box.createRigidArea(new Dimension(10, 0)));
p.add(right);
getContentPane().add(h, BorderLayout.NORTH);
getContentPane().add(p);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* 建立连接
*
* @author Administrator
*
*/
public class ConnectionAction extends AbstractAction {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
try {
client = new ClientHandler(hostField.getText(), portField.getText(), ClientChat.this);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/**
* 断开连接
*
* @author Administrator
*
*/
public class DisConnectionAction extends AbstractAction {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
try {
client.bc.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/**
* 发送消息
*
* @author Administrator
*
*/
public class BroadcastAction extends AbstractAction {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
IBlockingConnection bc;
try {
bc = new BlockingConnection(client.bc);
bc.setEncoding("UTF-8");
bc.setAutoflush(true);
bc.write(inputText.getText() + "\n\r");
area.append("client say : + " + inputText.getText() + "\n\r");
} catch (Exception e2) {
// TODO: handle exception
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
ClientChat chat = new ClientChat();
chat.pack();
chat.setVisible(true);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
xsocket源码和socket源码,文档
共25个文件
class:14个
java:7个
classpath:1个
5星 · 超过95%的资源 需积分: 20 107 下载量 66 浏览量
2012-02-14
15:33:10
上传
评论 3
收藏 324KB RAR 举报
温馨提示
socket,xscoket技术详解,包括两个实例,服务端客户端相互收发信息,建立监听,建立连接等
资源推荐
资源详情
资源评论
收起资源包目录
demo.rar (25个子文件)
demo
bin
com
sie
it
client
ClientChat$DisConnectionAction.class 1KB
ClientHandler$XsocketClient.class 2KB
ClientChat$BroadcastAction.class 2KB
ClientHandler.class 1KB
ClientChat$ConnectionAction.class 1KB
ClientChat.class 3KB
server
ServerChat.class 3KB
ServerHandler$XsocketServer.class 2KB
ServerChat$LogoutAction.class 1KB
ServerChat$LoginAction.class 1KB
ServerHandler.class 2KB
socket
server.class 2KB
client.class 2KB
socketutil
XsocketUtil.class 263B
.settings
org.eclipse.jdt.core.prefs 629B
src
com
sie
it
client
ClientHandler.java 1KB
ClientChat.java 5KB
server
ServerChat.java 4KB
ServerHandler.java 2KB
socket
client.java 2KB
server.java 1KB
socketutil
XsocketUtil.java 275B
.project 380B
.classpath 361B
lib
xSocket-2.7.2.jar 327KB
共 25 条
- 1
lsh744
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页