?? servercustomer.java
字號:
package Java.ICQ;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Vector;
public class ServerCustomer extends Thread
{
private Socket socket;//定義套接口
private BufferedReader in;//定義輸入流
private PrintWriter out;//定義輸出流
ConnectSQL con;
public ServerCustomer(Socket socket) //線程構(gòu)造函數(shù)
{
this.socket=socket;//取得傳遞參數(shù)
con= new ConnectSQL();
try
{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//創(chuàng)建輸入流
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);//創(chuàng)建輸出流
System.out.println("創(chuàng)建雙向通訊管道成功");
}
catch(IOException ie)
{
System.out.println("創(chuàng)建雙向通訊管道失敗");
}
start();//啟動線程
}
public void run()
{//線程監(jiān)聽函數(shù)
while(true)
{
try
{
transact();
}
catch(IOException e)
{
IOexception(e);
out.println("usernamepassword");
break;
}
}
}
public void transact() throws IOException
{
String str=in.readLine();//取得輸入字符串
System.out.println(str);
//如果是結(jié)束就關(guān)閉連接
if(str.equals("end"))
{
this.stop();
}
//如果是登錄
if(str.equals("login"))
{
login();
}
if(str.equals("new"))
{
getUserMess();
}
if(str.equals("find"))
{
find();
}
if(str.equals("friend"))
{
friend();
}
if(str.equals("addfriend"))
{
addfriend();
}
if(str.equals("addnewfriend"))
{
addnewfriend();
}
if(str.equals("delfriend"))
{
delfriend();
}
if(str.equals("logout"))
{
logout();
}
if(str.equals("getwhoaddme"))
{
getwhoaddme();
}
}
//登錄函數(shù)
public void login() throws IOException
{
int user=getUsername();
String password= getPassword();
System.out.println(user);
System.out.println(password);
//連接數(shù)據(jù)庫并查詢
con.connectDBSQL();
String strpassword= con.LoginSQL(user,password);
if(strpassword==null)
out.println("usernameError");
else
{
System.out.println(password.regionMatches(0,strpassword,0,strpassword.length()));
//判斷密碼是否相等
if(password.regionMatches(0,strpassword,0,strpassword.length()))
{
//如果相同就告訴客戶ok
//并且更新數(shù)據(jù)庫用戶為在線
//以及注冊用戶的ip 地址
String hostAddress = socket.getInetAddress().getHostAddress();
System.out.println(hostAddress);
con.connectDBSQL();
con.updateSQL(hostAddress,user);
out.println("succeed");
}
else
{
out.println("passwordError");
}
}
}
//注冊用戶函數(shù)
public void getUserMess() throws IOException
{
String nickname=in.readLine().trim();
String password=in.readLine().trim();
String email=in.readLine().trim();
String sex=in.readLine().trim();
String info=in.readLine().trim();
String place=in.readLine().trim();
int picindex=Integer.parseInt(in.readLine());
con.connectDBSQL();
System.out.println(nickname);
System.out.println(password);
System.out.println(email);
System.out.println(sex);
System.out.println(info);
System.out.println(place);
System.out.println(picindex);
String falg = con.insertSQL(nickname,password,email,sex,info,place,picindex);
System.out.println(falg);
out.println(falg);
}
//查找好友
public void find() throws IOException
{
con.connectDBSQL();
Vector rs= con.findFriend();
if(rs.size()>0)
{
ObjectOutputStream objStream = new ObjectOutputStream(socket.getOutputStream());
objStream.writeObject((Vector)rs);
}
}
//上線時讀取好友的資料
public void friend() throws IOException
{
int friend=getFriend();
con.connectDBSQL();
Vector rs=con.readFriend(friend);
System.out.println(rs.size());
if(rs.size()>0)
{
System.out.println("找到好友");
out.println("FindFriend");
ObjectOutputStream objStream = new ObjectOutputStream(socket.getOutputStream());
objStream.writeObject((Vector)rs);
}
else
{
System.out.println("沒有好友");
out.println("notFindFriend");
}
}
//以下處理用戶添加好友
public void addfriend() throws IOException
{
int friendicqno=getUsername();
int myicqno= getUsername();
con.connectDBSQL();
String rs= con.addfriend(myicqno,friendicqno);
out.println(rs);
}
//以下處理其他用戶如果加我,我就加他
public void addnewfriend() throws IOException
{
int friendicqno= getUsername();//好友號碼
int myicqno=getUsername();//自己號碼
con.connectDBSQL();
System.out.println(friendicqno);
Vector rsadd = con.addnewfriend(myicqno,friendicqno);//讀取好友的資料
con.connectDBSQL();
String rsString=con.addfriend(myicqno,friendicqno);
System.out.println(rsadd.size());
if(rsadd.size()>0&&rsString.equals("addSucceed"))
{
out.println("addSucceed");
ObjectOutputStream objStream = new ObjectOutputStream(socket.getOutputStream());
objStream.writeObject((Vector)rsadd);//把讀取好友的資料發(fā)給客戶
}
else if(rsadd.size()>0)
{
out.println("addLost");
}
else
{
out.println("addError");
}
}
//以下執(zhí)行用戶刪除好友
public void delfriend() throws IOException
{
int friendicqno=getUsername();
int myicqno=getUsername();
con.connectDBSQL();
String rsdel = con.delfriend(myicqno,friendicqno);
System.out.println(rsdel);
out.println(rsdel);
}
//以下處理用戶退出程序
public void logout() throws IOException
{
int myicqno=getUsername();
System.out.println(myicqno);
con.connectDBSQL();
String rsdate = con.logout(myicqno);
System.out.println(rsdate);
out.println(rsdate);
}
//以下處理那些人加了我為好友,以便上線通知他們
public void getwhoaddme() throws IOException
{
int myicqno=getUsername();
con.connectDBSQL();
con.getwhoaddme(myicqno);
}
//從客戶端獲取用戶名
public int getUsername() throws IOException
{
String icqno=in.readLine();
int user=Integer.parseInt(icqno);//取得輸入的jicq號碼
System.out.println(icqno);
return user;
}
//從客戶端獲取密碼
public String getPassword() throws IOException
{
String passwd=in.readLine().trim();//取得輸入的密碼
System.out.println(passwd);
return passwd;
}
public int getFriend() throws IOException
{
int icqno=Integer.parseInt(in.readLine());
System.out.println(icqno);
return icqno;
}
//處理IO異常函數(shù)
public void IOexception(IOException e)
{
System.out.println("讀取數(shù)據(jù)出錯"+e.getMessage());
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -