package com.hr.chat.impl;
import com.hr.Lib.FileVector;
import com.hr.Menu.*;
import com.hr.Programe;
import com.hr.chat.interf.IChatClient;
import com.hr.Entity.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;
public class JavaChatClient implements IChatClient, Runnable {
private static Socket s;
private static InetSocketAddress isa;
private UserEntity AllUser = new UserEntity();
private UserEntity user;
public FileVector GetFriendsFromServer(Socket s) {
return null;
}
@Override
public void closeClient() {
// TODO Auto-generated method stub
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void listenServer() {
// TODO Auto-generated method stub
Thread tr = new Thread(this);
tr.setName("listenServer");
tr.setDaemon(true);
tr.start();
}
@Override
public void recordChatContent() {
// TODO Auto-generated method stub
}
public void SendDataToOthers() {
}
public void EarnAccount() {
OutputStream os = null;
InputStream is = null;
try {
os = s.getOutputStream();
is = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EarnAccountMenu eam = new EarnAccountMenu();
String op = eam.EarnAccount();
String subStr = "";
if (op.equals("Submit")) {
subStr = EarnAccountMenu.submitStr;
try {
os.write(subStr.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = new byte[1024];
String receiveData = null;
try {
int rLen = is.read(b);
receiveData = new String(b, 0, rLen);
System.out.println("客户端已经收掉账号" + receiveData);
if (receiveData.indexOf("ServerAcc") != -1) {
String[] strs = receiveData.split("spliter");
System.out.println("您的账号是:" + strs[0] + ",请记好!");
} else if (receiveData.equals("error")) {
System.out.println("申请失败");
}
// String tempInfo =receiveData;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("-----^-^------------");
}
}
String chatInfo = null;
public void SendDataToServer() {
OutputStream os = null;
try {
os = s.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scan = new Scanner(System.in);
String str = "";
System.out.println("本地IP:" + s.getLocalAddress().getHostAddress());
System.out.println("本地端口:" + s.getLocalPort());
// 用户选择发送的好友
int flag = 1;
while (true) {
System.out.println(flag);
if (flag == 1) {
System.out.println("++++");
chatInfo = ChatMenu.DisMenu(user);
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("--------------------");
chatInfo = ChatMenu.DisMenu(AllUser, "serch");
chatInfo=chatInfo+"spliter"+"wantFri";
System.out.println(chatInfo);
System.out.println("这些是所有的用户信息----------------------------------------+");
flag=1;
try {
os.write(chatInfo.getBytes());
System.out.println("已经传完要添加好友的账号");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("sdfsdfsdf");
break;
}
// 如果选择离开
if (chatInfo.equals("Exit")) {
System.out.println("你将退出系统");
try {
os.write(chatInfo.getBytes());
// os.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
} else if (chatInfo.equals("Search")) {
flag = 0;
System.out.println("您将向服务器发送查询请求");
try {
os.write(chatInfo.getBytes());
// os.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
chatInfo = "";
// break;
} else {
flag = 1;
str = scan.next();
try {
os.write(chatInfo.getBytes());
os.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public boolean startClient() {
// TODO Auto-generated method stub
s = new Socket();
isa = new InetSocketAddress(2020);
try {
s.connect(isa);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
private void getFriInfo(String receiveData, UserEntity user) {
// System.out.println(receiveData);
user.getFriendsinfo().clear();
String[] serverinfo = receiveData.split("spliter");
// System.out.println(serverinfo.length);
// 封装用户信息
UserEntity uFriends;
for (int i = 0; i < (serverinfo.length - 2) / 4; i++) {
uFriends = new UserEntity();
uFriends.setAccount(serverinfo[0 + i * 4]);
uFriends.setNickname(serverinfo[1 + i * 4]);
uFriends.setName(serverinfo[2 + i * 4]);
uFriends.setLogin(serverinfo[3 + i * 4].equals("true") ? true
: false);
user.getFriendsinfo().add(uFriends);
// System.out.println(+serverinfo[1+i*3]+serverinfo[2+i*3]);
}
user.setNickname(serverinfo[serverinfo.length - 1].toString());
}
public boolean Login() {
user = LoginMenu.Login();
OutputStream os = null;
InputStream is;
try {
os = s.getOutputStream();
is = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
try {
// 登陆时向服务器发送:ip,端口号,账号,密码
os.write(s.getLocalAddress().getHostAddress().getBytes());
os.write((s.getLocalPort() + "").getBytes());
System.out.println("向服务器传账号");
os.write(user.getAccount().getBytes());
os.write(user.getPassword().getBytes());
System.out.println("向服务器传密码");
byte[] b = new byte[1024];
// 接收服务器返回的是否登陆成功的信息
int rLen = is.read(b);
String receiveData = new String(b, 0, rLen);
// 登陆成功
if (receiveData.equals("LoginOK")) {
b = new byte[1024];
rLen = is.read(b);
receiveData = new String(b, 0, rLen);
// 把用户的好友登陆信息更新
getFriInfo(receiveData, user);
// System.out.println(user.getNickname());
System.out.println("欢迎" + user.getNickname() + "登陆");
return true;
} else if (receiveData.equals("ReLogin")) {
System.out.println("账号已经登陆!");
return false;
} else
// 登陆失败
{
System.out.println("您输入的账号或密码错误,是否重新登陆[y/n]");
return false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
// try {
// os.close();
//
// is.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
// 监听服务器发送的信息
private UserEntity MyClone(UserEntity user) {
UserEntity ue = new UserEntity();
ue.setAccount(user.getAccount());
ue.setId(user.getId());
ue.setName(user.getName());
ue.setNickname(user.getNickname());
return ue;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
// socket=ss.accept();
InputStream is = null;
try {
is = s.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
byte[] b = new byte[1024 * 5]
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java聊天系统控制台程序 (650个子文件)
JavaChatServer.class 9KB
JavaChatServer.class 9KB
JavaChatServer.class 9KB
ThreadReadDataFromClient.class 9KB
JavaChatServer.class 9KB
JavaChatClient.class 8KB
JavaChatServer.class 8KB
JavaChatServer.class 8KB
ThreadReadDataFromClient.class 8KB
JavaChatServer.class 7KB
JavaChatClient.class 7KB
JavaChatClient.class 7KB
JavaChatClient.class 7KB
JavaChatServer.class 7KB
JavaChatClient.class 6KB
JavaChatServer.class 6KB
JavaChatClient.class 6KB
ThreadReadDataFromClient.class 6KB
ThreadReadDataFromClient.class 6KB
ThreadReadDataFromClient.class 6KB
ThreadReadDataFromClient.class 6KB
JavaChatClient.class 6KB
JavaChatClient.class 5KB
UserProcess.class 5KB
UserProcess.class 5KB
UserProcess.class 5KB
UserProcess.class 5KB
JavaChatServer.class 5KB
ThreadReadDataFromClient.class 4KB
UserProcess.class 4KB
UserProcess.class 4KB
UserProcess.class 4KB
UserProcess.class 4KB
ChatMenu.class 4KB
JavaChatClient.class 4KB
JavaChatClient.class 4KB
JavaChatClient.class 4KB
UserProcess.class 3KB
UserProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
UserFriendsInfoProcess.class 3KB
JavaChatServer.class 3KB
ThreadSendDataToAllClient.class 3KB
ThreadSendDataToAllClient.class 3KB
ThreadSendDataToAllClient.class 3KB
ThreadSendDataToAllClient.class 3KB
ChatMenu.class 3KB
ChatMenu.class 3KB
ChatMenu.class 3KB
ChatMenu.class 3KB
ChatMenu.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
UserEntity.class 3KB
ThreadReadDataFromClient.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
ThreadReadDataFromClient.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
test.class 2KB
ChatMenu.class 2KB
UserEntity.class 2KB
UserEntity.class 2KB
ThreadReadDataFromClient.class 2KB
ThreadReadDataFromClient.class 2KB
ChatMenu.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
SendData.class 2KB
共 650 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- 拓荒牛2013-05-23对于初学者,十分感谢分享,涉及到了很多知识点
SunLeibo
- 粉丝: 3
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功