package com.info.window;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client extends JFrame {
private JPanel p1, p2, p3, p5, p6, p7;
private JLabel lb1, lb2, lb3, lb4, lb5;
private Container container;
private JTextField t1, t2, t3;
private static JTextArea ta;
private JButton bt1, bt2;
public Client(){
this.setTitle("客户机");
container = this.getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
lb1 = new JLabel("通信面板");
lb2 = new JLabel("服务器主机名:");
lb3 = new JLabel("服务器端口:");
lb4 = new JLabel("待发送消息:");
lb5 = new JLabel("服务器Echo消息面板");
bt1 = new JButton("连接服务器");
bt2 = new JButton(" 发 言 ");
bt1.addActionListener(new bt1ActionListener());
bt2.addActionListener(new bt2ActionListener());
t1 = new JTextField(10);
t2 = new JTextField(5);
t3 = new JTextField(25);
ta = new JTextArea(15, 40);
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
p7 = new JPanel();
p1.add(lb1);
container.add(p1);
p2.add(lb2);
p2.add(t1);
p2.add(lb3);
p2.add(t2);
p2.add(bt1);
container.add(p2);
p3.add(lb4);
p3.add(t3);
p3.add(bt2);
container.add(p3);
p5.add(lb5);
container.add(p5);
p6.add(ta);
p7.add(p6);
container.add(p7);
this.setSize(500, 500);
this.setVisible(true);
this.setResizable(false);
}
private Socket clientSocket = null;
private BufferedReader in;
private PrintWriter out;
public class bt1ActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
try {
bt1.setEnabled(false);
String remoteName = t1.getText();
int remotePort = Integer.parseInt(t2.getText());
SocketAddress remoteAddr = new InetSocketAddress(
InetAddress.getByName(remoteName),remotePort);
clientSocket = new Socket();
clientSocket.connect(remoteAddr);
ta.append("连接服务器成功,会话开始…\n");
out = new PrintWriter(new OutputStreamWriter(
clientSocket.getOutputStream(),"UTF-8"),true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream(),"UTF-8"));
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e.getMessage(),
"连接错误",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
public class bt2ActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if(clientSocket == null){
JOptionPane.showMessageDialog(null, "请先检查服务器连接情况。\n确保客户及连接服务器!",
"错误提示", JOptionPane.ERROR_MESSAGE);
return;
}
String outStr = t3.getText();
if(outStr.length() == 0){
JOptionPane.showMessageDialog(null, "请输入发送消息!",
"提示", JOptionPane.ERROR_MESSAGE);
return;
}
out.println(outStr);
t3.setText("");
try {
String inStr;
inStr = in.readLine();
ta.append("Echo:" + inStr + "\n");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "客户机接收消息错误!",
"错误提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
private void formWindowClosing(java.awt.event.WindowEvent evt){
try {
if(in != null)in.close();
if(out != null)out.close();
if(clientSocket != null)clientSocket.close();
} catch (Exception e) {}
}
public static void main(String[] args) {
new Client();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
服务器-客户端.rar (14个子文件)
服务器-客户端
bin
com
info
window
ClientThread.class 3KB
Client$bt2ActionListener.class 2KB
Server$btActionListener$1.class 2KB
Client$bt1ActionListener.class 3KB
Server$btActionListener.class 2KB
Server.class 4KB
Server$taActionListener.class 667B
Client.class 4KB
.settings
org.eclipse.jdt.core.prefs 598B
src
com
info
window
Server.java 4KB
ClientThread.java 2KB
Client.java 4KB
.project 395B
.classpath 301B
共 14 条
- 1
资源评论
qq_37006420
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功