Android 开发即时聊天工具 YQ :(三) 实现登陆功能

本文详细介绍了如何在Android开发中实现即时聊天工具YQ的登录功能。通过封装用户信息到user对象并发送到服务器,客户端与服务器进行通信。登录成功后,会启动新的线程保持与服务器的连接,处理SocketTimeoutException异常,确保连接稳定性。源码已上传供学习交流。
摘要由CSDN通过智能技术生成

前面socket基本通信完了,登陆界面也已经完成,下面就是重点了,实现登陆功能

服务器和客户端的代码当然不肯能用那个控制台的那个了,所以全部得重写,不过原理都一样,代码也差不多,都有注释,一看就明白。

先是登陆的Activity:

public class LoginActivity extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_login);
	    Button btnLogin=(Button) findViewById(R.id.btn_login);
	    btnLogin.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				int account=Integer.parseInt(((EditText) findViewById(R.id.et_account)).getText().toString());
			    String password=((EditText) findViewById(R.id.et_password)).getText().toString();
			    login(account, password);
			}
	    }); 
	}
	
	void login(int a, String p){
		User user=new User();
		user.setAccount(a);
		user.setPassword(p);
		user.setOperation("login");
		boolean b=new YQConServer().sendLoginInfo(user);
		//登陆成功
		if(b){
			try {
				//发送一个要求返回在线好友的请求的Message
				//---
			} catch (IOException e) {
				e.printStackTrace();
			}
			//转到主界面,
			Intent i=new Intent(this, MainActivity.class);
			startActivity(i);
		}else {
			Toast.makeText(this, "登陆失败!不告诉你为什么,", Toast.LENGTH_SHORT).show();
		}
	}
}

将登陆的信息封装到user中,user的operation用来判断该user包的类型,交由YQClient来发送到服务器。

客户端:

public class YQClient{
	public Socket s;
	
	public boolean sendLoginInfo(Object obj){
		boolean b=false;
		try {
			s=new Socket();
			try{
				s.connect(new InetSocketAddress("10.0.2.2",5469),2000);
			}catch(SocketTimeoutException e){
				//连接服务器超时
				return false;
			}
			ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
			oos.writeObject(obj);
			ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
			YQMessage ms=(YQMessage)ois.readObject();
			if(ms.getType().equals(YQMessageType.SUCCESS)){
				//创建一个该账号和服务器保持连接的线程
				//---
				b=true;
			}else if(ms.getType().equals(YQMessageType.FAIL)){
				b=false;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return b;
	}
}

在登陆成功后,将会新开一个线程和服务器保持连接,该线程将用来通信,

要捕获SocketTimeoutException异常,否则连接 不到服务器,app会无响应,这里设置2s服务器无响应,则连接服务器超时。


最后在看服务器端:

public class YQServer {
	public YQServer(){
		ServerSocket ss = null;
		try {
			ss=new ServerSocket(5469);
			System.out.println("服务器已启动,正在监听5469端口...");
			while(true){
				Socket s=ss.accept();
				//接受客户端发来的信息
				ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
				User u=(User) ois.readObject();
				YQMessage m=new YQMessage();
				ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
				
		        if(u.getOperation().equals("login")){ //登录
		        	int account=u.getAccount();
		        	boolean b=new UserDao().login(account, u.getPassword());//连接数据库验证用户
					if(b){
						System.out.println("["+account+"] 上线了!");
						m.setType(YQMessageType.SUCCESS);//返回一个成功登陆的信息包
						oos.writeObject(m);
						//单开一个线程,让该线程与该客户端保持连接
						//---
					}else{
						m.setType(YQMessageType.FAIL);
						oos.writeObject(m);
					}
		        }else if(u.getOperation().equals("register")){
		        	//注册
		        }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

}

最后测试一下:




源码已经上传至我的资源,谢谢大家支持!欢迎一起学习交流!

转载请注明出处:http://blog.csdn.net/mimitracely
评论 88
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值