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

📄 talkclient.java

📁 java写的简单的cs模式的im程序
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class TalkClient {

	private Socket client;
	private String name;
	private String server;
	private BufferedReader br;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;
	private Msg msg,send;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		TalkClient ta = new TalkClient();
		ta.exec();
	}
	
	public void exec(){
		br = new BufferedReader(new InputStreamReader(System.in),1);
		System.out.println("请输入服务器地址:");
		try {
			server = br.readLine();
			System.out.println("请输入昵称:");
			name = br.readLine();
			System.out.println("连接服务器中……");
			client = new Socket(server,4200);
			oos = new ObjectOutputStream(client.getOutputStream());
			ois = new ObjectInputStream(client.getInputStream());
			System.out.println("123");
			oos.writeObject( Msg.HANDSHAKEMSG(name));
			oos.flush();
			System.out.println("send...");
			msg = (Msg)ois.readObject();
			if(msg.gettype() == Msg.OK){
				System.out.println("以连接上。");
				Thread thread  = new Thread(){
					public void run(){
						try {
							getmsg();
						} catch (IOException e) {
							// TODO 自动生成 catch 块
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							// TODO 自动生成 catch 块
							e.printStackTrace();
						}
					}
				};
				thread.start();
				while(true){
					System.out.print('>');
					String cmd = br.readLine();
					if(cmd.length()!=0){
						if(cmd.equalsIgnoreCase("send")){
							System.out.println("To:");
							String to = br.readLine();
							System.out.println("消息内容:");
							String content = br.readLine();
							send = new Msg(name, to, content);
							oos.writeObject(send);
							oos.flush();
						}
						else if(cmd.equalsIgnoreCase("list")){
							send = Msg.LISTMSG(name);
							oos.writeObject(send);
							oos.flush();
						}
						else if(cmd.equalsIgnoreCase("exit")){
							send = Msg.EXITMSG(name);
							oos.writeObject(send);
							oos.flush();
							thread.stop();
							break;
						}
					}
				}
			}
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		finally{
			try {
				ois.close();
				oos.close();
				client.close();
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}

		}
		System.exit(0);
	}
	
	public void getmsg() throws IOException, ClassNotFoundException{
		while(true){
			msg = (Msg)ois.readObject();
			System.out.print("\n"+msg+"\n>");
		}
	}
	
}

⌨️ 快捷键说明

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