前面socket基本通信完了,登陸界面也已經(jīng)完成,下面就是重點了,實現(xiàn)登陸功能
服務器和客戶端的代碼當然不肯能用那個控制臺的那個了,所以全部得重寫,不過原理都一樣,代碼也差不多,都有注釋,一看就明白。
先是登陸的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 {
-
- "WHITE-SPACE: pre">
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- Intent i=new Intent(this, MainActivity.class);
- startActivity(i);
- }else {
- Toast.makeText(this, "登陸失敗!不告訴你為什么,", Toast.LENGTH_SHORT).show();
- }
- }
- }
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 {
//發(fā)送一個要求返回在線好友的請求的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來發(fā)送到服務器。
客戶端:
- 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;
- }
- }
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)){
//創(chuàng)建一個該賬號和服務器保持連接的線程
//---
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("服務器已啟動,正在監(jiān)聽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();
- }
- }
-
- }
public class YQServer {
public YQServer(){
ServerSocket ss = null;
try {
ss=new ServerSocket(5469);
System.out.println("服務器已啟動,正在監(jiān)聽5469端口...");
while(true){
Socket s=ss.accept();
//接受客戶端發(fā)來的信息
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());//連接數(shù)據(jù)庫驗證用戶
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();
}
}
}
最后測試一下:
該文章在 2013/2/25 14:19:38 編輯過