⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 一个使用Java实现的类似与QQ的聊天程序。使用了Hibernate组建。可用于学习Java网络编程和Hiberante数据库组件的学习
💻 JAVA
字号:
package com.jim.client;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

import com.jim.database.Friendship;
import com.jim.database.JIMUser;
import com.jim.net.Connection;
import com.jim.net.JIMProtocol;
import com.jim.net.Message;
import com.jim.net.RequestObject;

public class Client {
	private static JIMUser user;
	private static Connection con;
	private static String server;
	private static int port=9001;
	private static Logger log = Logger.getLogger(Client.class);
	private static ReceiveThread rTh = null;
	private static boolean loggedIn = false;
	private boolean connected;
	private MainWin mainWin;
	private InetAddress localAdd;
	
	protected void login(){
		Scanner in = new Scanner(System.in).
					useDelimiter(System.getProperty("line.separator"));
		int id;		
		System.out.println("INPUT YOUR No.");
		id = Integer.parseInt(in.next());
		con.setEndPoint(id);
		RequestObject req = new RequestObject();
		req.setCommand(JIMProtocol.LONIN);
		req.addParameter("id", id);
		con.setBusy(true);
		con.sendRequest(req);
		con.setBusy(false);
		rTh = new ReceiveThread(con ,null);
		rTh.start();
	}
	protected String login(int id ,String pwd){
		//con.setEndPoint(id);
		RequestObject response;
		RequestObject req = new RequestObject();
		req.setCommand(JIMProtocol.LONIN);
		req.addParameter("id", new Integer(id));
		req.addParameter("pwd", pwd);
		con.setBusy(true);
		con.sendRequest(req);
		response = con.getRequest();
		con.setBusy(false);
		String status =(String) response.getParameter("status");
		if(status.equals("OK")){
			user = (JIMUser)response.getParameter("user");
			con.setEndPoint(user.getJimno());
			loggedIn = true;
			rTh = new ReceiveThread(con,mainWin);
			rTh.start();
		}
		return status;
	}
	
	protected void sendMessage(){
		Scanner in = new Scanner(System.in).
						useDelimiter(System.getProperty("line.separator"));
		int to = 0;
		Message msg = null;
		RequestObject req = new RequestObject(JIMProtocol.MSG2FRIEND);
		try {
			System.out.println("INPUT RECEIVER No.");
			to = Integer.parseInt(in.next());
		} catch (NumberFormatException e) {
			System.out.println("input format error");
			return;
		}
		System.out.println("INPUT MESSAGE CONTENT");
		String mg = in.next();
		msg = new Message(con.getEndPoint(),to);
		msg.addContent(mg);
		req.addParameter("message", msg);
		msg.setSendTime(new Date());
		con.setBusy(true);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		con.sendRequest(req);
		con.setBusy(false);
		log.debug("send out command msg2Friend");
	}
	
	
	protected void logout() {
		if(loggedIn){
			log.debug("send out command logout");
			RequestObject req = new RequestObject(JIMProtocol.LOGOUT);
			req.addParameter("id", con.getEndPoint());
			rTh.finish();
			//con.setBusy(true);
			con.sendRequest(req);
			//con.setBusy(false);
			//con.getCommand();
			con.close();
			connected = false;
		}else{
			log.debug("send out command disconnect");
			if(	rTh != null)
				rTh.finish();
			RequestObject req = new RequestObject(JIMProtocol.DISCONNECT);
			//con.setBusy(true);
			con.sendRequest(req);
			//con.setBusy(false);
			connected = false;
			con.close();
		}
	}
	protected String  updateInfo(JIMUser u) {
		log.debug("send out command updateInfo");
		RequestObject req = new RequestObject(JIMProtocol.UPDATEINFO);
		req.addParameter("user", u);
		con.setBusy(true);
		con.sendRequest(req);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String status = (String)con.receive();
		con.setBusy(false);
		log.debug("finish command updateInfo");
		return status;
	}
	protected RequestObject updateFriends() {
		log.debug("send out command updateFriends");
		RequestObject req = new RequestObject(JIMProtocol.UPDATEFRIENDS);
		RequestObject response = null;
		req.addParameter("id", user.getJimno());
		con.setBusy(true);
		con.sendRequest(req);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response = con.getRequest();
		con.setBusy(false);
		log.debug("finish command updateInfo");
		return response;
	}
	protected void deleteFriend(Integer uid) {
		log.debug("send out command deleteFreind");		
		RequestObject req = new RequestObject(JIMProtocol.DELFRIEND);
		req.addParameter("id", MainWin.client.getUser().getJimno());
		req.addParameter("uid", uid);
		con.setBusy(true);
		con.sendRequest(req);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		con.setBusy(false);
	}
	protected void addFriend(Integer id) {
		log.debug("send out command addFriend");
		RequestObject req = new RequestObject(JIMProtocol.ADDFRIEND);
		req.addParameter("uid", id);
		req.addParameter("id", user.getJimno());
		con.setBusy(true);
		con.sendRequest(req);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		con.setBusy(false);
	}
	protected HashMap<Integer,JIMUser> findUsers() {
		log.debug("send out command findUsers");
		RequestObject req = new RequestObject(JIMProtocol.FINDUSERS);
		RequestObject response = null;
		con.setBusy(true);
		con.sendRequest(req);
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response = con.getRequest();
		con.setBusy(false);
		HashMap<Integer,JIMUser> users = (HashMap<Integer,JIMUser>)response.getParameter("users");
		return users;
	}
	protected void getFriends() {
		log.debug("send out command getFriends");	
	}
	protected void register() {
		log.debug("send out command register");		
	}
	public static void main(String[] args){
		Client mySelf = new Client();
		boolean logout = false;
		int cmd = 0;
		Scanner in = new Scanner(System.in).
			useDelimiter(System.getProperty("line.separator"));
		while (true) {
			System.out.println("INPUT THE SERVER NAME OR ADDRESS");
			server = in.next();
			System.out.println("INPUT THE PORT NUMBER [DEFAULT:9001]");
			port =Integer.parseInt(in.next());
			try {
				Socket s = new Socket(server,port);
				log.debug("socket to server build up." + s);
				con = new Connection(s);
				log.debug("connection to server build up");
				break;
			} catch (UnknownHostException e) {
				log.debug("unknown host", e);
				continue;
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
		}
		
	while(!logout){
		printHelp();
		try {
			cmd = Integer.parseInt(in.next());
		} catch (NumberFormatException e) {
			System.out.println("input error");
			continue;
		}
		switch(cmd){
		case 1:
			mySelf.login();
			break;
		case 2:
			mySelf.register();
			break;
		case 3:
			mySelf.getFriends();
			break;
		case 4:
			mySelf.addFriend(null);
			break;
		case 5:
			mySelf.findUsers();
			break;
		case 6:
			mySelf.updateFriends();
			break;
		case 7:
			mySelf.updateInfo(null);
			break;
		case 8:
			mySelf.deleteFriend(null);
			break;
		case 9:
			mySelf.sendMessage();
			break;
		case 10:
			mySelf.logout();
			logout = true ;
			break;
		default:
			System.out.println("unrecgnized command");
			break;
		}
	}
	}
	private static void printHelp(){
		System.out.println();
		System.out.println("**********select function**********");
		System.out.println("1.Login");
		System.out.println("2.Register a new user");
		System.out.println("3.List my friends");
		System.out.println("4.Add a new friend");
		System.out.println("5.Find users in system");
		System.out.println("6.Update friends information");
		System.out.println("7.Update your personal information");
		System.out.println("8.Delete one friend");
		System.out.println("9.Send message to a friend");
		System.out.println("10.Logout");	
		System.out.println();
	}

	public static Connection getCon() {
		return con;
	}

	public static void setCon(Connection con) {
		Client.con = con;
	}

	public static int getPort() {
		return port;
	}

	public static void setPort(int port) {
		Client.port = port;
	}

	public static String getServer() {
		return server;
	}

	public static void setServer(String server) {
		Client.server = server;
	}

	public static JIMUser getUser() {
		return user;
	}

	public static void setUser(JIMUser user) {
		Client.user = user;
	}

	public static boolean isLoggedIn() {
		return loggedIn;
	}
	
	protected String connectServer(String server,int port){
		try {
			Socket s = new Socket(server,port);
			localAdd = s.getLocalAddress();
			log.debug("socket to server build up." + s);
			con = new Connection(s);
			log.debug("connection to server build up");
			this.server = server;
			this.port = port;
			connected = true;
			return "OK";
		} catch (UnknownHostException e) {
			log.debug("unknown host", e);
			return "UnknownHost";
		} catch (IOException e) {
			e.printStackTrace();
			return "IOException";
		}
	}
	public int register(JIMUser u) {
		RequestObject req = new RequestObject(JIMProtocol.REGIESTER);
		req.addParameter("user", u);
		con.sendRequest(req);
		log.debug("send out command register");
		con.setBusy(true);
		if(rTh != null ){
			while(rTh.isBlock())
				try {
					Thread.currentThread().sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}else{
			con.getCommand();
		}
		Integer num = (Integer)con.receive();
		con.setBusy(false);
		return num.intValue();
		
	}
	public boolean isConnected() {
		return connected;
	}
	public void sendMessage(Message msg) {		
		RequestObject req = new RequestObject(JIMProtocol.MSG2FRIEND);
		req.addParameter("message", msg);
		con.sendRequest(req);
		log.debug("send out command msg2Friend");
	}
	public void setMaiWin(MainWin mainWin) {
		this.mainWin = mainWin;
		rTh.setMainWin(mainWin);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -