Socket服务端与客户端通讯

一、先看一下界面,本地服务端开启,同时开了二个客户端;


二、看看广域网

广域网服务开启,并且开了二个客户端


当前登录客户端的用户是user1,user1与user2通信

当前登录客户端的用户是user2,user2与user1通信


客户端依次断开


已实现:现在实现的是只有在线的客户端可以互相通信

三、待完善的

1:自己打开聊天窗口,对方未打开聊天窗口时,加消息提醒,并统计消息数量;

2:自己打开聊天窗口,对方打开聊天窗口时有未读消息,在打开窗口时加载未读消息;

3:对方发来消息,自己未打开聊天窗口时,加消息提醒,并统计消息数量;

4:自己打开聊天窗口时,有未读消息,在打开窗口时加载未读消息;

5:自己在线,对方不在线时,发送的消息服务器增加消息记录功能;

6:断线重连;

7:在线与非在线排序;

还有很多要完善的,不在一一列举。

四、关于代码

1、Wodsoft.Net,博客地址不记得了,搜一下可以找到,附一个地址https://blog.csdn.net/zip_xg/article/details/8083809

对于Wodsoft.Net中的很多东西都不太明白,只用到了TCPListener相关的。

2、ESBasic 附地址 https://www.cnblogs.com/zhuweisky/category/206662.html

ESBasic用的不多,只用到窗口管理,其他的没深入去看。

五、数据库

此项目用到的数据库为mysql,具体mysql代码如下:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50538
Source Host           : localhost:3306
Source Database       : dbsocket

Target Server Type    : MYSQL
Target Server Version : 50538
File Encoding         : 65001

Date: 2018-04-26 09:54:30
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `usermobile` varchar(20) DEFAULT NULL COMMENT '手机号',
  `userpwd` varchar(128) DEFAULT NULL COMMENT '密码',
  `userflag` int(1) DEFAULT '0' COMMENT '登录标识0默认允许,1禁止',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('8', 'user1', '123', '0');
INSERT INTO `user` VALUES ('9', 'user2', '123', '0');
INSERT INTO `user` VALUES ('10', 'user3', '123', '0');
INSERT INTO `user` VALUES ('11', 'user4', '123', '0');
INSERT INTO `user` VALUES ('12', 'user5', '123', '0');
INSERT INTO `user` VALUES ('17', 'user10', '123', '0');
INSERT INTO `user` VALUES ('16', 'user9', '123', '0');
INSERT INTO `user` VALUES ('15', 'user8', '123', '0');
INSERT INTO `user` VALUES ('14', 'user7', '123', '0');
INSERT INTO `user` VALUES ('13', 'user6', '123', '0');
INSERT INTO `user` VALUES ('18', 'user11', '123', '0');
INSERT INTO `user` VALUES ('19', 'user12', '123', '0');
INSERT INTO `user` VALUES ('20', 'user13', '123', '0');
INSERT INTO `user` VALUES ('21', 'user14', '123', '0');
INSERT INTO `user` VALUES ('22', 'user15', '123', '0');
INSERT INTO `user` VALUES ('23', 'user16', '123', '0');
INSERT INTO `user` VALUES ('24', 'user17', '123', '0');
INSERT INTO `user` VALUES ('25', 'user18', '123', '0');
INSERT INTO `user` VALUES ('26', 'user19', '123', '0');
INSERT INTO `user` VALUES ('27', 'user20', '123', '0');
INSERT INTO `user` VALUES ('28', 'user21', '123', '0');

-- ----------------------------
-- Table structure for `userfriend`
-- ----------------------------
DROP TABLE IF EXISTS `userfriend`;
CREATE TABLE `userfriend` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) DEFAULT NULL COMMENT '关联user表id',
  `friendid` int(11) DEFAULT NULL COMMENT '关联user表id',
  `friendflag` int(1) DEFAULT '0' COMMENT '0默认,1对隐身,2拉黑',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of userfriend
-- ----------------------------
INSERT INTO `userfriend` VALUES ('1', '8', '10', '0');
INSERT INTO `userfriend` VALUES ('2', '8', '11', '0');
INSERT INTO `userfriend` VALUES ('3', '8', '12', '0');
INSERT INTO `userfriend` VALUES ('4', '8', '13', '0');
INSERT INTO `userfriend` VALUES ('5', '8', '14', '0');
INSERT INTO `userfriend` VALUES ('6', '9', '15', '0');
INSERT INTO `userfriend` VALUES ('7', '9', '16', '0');
INSERT INTO `userfriend` VALUES ('8', '9', '17', '0');
INSERT INTO `userfriend` VALUES ('9', '9', '18', '0');
INSERT INTO `userfriend` VALUES ('10', '9', '19', '0');
INSERT INTO `userfriend` VALUES ('11', '9', '20', '0');
INSERT INTO `userfriend` VALUES ('12', '10', '21', '0');
INSERT INTO `userfriend` VALUES ('13', '10', '22', '0');
INSERT INTO `userfriend` VALUES ('14', '10', '23', '0');
INSERT INTO `userfriend` VALUES ('15', '10', '24', '0');
INSERT INTO `userfriend` VALUES ('16', '10', '25', '0');
INSERT INTO `userfriend` VALUES ('17', '10', '26', '0');
INSERT INTO `userfriend` VALUES ('18', '9', '10', '0');
INSERT INTO `userfriend` VALUES ('19', '9', '11', '0');
INSERT INTO `userfriend` VALUES ('20', '9', '12', '0');
INSERT INTO `userfriend` VALUES ('21', '9', '13', '0');
INSERT INTO `userfriend` VALUES ('22', '8', '15', '0');
INSERT INTO `userfriend` VALUES ('23', '8', '19', '0');
INSERT INTO `userfriend` VALUES ('24', '8', '24', '0');
INSERT INTO `userfriend` VALUES ('25', '10', '9', '0');
INSERT INTO `userfriend` VALUES ('26', '8', '9', '0');
INSERT INTO `userfriend` VALUES ('27', '9', '8', '0');

-- ----------------------------
-- Table structure for `userinfo`
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL COMMENT '姓名',
  `userheadimg` varchar(200) DEFAULT NULL COMMENT '头像地址',
  `usernickname` varchar(30) DEFAULT NULL COMMENT '昵称',
  `usersex` int(1) DEFAULT '0' COMMENT '0未知,1男,2女',
  `usermobile` varchar(20) DEFAULT NULL COMMENT '手机号',
  `conutry` varchar(30) DEFAULT NULL,
  `provice` varchar(30) DEFAULT NULL,
  `city` varchar(30) DEFAULT NULL,
  `area` varchar(30) DEFAULT NULL,
  `userid` int(11) DEFAULT '0' COMMENT '关联user表id',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('13', 'hiauok', 'hiauoklogo', 'hiauok', '0', '13716315870', 'CN', 'beijing', 'beijingshi', 'haidian', '8');
INSERT INTO `userinfo` VALUES ('14', '大明', 'daming', '大明', '0', '13716315870', 'CN', 'beijing', 'beijingshi', 'haidian', '9');
INSERT INTO `userinfo` VALUES ('15', '小明', 'xiaoming', '小明', '0', '13716315870', 'CN', 'beijing', 'beijingshi', 'haidian', '10');

-- ----------------------------
-- Table structure for `userloginlog`
-- ----------------------------
DROP TABLE IF EXISTS `userloginlog`;
CREATE TABLE `userloginlog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) DEFAULT NULL COMMENT '关联user表id',
  `loginclient` varchar(40) DEFAULT NULL COMMENT '登录者客户端',
  `logintime` int(11) DEFAULT NULL COMMENT '登录时间',
  `logintype` int(1) DEFAULT '0' COMMENT '0默认,1pc端,2移动端,3web端',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of userloginlog
-- ----------------------------


-- ----------------------------
-- Table structure for `useronline`
-- ----------------------------
DROP TABLE IF EXISTS `useronline`;
CREATE TABLE `useronline` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) DEFAULT NULL COMMENT '关联user表id',
  `userclient` varchar(40) DEFAULT NULL COMMENT '登录客户端',
  `logintime` int(11) DEFAULT '0' COMMENT '登录时间',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=86 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of useronline
-- ----------------------------

-- ----------------------------
-- Table structure for `usertalkmsg`
-- ----------------------------
DROP TABLE IF EXISTS `usertalkmsg`;
CREATE TABLE `usertalkmsg` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `selfid` varchar(20) DEFAULT NULL COMMENT '关联user表id',
  `friendid` varchar(20) DEFAULT NULL COMMENT '关联user表id',
  `isme` int(1) DEFAULT '0' COMMENT '发送者,0朋友,1自己',
  `talkmsg` text,
  `talktime` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of usertalkmsg
-- ----------------------------

-- ----------------------------
-- View structure for `userfriend_v`
-- ----------------------------
DROP VIEW IF EXISTS `userfriend_v`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `userfriend_v` AS select `user`.`id` AS `id`,`user`.`usermobile` AS `usermobile`,`user`.`userflag` AS `userflag`,`userfriend`.`friendid` AS `friendid`,`userfriend`.`friendflag` AS `friendflag`,`userfriend`.`userid` AS `userid` from (`userfriend` join `user` on((`userfriend`.`friendid` = `user`.`id`))) ;

-- ----------------------------
-- View structure for `userinfo_v`
-- ----------------------------
DROP VIEW IF EXISTS `userinfo_v`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `userinfo_v` AS select `user`.`usermobile` AS `usermobile`,`user`.`userflag` AS `userflag`,`userinfo`.`username` AS `username`,`userinfo`.`id` AS `id`,`userinfo`.`userheadimg` AS `userheadimg`,`userinfo`.`usernickname` AS `usernickname`,`userinfo`.`usersex` AS `usersex`,`userinfo`.`usermobile` AS `mobile`,`userinfo`.`conutry` AS `conutry`,`userinfo`.`provice` AS `provice`,`userinfo`.`city` AS `city`,`userinfo`.`area` AS `area`,`userinfo`.`userid` AS `userid` from (`user` join `userinfo` on((`userinfo`.`userid` = `user`.`id`))) ;

-- ----------------------------
-- View structure for `useronline_v`
-- ----------------------------
DROP VIEW IF EXISTS `useronline_v`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `useronline_v` AS select `user`.`id` AS `id`,`user`.`usermobile` AS `usermobile`,`user`.`userflag` AS `userflag`,`useronline`.`userclient` AS `userclient`,`useronline`.`logintime` AS `logintime` from (`user` join `useronline` on((`useronline`.`userid` = `user`.`id`))) ;

六、源码

请到下面的地址下载,全部源码

https://download.csdn.net/download/xuelin663/10379302

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值