登录 之 通讯机制(socket)续

按钮响应之“登录”

package com.net.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

//import com.net.convert.MakeCertPic;
import com.net.convert.Security;
import com.net.ui.LandWindow;

public class Submit implements ActionListener{
	private LandWindow landUI=null;
	
	private String name;
	private String pswd;
	
	private String IP=null;
	private int SERVERPORT=-1;
	private String SPLITSTR=null;
	
	private OutputStream os=null;
	private InputStream is=null;
	private final String respondStyle="land";
	private final String ISACK = "ACK";
	private final String ISNAK = "NAK!";
	
	public Submit(LandWindow landUI) {
		this.landUI=landUI;
		this.landUI.refreshCertPicLabel();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String name = landUI.getUserNameField().getText();
		String pswd = new String(landUI.getPasswdField().getPassword());
		String certStr = landUI.getCertTextField().getText();
		
		Security security = new Security(pswd);
		security.securityAlgorithm("MD5");
		
		boolean passFlag=true;
		
		landUI.getUserNameLabel().setText(null);
		landUI.getPasswdLabel().setText(null);
		landUI.getStatLabel().setText(null);
		
		if (name.length() == 0){
			landUI.getUserNameLabel().setText("用户名不能为空");
			passFlag=false;
		}
		if (pswd.length() == 0){
			landUI.getPasswdLabel().setText("密码不能为空");
			passFlag=false;
		}
		if (certStr.length() == 0){
			landUI.getStatLabel().setText("验证码不能为空");
			passFlag=false;
		}
		
		if(passFlag){
			if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
				this.name=name;
				this.pswd=security.getSecurityResult();
				landUI.getStatLabel().setText("正在登录...");
				start();
			} else {
				landUI.getStatLabel().setText("验证码错误");
			}
		} else {
			landUI.refreshCertPicLabel();
		}
	}
	
	public void setIP(String ip){
		this.IP=ip;
	}
	
	public void setSplitStr(String splitStr){
		this.SPLITSTR=splitStr;
	}
	
	public void setServerPort(int port){
		this.SERVERPORT=port;
	}
	
	public void start() { // 建立联网线程
		new Thread(new Runnable() {
			public void run() {
				Socket s = null;
				try {
					s = new Socket(IP, SERVERPORT);
					os = s.getOutputStream();
					os.write((name + SPLITSTR + pswd + SPLITSTR + respondStyle).getBytes());

					os.flush();
					// 读内容
					Thread.sleep(1000);
					is = s.getInputStream();
					int len = is.available();
					byte[] bytes = new byte[len];
					is.read(bytes);
					String result = new String(bytes);

					// TODO 这里通过返回结果处理
					int index=result.indexOf(SPLITSTR);
					String resultStatus=result.substring(0, index);
					String resultInfo=result.substring(index+SPLITSTR.length());
					if (ISACK.equals(resultStatus)){
						landUI.getStatLabel().setText("Welcome:" + resultInfo);
					} else if(ISNAK.equals(resultStatus)){ //验证成功后输出对应用户名
						landUI.getPasswdField().setText(null);
						landUI.getStatLabel().setText("ERROR:" + resultInfo);
					} else {
						landUI.getStatLabel().setText("ERROR:can not get respond from server");
					}
				} catch(ConnectException e){
					landUI.getStatLabel().setText("ERROR:connect server failed");
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					try {
						if (os != null) os.close();
						if (is != null) is.close();
						if (s != null)	s.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}


响应动作之“注册”

package com.net.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.net.convert.Security;
import com.net.ui.LandWindow;

public class Register implements ActionListener{
	private LandWindow landUI=null;
	
	private String name;
	private String pswd;
	
	private String IP=null;
	private int SERVERPORT=-1;
	private String SPLITSTR=null;
	
	private OutputStream os=null;
	private InputStream is=null;
	private final String respondStyle="register";
	private final String ISACK = "ACK";
	private final String ISNAK = "NAK!";
	
	public Register(LandWindow landUI) {
		this.landUI=landUI;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String name = landUI.getUserNameField().getText();
		String pswd = new String(landUI.getPasswdField().getPassword());
		String certStr = landUI.getCertTextField().getText();
		
		Security security = new Security(pswd);
		security.securityAlgorithm("MD5");
		boolean passFlag=true;
		
		landUI.getUserNameLabel().setText(null);
		landUI.getPasswdLabel().setText(null);
		landUI.getStatLabel().setText(null);
		
		if (name.length() == 0){
			landUI.getUserNameLabel().setText("用户名不能为空");
			passFlag=false;
		}
		if (pswd.length() == 0){
			landUI.getPasswdLabel().setText("密码不能为空");
			passFlag=false;
		}
		if (certStr.length() == 0){
			landUI.getStatLabel().setText("验证码不能为空");
			passFlag=false;
		}
		
		if(passFlag){
			if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
				this.name=name;
				this.pswd=security.getSecurityResult();
				landUI.getStatLabel().setText("提交信息中...");
				start();
			} else {
				landUI.getStatLabel().setText("验证码错误");
			}
		} else {
			landUI.refreshCertPicLabel();
		}
	}
	
	public void setIP(String ip){
		this.IP=ip;
	}
	
	public void setSplitStr(String splitStr){
		this.SPLITSTR=splitStr;
	}
	
	public void setServerPort(int port){
		this.SERVERPORT=port;
	}
	
	public void start() { // 建立联网线程
		new Thread(new Runnable() {
			public void run() {
				Socket s = null;
				try {
					s = new Socket(IP, SERVERPORT);
					os = s.getOutputStream();
					os.write((name + SPLITSTR + pswd + SPLITSTR + respondStyle).getBytes());

					os.flush();
					// 读内容
					Thread.sleep(1000);
					is = s.getInputStream();
					int len = is.available();
					byte[] bytes = new byte[len];
					is.read(bytes);
					String result = new String(bytes);

					// TODO 这里通过返回结果处理
					int index=result.indexOf(SPLITSTR);
					String resultStatus=result.substring(0, index);
					String resultInfo=result.substring(index+SPLITSTR.length());
					if (ISACK.equals(resultStatus)){
						landUI.getPasswdField().setText(null);
						landUI.getStatLabel().setText("INFO:" + resultInfo + " regist successfully");
					} else if(ISNAK.equals(resultStatus)){ //验证成功后输出对应用户名
						landUI.getPasswdField().setText(null);
						landUI.getStatLabel().setText("ERROR:" + resultInfo);
					} else {
						landUI.getStatLabel().setText("ERROR:can not get respond from server");
					}
				} catch(ConnectException e){
					landUI.getStatLabel().setText("无法连接服务端");
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					try {
						if (os != null) os.close();
						if (is != null) is.close();
						if (s != null)	s.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}


响应动作之“重置”

package com.net.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.net.ui.LandWindow;

public class Cancle implements ActionListener {
	private LandWindow landUI=null;

	public Cancle(LandWindow landUI) {
		this.landUI=landUI;
		//this.landUI.refreshCertPicLabel();
	}
	
	private void clearPanel() {
		this.landUI.getUserNameField().setText(null);
		this.landUI.getUserNameLabel().setText(null);
		this.landUI.getPasswdField().setText(null);
		this.landUI.getPasswdLabel().setText(null);
		this.landUI.getCertTextField().setText(null);
		this.landUI.getStatLabel().setText(null);
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		this.clearPanel();
		this.landUI.refreshCertPicLabel();
	}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值