永发信息网

怎样有java socket实现发送接受表情类似QQ详细点代码

答案:2  悬赏:20  手机版
解决时间 2021-02-22 23:25
  • 提问者网友:書生途
  • 2021-02-22 08:28
怎样有java socket实现发送接受表情类似QQ详细点代码
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-02-22 09:52
发送的表情其实只是个代码
类似于/kb这样 所以和发送普通的文本消息是没有区别的
只是用户端在收到类似这样的信息后将其替换成了表情罢了

当然如果是发送自定义表情就复杂一点 像QQ都是把所有自定义表情都保存在一个目录下的 猜测应该是有个hash表 先传表情的hash值判断以前是否收到过这个表情 如果有就直接显示 如果没有就把表情接收过来
全部回答
  • 1楼网友:猎心人
  • 2021-02-22 10:36
哈 我正好在做一个类似qq的系统,不过还没完工,不过可以运行,现在只实现了登录,显示分组,列出好友,查看好友信息功册,看看代码吧
哎 类太多了,像什么vo类及实现类我就不发了,再说这些类没什么技术含量
如果有必要的话 你留下邮箱,我将两个工程和数据库代码发给你
服务器类:
package chat;
import impl.UserImpl;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import vo.Userinfo;
import dao.UserDao;

public class ChatServer extends Thread {
// 定义最大连接数
private static final int MAXLINK = 50;
// 记录当前连接数
public static int linkNum = 0;
// 定义服务连接对象
private static ServerSocket ss;
public static void main(String[] args) throws Exception {
// 实例化服务类
ss = new ServerSocket(8001);
// 启用多线程:其实就是调用run方法,好像没必要另启线程,哎 写好了 就不改了
new ChatServer().start();
System.out.println("服务器已成功启动…………");
}

public void run() {
Socket soc = null;
try {
// 循环监听客户端请求
while (true) {
while (true) {
// 如果连接数已满,则等待
if (linkNum >= MAXLINK)
this.sleep(100);
else {
break;
}
}
// 监听请求
soc = ss.accept();
// 将连接加一
linkNum++;
// 获得socket对象后调用处理类
new Operate(soc);
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (soc != null) {
try {
soc.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("服务器出现莫名故障,服务器退出!");
}
}
}
}

class Operate {
// 接定义用于实别客户端发来的头号请求将做何操作的变量
private String operate;

private Socket soc = null;
private InputStream ips = null;
private OutputStream ops = null;
private ObjectOutputStream soops = null;
private ObjectInputStream soips = null;

Operate(Socket soc) {
this.soc = soc;
try {
ips = soc.getInputStream();
ops = soc.getOutputStream();
soips = new ObjectInputStream(ips);
// 接收从客户端发来的头号请求,它用于实别此次请求的内容
operate = soips.readObject().toString();
} catch (Exception e) {
System.out.println(e);
}
// 调用实际操作方法
whichOpe();
}

private void whichOpe() {
if ("login".equals(operate)) {
login();
} else {
if ("".equals(operate)) {
}
}
}

private void login() {
UserDao userdao = new UserImpl();
Userinfo user = null;
try {
// 读取从客户端传过来的数据
user = (Userinfo) soips.readObject();
user = userdao.login(user);
soops = new ObjectOutputStream(ops);
// 将结果传给客户端
soops.writeObject(user);
soops.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
soops.close();
soips.close();
soc.close();
// 将连接数减一
ChatServer.linkNum--;
} catch (IOException e) {
System.out.println(e);
}
}
}
}
客户端的两个类:
package client;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import vo.Userinfo;

public class Login extends JFrame {
private final int TEXTSIZE = 10;// 定义文本长度的值
private JLabel lname = new JLabel("用户名");
private JLabel lpassword = new JLabel("密码");
private JLabel lhead = new JLabel("用户登录");
private JButton bok = new JButton("登录");
private JButton bcancel = new JButton("退出");
private JPanel pcenter = new JPanel();
private JPanel pname = new JPanel();
private JPanel ppwd = new JPanel();
private JPanel psouth = new JPanel();
private JPanel pnorth = new JPanel();
private TextField pwd = new TextField(TEXTSIZE);
private JTextField name = new JTextField(TEXTSIZE);
private GridLayout glayout = new GridLayout();
public Login() {
// 设置界面大小及位置
this.setBounds(300, 200, 320, 250);
this.setTitle("用户登录");
this.setResizable(false);
// 设置窗口的关闭方式
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
// 布置窗口
this.frameLayout();
// 添加事件
this.addAction();
// 显示窗口
this.show();
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}

private final void frameLayout() {
lhead.setFont(new Font("", Font.BOLD, 28));
glayout.setColumns(1);
glayout.setRows(2);
pcenter.setLayout(glayout);
pname.add(lname);
pname.add(name);
pwd.setEchoChar('*');
ppwd.add(lpassword);
ppwd.add(pwd);
pnorth.add(lhead);
psouth.add(bok);
psouth.add(bcancel);
pcenter.add(pname);
pcenter.add(ppwd);
this.add(pnorth, "North");
this.add(pcenter, "Center");
this.add(psouth, "South");
}

private final void addAction() {
bok.addActionListener(new AddAction());
bcancel.addActionListener(new AddAction());
name.addActionListener(new AddAction());
pwd.addActionListener(new AddAction());
}

private class AddAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bcancel) {
System.exit(0);
} else {
if ("".equals(name.getText()) || "".equals(pwd.getText())) {
JOptionPane.showMessageDialog(Login.this, "用户名和密码不能为空");
} else {
try {
// 判断输入的数据是否合法
Integer.parseInt(name.getText());
} catch (Exception ex) {
JOptionPane.showMessageDialog(Login.this, "请正确输入号码");
return;
}
new TcpLogin(name.getText(), pwd.getText()).check();
}
}
}
}

class TcpLogin {
private int name;
private String pwd = "";

TcpLogin(String name, String pwd) {
this.name = Integer.parseInt(name);
this.pwd = pwd;
}

void check() {
Window waitWin=new Window(Login.this);
// 获得界面
Login login = Login.this;
Userinfo user = new Userinfo();
user.setUserId(name);
user.setPassword(pwd);
Socket soc = null;
InputStream ips = null;
OutputStream ops = null;
ObjectInputStream coips = null;
ObjectOutputStream coops = null;
try {
soc = new Socket("127.0.0.1", 8001);
ips = soc.getInputStream();
ops = soc.getOutputStream();
coops = new ObjectOutputStream(ops);
coops.writeObject("login");
coops.writeObject(user);
coops.flush();
ObjectInputStream oips = new ObjectInputStream(ips);
user = null;
user = (Userinfo) oips.readObject();
if (user != null){
new XiHa(user);
login.dispose();
}
else {
JOptionPane.showMessageDialog(login, "用户名和密码错误");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ips.close();
ops.close();
soc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package client;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.tree.DefaultMutableTreeNode;
import vo.Groupinfo;
import vo.Relationinfo;
import vo.Userinfo;

public class XiHa extends JFrame {
// 好友信息
static List friends = new ArrayList();
// 用户信息
private Userinfo user = null;
private JPanel ptree = new JPanel();
private JPanel psouth = new JPanel();
private JButton bfind = new JButton("查找");
private JButton bsystem = new JButton("系统设置");

public XiHa(Userinfo user) {
this.user = user;
this.setTitle("嘻嘻哈哈");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(800, 100, 200, 500);
this.setResizable(false);
// 布置界面
initFrame();
// this.pack();
this.setVisible(true);
}

private void initFrame() {
ptree.add(new JScrollPane(new CreateTree(user).createNode()));
psouth.add(bsystem);
psouth.add(bfind);
this.add(ptree, "Center");
this.add(psouth, "South");
}
}

class CreateTree {
JTree tree=null;
private Userinfo user = null;
CreateTree(Userinfo user) {
this.user = user;
}

JTree createNode() {
//根结点
DefaultMutableTreeNode all = new DefaultMutableTreeNode("所有好友");
Set groups = user.getGroupinfos();
Set relations = user.getRelationinfosForUserId();
Iterator ig = groups.iterator();
Iterator ir = null;
Groupinfo group = null;
Relationinfo relation = null;
DefaultMutableTreeNode friendNode = null;
Userinfo friend = null;
while (ig.hasNext()) {
group = ig.next();
friendNode = new DefaultMutableTreeNode(group.getGroupName());
ir = relations.iterator();
while (ir.hasNext()) {
relation = ir.next();
if (relation.getResideGroupId() == group.getGroupId()) {
friend = relation.getFriend();
friendNode.add(new DefaultMutableTreeNode(friend
.getPetname()
+ "(" + relation.getFriendId() + ")"));
XiHa.friends.add(friend);
}
}
all.add(friendNode);
// rootTree.add(friendNode);
}
tree=new JTree(all);
tree.addMouseListener(new TreeListener());
return tree;
}

class TreeListener extends MouseAdapter{
private JFrame friendInfo=null;
private String nodeInfo="";
//用于区分单击双击
Timer mouseTimer =null;

public void mouseClicked(MouseEvent e){
//返回选定的结点
DefaultMutableTreeNode treeNode =null;
treeNode =(DefaultMutableTreeNode)CreateTree.this.tree.getLastSelectedPathComponent();
if(treeNode==null)return;
//如果它不是叶子结点就返回
if(!treeNode.isLeaf())return;
nodeInfo=treeNode.toString();
if(e.getClickCount()==1){
mouseTimer = new javax.swing.Timer(350, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//布置信息界面
initFriendInfo();
mouseTimer.stop();
}
});
mouseTimer.restart();
}else{
if(e.getClickCount()==2&&mouseTimer.isRunning()){
mouseTimer.stop();
//实例化聊天界面
new WinChat();
}
}
}

private void initFriendInfo() {
friendInfo=new JFrame();
friendInfo.setSize(300,120);
friendInfo.setDefaultCloseOperation(friendInfo.DISPOSE_ON_CLOSE);
friendInfo.setLocationRelativeTo(null);
friendInfoContent(this.treat());
friendInfo.setVisible(true);
}

private void friendInfoContent(int friendId) {
friendInfo.dispose();
Userinfo friend=this.findFriend(friendId);
JPanel pmain=new JPanel();
pmain.setLayout(new GridLayout(3,1));
JPanel panel1=new JPanel();
panel1.add(new JLabel("嘻哈号:"+friend.getUserId()+" "+"昵称:"+friend.getPetname()));
JPanel panel2=new JPanel();
panel2.add(new JLabel("姓名:"+(friend.getName()==null?"":friend.getName())+" "+"年龄:"+friend.getAge()+" "+"性别:"+friend.getSex()));
JPanel panel3=new JPanel();
panel3.add(new JLabel("个性签名: "+(friend.getAutograph()==null?"":friend.getAutograph())));
pmain.add(panel1);pmain.add(panel2);pmain.add(panel3);
friendInfo.add(pmain);
}

private int treat(){
int len=nodeInfo.getBytes().length;
int friendId=Integer.parseInt(new String(nodeInfo.getBytes(),len-5,4));
return friendId;
}

private Userinfo findFriend(int friendId){
Userinfo friend=null;
Iterator friends=XiHa.friends.iterator();
while(friends.hasNext()){
Userinfo ifriend=(Userinfo)friends.next();
if(ifriend.getUserId()==friendId){
friend=ifriend;
break;
}
}
return friend;
}
}
}

class WinChat extends JFrame{
private JPanel pmain=new JPanel();
private JPanel pcether=new JPanel();
private JPanel psouth=new JPanel();
//private JTextArea precod=new JTextArea(JTextArea.);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯