写在前面:

  昨天在博客记录自己抽空写的一个Socket聊天程序的初始设计,那是这个程序的整体设计,为了完整性,今天把服务端的设计细化记录一下,首页贴出Socket聊天程序的服务端大体设计图,如下图:

功能说明:

  服务端主要有两个操作,一是阻塞接收客户端的socket并做响应处理,二是检测客户端的心跳,如果客户端一段时间内没有发送心跳则移除该客户端,由Server创建ServerSocket,然后启动两个线程池去处理这两件事(newFixedThreadPool,newScheduledThreadPool),对应的处理类分别是SocketDispatcher、SocketSchedule,其中SocketDispatcher根据socket不同的请求分发给不同SocketHandler去处理,而SocketWrapper则是对socket加了一层外壳包装,用lastAliveTime记录socket最新的交互时间,SocketHolder存储当前跟服务端交互的socket集合。

具体实现:

  [Server.java]

Server是服务端的入口,由Server的start()方法启动ServerSocket,然后阻塞接收客户端的请求,交由SocketDispatcher去分发,SocketDispatcher由newFixedThread类型的线程池启动,当连接数超过最大数据时将被队列处理,使用scheduleAtFixedRate启动SocketSchedule定时循环去监听客户端的心跳包,这两个类型都实现了Runnable接口,下面给出服务端的代码:

 package yaolin.chat.server;

 import java.io.IOException;
import java.net.ServerSocket;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import yaolin.chat.common.ConstantValue;
import yaolin.chat.util.LoggerUtil; /**
* 服务器
* @author yaolin
*/
public class Server { private final ServerSocket server;
private final ExecutorService pool; public Server() throws IOException {
server = new ServerSocket(ConstantValue.SERVER_PORT);
pool = Executors.newFixedThreadPool(ConstantValue.MAX_POOL_SIZE);
} public void start() {
try {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
// Watch dog. Exception??
schedule.scheduleAtFixedRate(new SocketSchedule(), 10, ConstantValue.TIME_OUT, TimeUnit.SECONDS); while (true) {
pool.execute(new SocketDispatcher(server.accept()));
LoggerUtil.info("ACCEPT A CLIENT AT " + new Date());
}
} catch (IOException e) {
pool.shutdown();
}
} public static void main(String[] args) {
try {
new Server().start();
} catch (IOException e) {
LoggerUtil.error("Server start failed! -> " + e.getMessage(), e);
}
}
}

  [SocketDispatcher.java]

Server只是服务端的入口,并指挥中心,SocketDispatcher才是服务端的指挥中心,对客户端不同的消息类型请求进行分发,让不同的SocketHandler去处理对应的消息请求,这里服务端和客户端的消息交互都是用JSON数据,所有消息类都继承BaseMessage,所以将接收到数据转换成BaseMessage类型,再判断其类型,(数据类型模块属于common模块),这里需要提一下的是当消息类型是文件类型的时候会睡眠配置执行的间隔时间,这样FileHandler才能有时间对文件流进行读取和重新发送给指定的客户端,而不会立即进入下一次循环对消息类型的判断(可能这里设计有点问题,不过暂时先这样做),下面给出SocketDispatcher的代码:

 /**
* SocketDispatcher
*
* @author yaolin
*/
public class SocketDispatcher implements Runnable { private final Socket socket; public SocketDispatcher(Socket socket) {
this.socket = socket;
} @Override
public void run() {
if (socket != null) {
while (!socket.isClosed()) {
try {
InputStream is = socket.getInputStream();
String line = null;
StringBuffer sb = null; if (is.available() > 0) { BufferedReader bufr = new BufferedReader(new InputStreamReader(is));
sb = new StringBuffer();
while (is.available() > 0 && (line = bufr.readLine()) != null) {
sb.append(line);
}
LoggerUtil.trach("RECEIVE [" + sb.toString() + "] AT " + new Date()); BaseMessage message = JSON.parseObject(sb.toString(), BaseMessage.class); switch (message.getType()) {
case MessageType.ALIVE:
HandlerFactory.getHandler(MessageType.ALIVE).handle(socket, sb.toString());
break;
case MessageType.CHAT:
HandlerFactory.getHandler(MessageType.CHAT).handle(socket, sb.toString());
break;
case MessageType.FILE:
HandlerFactory.getHandler(MessageType.FILE).handle(socket, sb.toString());
LoggerUtil.trach("SEVER:PAUSE TO RECEIVE FILE");
Thread.sleep(ConstantValue.MESSAGE_PERIOD);
break;
case MessageType.LOGIN:
HandlerFactory.getHandler(MessageType.LOGIN).handle(socket, sb.toString());
break;
case MessageType.LOGOUT:
break;
case MessageType.REGISTER:
HandlerFactory.getHandler(MessageType.REGISTER).handle(socket, sb.toString());
break;
}
} else {
Thread.sleep(ConstantValue.MESSAGE_PERIOD);
}
} catch (Exception e) { // catch all handler exception
LoggerUtil.error("SocketDispatcher Error!" + e.getMessage(), e);
}
}
}
}
}

  [SocketSchedule.java]

跟Server有直接关系的另一个类(组件)是SocketSchedule,SocketSchedule主要负责检测客户端的最新一次跟服务端的交互时间是否超过系统配置允许最大的时间,如果超过了,则将该客户端socket从服务端移除,否则更新客户端的最新一次跟服务端的交互时间。下面是具体的实现:

 /**
* Remove socket from SocketHolder if lastAliveTime > TIME_OUT
* @author yaolin
*
*/
public class SocketSchedule implements Runnable { @Override
public void run() {
for (String key : SocketHolder.keySet()) {
SocketWrapper wrapper = SocketHolder.get(key);
if (wrapper != null && wrapper.getLastAliveTime() != null) {
if (((new Date().getTime() - wrapper.getLastAliveTime().getTime()) / 1000) > ConstantValue.TIME_OUT) {
// remove socket if timeout
SocketHolder.remove(key);
}
}
}
}
}

  [SocketHolder.java、SocketWrapper.java]

从上面的代码可以看出,SocketSchedule#run()只是简单的对时间进行一次判断,真正有意义的其实是SocketHolder和SocketWrapper,SocketWrapper则是对socket加了一层外壳包装,SocketHolder的存储了当前有效时间内所有跟服务端有交互的客户端,SocketHolder以客户端的唯一标识(这里使用用户名),作为KEY,客户端所在的socket作为VALUE的键值对形式存储,其中SocketHolder#flushClientStatus()的处理逻辑是用于通知其他客户端当前客户端的上线/离线状态,下面给出这两个类的具体实现:

 /**
* Wrap Socket, SocketSchedule remove socket if lastAliveTime > TIME_OUT
* @author yaolin
*
*/
public class SocketWrapper { private Socket socket;
private Date lastAliveTime; // full constructor
public SocketWrapper(Socket socket, Date lastAliveTime) {
this.socket = socket;
this.lastAliveTime = lastAliveTime;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public Date getLastAliveTime() {
return lastAliveTime;
}
public void setLastAliveTime(Date lastAliveTime) {
this.lastAliveTime = lastAliveTime;
}
 /**
* SocketHolder
* @author yaolin
*/
public class SocketHolder { private static ConcurrentMap<String, SocketWrapper> listSocketWrap = new ConcurrentHashMap<String, SocketWrapper>(); public static Set<String> keySet() {
return listSocketWrap.keySet();
} public static SocketWrapper get(String key) {
return listSocketWrap.get(key);
} public static void put(String key, SocketWrapper value) {
listSocketWrap.put(key, value);
flushClientStatus(key, true);
} public static SocketWrapper remove(String key) {
flushClientStatus(key, false);
return listSocketWrap.remove(key);
} public static void clear() {
listSocketWrap.clear();
} /**
* <pre>content:{username:"",flag:false}</pre>
* @param flag true:put,false:remove;
*/
private static void flushClientStatus(String key, boolean flag) {
ClientNotifyDTO dto = new ClientNotifyDTO(flag, key);
ReturnMessage rm = new ReturnMessage().setKey(Key.NOTIFY).setSuccess(true).setContent(dto);
rm.setFrom(ConstantValue.SERVER_NAME);
for (String toKey : listSocketWrap.keySet()) {
if (!toKey.equals(key)) { // not send to self
rm.setTo(toKey);
SocketWrapper wrap = listSocketWrap.get(toKey);
if (wrap != null) {
SendHelper.send(wrap.getSocket(), rm);
}
}
}
}
}

  [SocketHandler.java、HandlerFactory.java、OtherHandlerImpl.java]

SocketDispatcher让不同的SocketHandler去处理对应的消息请求,SocketHandler的设计其实就是一套简单的工厂组件吧(其中ReturnHandler暂时由SendHelper实现信息传送,暂时没有用到,已经@Deprecated ,这里还是给出),完整类图如下:

下面给出这一块的代码,为了缩小篇幅,将所有Handler实现的代码收起来。

 /**
* SocketHandler
* @author yaolin
*/
public interface SocketHandler {
/**
* Handle Client Socket
*/
public Object handle(Socket client,Object data);
 /**
* SocketHandlerFactory
* @author yaolin
*/
public class HandlerFactory { // can not create instance
private HandlerFactory(){} public static SocketHandler getHandler(int type) {
switch (type) {
case MessageType.ALIVE: // usually use
return new AliveHandler();
case MessageType.CHAT:
return new ChatHandler();
case MessageType.LOGIN:
return new LoginHandler();
// case MessageType.RETURN:
// return new ReturnHandler();
case MessageType.LOGOUT:
return new LogoutHandler();
case MessageType.REGISTER:
return new RegisterHandler();
case MessageType.FILE:
return new FileHandler();
}
return null; // NullPointException
}
 /**
* AliveSocketHandler
* @author yaolin
*/
public class AliveHandler implements SocketHandler { /**
* @return null
*/
@Override
public Object handle(Socket client, Object data) {
if (data != null) {
BaseMessage message = JSON.parseObject(data.toString(), BaseMessage.class);
if (StringUtil.isNotEmpty(message.getFrom())) {
SocketWrapper wrapper = SocketHolder.get(message.getFrom());
if (wrapper != null) {
wrapper.setLastAliveTime(new Date()); // KEEP SOCKET ...
SocketHolder.put(message.getFrom(), wrapper);
}
}
}
return null;
} }
 /**
* ChatHandler
*
* @author yaolin
*/
public class ChatHandler implements SocketHandler { @Override
public Object handle(Socket client, Object data) {
if (data != null) {
ChatMessage message = JSON.parseObject(data.toString(), ChatMessage.class); if (StringUtil.isNotEmpty(message.getFrom()) && StringUtil.isNotEmpty(message.getTo())) {
// exist & send
if (SocketHolder.keySet().contains(message.getFrom())) {
String owner = message.getFrom();
message.setOwner(owner); // owner will be display
if (ConstantValue.TO_ALL.equals(message.getTo())) { // one-to-all
// TO_ALL TAB will be select;
message.setFrom(ConstantValue.TO_ALL);
for (String key : SocketHolder.keySet()) {
// also send to self
SocketWrapper wrapper = SocketHolder.get(key);
if (wrapper != null) {
SendHelper.send(wrapper.getSocket(), message);
}
}
} else {// one-to-one
SocketWrapper wrapper = SocketHolder.get(message.getTo());
if (wrapper != null) {
// owner = from
SendHelper.send(wrapper.getSocket(), message);
// also send to self
// TO TAB will be select;
message.setFrom(message.getTo()).setTo(owner);
SendHelper.send(client, message);
}
}
}
}
}
return null;
}
}
 public class FileHandler implements SocketHandler {

     @Override
public Object handle(Socket client, Object data) {
if (client != null) {
FileMessage message = JSON.parseObject(data.toString(), FileMessage.class);
if (StringUtil.isNotEmpty(message.getFrom()) && StringUtil.isNotEmpty(message.getTo())) {
// exist & send
if (SocketHolder.keySet().contains(message.getFrom())) {
if (!ConstantValue.TO_ALL.equals(message.getTo())) { // one-to-all
SocketWrapper wrapper = SocketHolder.get(message.getTo());
if (wrapper != null) {
SendHelper.send(wrapper.getSocket(), message);
try {
if (client != null && wrapper.getSocket() != null && message.getSize() > 0) {
InputStream is = client.getInputStream();
OutputStream os = wrapper.getSocket().getOutputStream();
int total = 0;
while (!client.isClosed() && !wrapper.getSocket().isClosed()) {
if (is.available() > 0) {
byte[] buff = new byte[ConstantValue.BUFF_SIZE];
int len = -1;
while (is.available() > 0 && (len = is.read(buff)) != -1) {
os.write(buff, 0, len);
total += len;
LoggerUtil.debug("SEND BUFF [" + len + "]");
}
os.flush();
if (total >= message.getSize()) {
LoggerUtil.info("SEND BUFF [OK]");
break;
}
}
}
// AFTER SEND FILE
// SEND SUCCESSFULLY
ReturnMessage result = new ReturnMessage().setKey(Key.TIP)
.setSuccess(true)
.setContent(I18N.INFO_FILE_SEND_SUCCESSFULLY);
result.setFrom(message.getTo()).setTo(message.getFrom())
.setOwner(ConstantValue.SERVER_NAME);
SendHelper.send(client, result);
// RECEIVE SUCCESSFULLY
result.setContent(I18N.INFO_FILE_RECEIVE_SUCCESSFULLY)
.setFrom(message.getFrom())
.setTo(message.getTo());
SendHelper.send(wrapper.getSocket(), result);
}
} catch (Exception e) {
LoggerUtil.error("Handle file failed !" + e.getMessage(), e);
}
}
}
}
}
}
return null;
}
}
 /**
* LoginHandler
*
* @author yaolin
*
*/
public class LoginHandler implements SocketHandler { private UsrService usrService = new UsrService(); @Override
public Object handle(Socket client, Object data) {
ReturnMessage result = new ReturnMessage();
result.setSuccess(false);
if (data != null) {
LoginMessage message = JSON.parseObject(data.toString(), LoginMessage.class);
if (StringUtil.isNotEmpty(message.getUsername()) && StringUtil.isNotEmpty(message.getPassword())) {
if (usrService.login(message.getUsername(), message.getPassword()) != null) {
result.setSuccess(true);
} else {
result.setMessage(I18N.INFO_LOGIN_ERROR_DATA);
}
result.setFrom(ConstantValue.SERVER_NAME).setTo(message.getUsername());
} else {
result.setMessage(I18N.INFO_LOGIN_EMPTY_DATA);
}
// AFTER LOGIN
result.setKey(Key.LOGIN);
if (result.isSuccess()) { // HOLD SOCKET
SocketHolder.put(result.getTo(), new SocketWrapper(client, new Date()));
}
SendHelper.send(client, result);
if (result.isSuccess()) { // SEND LIST USER
ClientListUserDTO dto = new ClientListUserDTO();
dto.setListUser(SocketHolder.keySet());
result.setContent(dto).setKey(Key.LISTUSER);
SendHelper.send(client, result);
}
}
return null;
} }
 public class LogoutHandler implements SocketHandler {

     @Override
public Object handle(Socket client, Object data) {
if (data != null) {
LogoutMessage message = JSON.parseObject(data.toString(), LogoutMessage.class);
if (message != null && StringUtil.isNotEmpty(message.getFrom())) {
SocketWrapper wrapper = SocketHolder.get(message.getFrom());
Socket socket = wrapper.getSocket();
if (socket != null) {
try {
socket.close();
socket = null;
} catch (Exception ignore) {
}
}
SocketHolder.remove(message.getFrom());
}
}
return null;
} }
 public class RegisterHandler implements SocketHandler {

     private UsrService usrService = new UsrService();

     @Override
public Object handle(Socket client, Object data) {
ReturnMessage result = new ReturnMessage();
result.setSuccess(false).setFrom(ConstantValue.SERVER_NAME);
if (data != null) {
RegisterMessage message = JSON.parseObject(data.toString(), RegisterMessage.class);
if (StringUtil.isNotEmpty(message.getUsername()) && StringUtil.isNotEmpty(message.getPassword())) {
if (usrService.register(message.getUsername(), message.getPassword()) != null) {
result.setSuccess(true).setContent(I18N.INFO_REGISTER_OK);
} else {
result.setMessage(I18N.INFO_REGISTER_CLIENT_EXIST);
}
} else {
result.setMessage(I18N.INFO_REGISTER_EMPTY_DATA);
} if (StringUtil.isNotEmpty(message.getUsername())) {
result.setTo(message.getUsername());
}
// AFTER REGISTER
result.setKey(Key.REGISTER);
SendHelper.send(client, result);
}
return null;
} }
 /**
* Use SendHelper to send ReturnMessage,
* @see yaolin.chat.server.SocketDispatcher#run()
* @author yaolin
*/
@Deprecated
public class ReturnHandler implements SocketHandler { /**
* @param data ReturnMessage
*/
@Override
public Object handle(Socket client, Object data) {
if (data != null) {
ReturnMessage message = (ReturnMessage) data;
if(StringUtil.isNotEmpty(message.getFrom()) && StringUtil.isNotEmpty(message.getTo())) {
SocketWrapper wrap = SocketHolder.get(message.getTo());
if (wrap != null) {
SendHelper.send(wrap.getSocket(), message);
}
}
}
return null;
} }

用户业务:

  服务端除了socket之外,还有一点点具体的业务,那就是用户的注册、登陆等,这里简单的列出Usr和UsrService这两个类,这些业务暂时没有怎么实现,我并不打算在这个程序中引入ORM框架,所以自己写一套DBUtil(待改善),在这里也一并贴出来。

这里只进行了简单的校验,没有持久化存储到DB中,下面是Usr和UsrService:

 public class Usr {

     private long id;
private String username;
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
 /**
* // TODO
* @see yaolin.chat.server.usr.repository.UsrRepository
* @author yaolin
*
*/
public class UsrService {
// TODO db
private static Map<String,Usr> db = new HashMap<String,Usr>(); public Usr register(String username, String password) {
if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
return null;
}
if (db.containsKey(username)) {
return null; // exist;
}
Usr usr = new Usr();
usr.setUsername(username);
usr.setPassword(MD5Util.getMD5Code(password));
db.put(username, usr);
return usr;
} public Usr login(String username, String password) {
if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
return null;
}
if (db.containsKey(username)) {
Usr usr = db.get(username);
if (MD5Util.getMD5Code(password).equals(usr.getPassword())) {
return usr;
}
}
return null;
}
}

下面是DBUtil工具:

 /**
* DBUtils // TODO 有待调整&优化!!
* @author yaolin
*/
public class DBUtil {
// make connection used repeatedly
private static final List<Connection> cache = new LinkedList<Connection>();
private static String url;
private static String driver;
private static String user;
private static String password;
private static Boolean debug; static {
InputStream is = DBUtil.class.getResourceAsStream("/db.properties");
try {
Properties p = new Properties();
p.load(is);
url = p.getProperty("url");
driver = p.getProperty("driver");
user = p.getProperty("user");
password = p.getProperty("password");
// just for debug
try {
debug = Boolean.valueOf(p.getProperty("debug"));
} catch (Exception ignore) {
debug = false;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
is = null;
} catch (Exception ignore) {
}
}
}
} public synchronized static Connection getConnection() {
if (cache.isEmpty()) {
cache.add(makeConnection());
}
Connection conn = null;
int i = 0;
try {
do {
conn = cache.remove(i);
} while (conn != null && conn.isClosed() && i < cache.size());
} catch (Exception ignore) {
} try {
if (conn == null || conn.isClosed()) {
cache.add(makeConnection());
conn = cache.remove(0);
}
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
} public synchronized static void close(Connection connection) {
try {
if (connection != null && !connection.isClosed()) {
if (debug)
debug("release connection!");
cache.add(connection);
}
} catch (SQLException ignore) {
}
} public static Object query(String sql, ResultSetMapper mapper, Object... args) {
if (debug)
debug(sql);
Connection conn = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
Object result = null;
try {
ps = conn.prepareStatement(sql);
int i = 1;
for (Object object : args) {
ps.setObject(i++, object);
}
rs = ps.executeQuery();
result = mapper.mapper(rs);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
} catch (Exception ignore) {
}
}
close(conn);
return result;
} public static int modify(String sql, Object... args) {
if (debug)
debug(sql);
Connection conn = getConnection();
PreparedStatement ps = null;
int row = 0;
try {
ps = conn.prepareStatement(sql);
int i = 1;
for (Object object : args) {
ps.setObject(i++, object);
}
row = ps.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
ps = null;
}
} catch (Exception ignore) {
}
}
close(conn);
return row;
} public static int[] batch(List<String> sqls) {
if (debug)
debug(sqls.toString());
Connection conn = getConnection();
Statement stmt = null;
int[] row;
try {
stmt = conn.createStatement();
for (String sql : sqls) {
stmt.addBatch(sql);
}
row = stmt.executeBatch();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (Exception ignore) {
}
}
close(conn);
return row;
} public static int[] batch(String sql, PreparedStatementSetter setter) {
if (debug)
debug(sql);
Connection conn = getConnection();
PreparedStatement ps = null;
int[] row;
try {
ps = conn.prepareStatement(sql);
setter.setter(ps);
row = ps.executeBatch();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (ps != null) {
ps.close();
ps = null;
}
} catch (Exception ignore) {
}
}
close(conn);
return row;
} private static Connection makeConnection() {
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
if (debug)
debug("create connection!");
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
} private static void debug(String sqls) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())
+ " DEBUG " + Thread.currentThread().getId()
+ " --- [" + Thread.currentThread().getName() + "] " + "excute sqls : " + sqls);
}
 /**
* PreparedStatementSetter
* @author yaolin
*/
public interface PreparedStatementSetter {
public void setter(PreparedStatement ps);
 /**
* ResultSetMapper
* @author yaolin
*/
public interface ResultSetMapper {
public Object mapper(ResultSet rs);
}

对于DBUtil的使用参考我之前写过了一个小demo,目前挂在github上:https://github.com/jAVA-yaolin/web

未完,待续。

(本文所有的代码已经挂在csdn的仓库中 :地址 https://code.csdn.net/yaoIin/niloay-chat ,完整代码参考这里,有时间将更新)

Socket聊天程序——服务端的更多相关文章

  1. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  2. 用c++语言socket库函数实现服务端客户端聊天室

    客户端 /* * 程序名:client.cpp,此程序用于演示socket的客户端 * 作者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include < ...

  3. Socket聊天程序——客户端

    写在前面: 上周末抽点时间把自己写的一个简单Socket聊天程序的初始设计和服务端细化设计记录了一下,周二终于等来毕业前考的软考证书,然后接下来就是在加班的日子度过了,今天正好周五,打算把客户端的详细 ...

  4. java实现微信小程序服务端(登录)

    微信小程序如今被广泛使用,微信小程序按照微信官网的定义来说就是: 微信小程序是一种全新的连接用户与服务的方式,它可以在微信内被便捷地获取和传播,同时具有出色的使用体验. 这就是微信小程序的魅力所在,有 ...

  5. 简单的同步Socket程序服务端

    首先,Socket是.Net提供的 System.Net.Sockets命名空间的Scoket类为网络通信提供了一套丰富的方法和属性 服务器按照Socket的基本流程 先创建Socket 在用Bind ...

  6. Socket聊天程序——初始设计

    写在前面: 可能是临近期末了,各种课程设计接踵而来,最近在csdn上看到2个一样问答(问题A,问题B),那就是编写一个基于socket的聊天程序,正好最近刚用socket做了一些事,出于兴趣,自己抽了 ...

  7. socket 客户端和服务端通信

    客户端要连接服务器:首先要知道服务器的IP地址.而服务器里有很多的应用程序,每一个应用程序对应一个端口号 所以客户端想要与服务器中的某个应用程序进行通信就必须要知道那个应用程序的所在服务器的IP地址, ...

  8. Socket通信时服务端无响应,客户端超时设置

    背景:在写一个客户端的socket程序,服务端没有返回消息,客户端一直在等待. 目标:我需要设置一个时间,如果超过这个时间客户端自动断开连接.最好是在服务端实现,客户端对我来说不可控.

  9. 基于 IOCP 的通用异步 Windows Socket TCP 高性能服务端组件的设计与实现

    设计概述 服务端通信组件的设计是一项非常严谨的工作,其中性能.伸缩性和稳定性是必须考虑的硬性质量指标,若要把组件设计为通用组件提供给多种已知或未知的上层应用使用,则设计的难度更会大大增加,通用性.可用 ...

随机推荐

  1. C#与C++的发展历程第三 - C#5.0异步编程巅峰

    系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 3. C#与C++的发展历程第三 - C#5.0异步编程的巅峰 C#5.0 ...

  2. C# 发送邮件 附件名称为空

     示例代码: // 1.创建邮件 MailMessage mailMsg = new MailMessage(); mailMsg.To.Add(new MailAddress("test@ ...

  3. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

  4. gradle学习笔记(1)

    1. 安装     (1) 下载最新gradle压缩包,解压到某处.地址是:Gradle web site:     (2) 添加环境变量:             1) 变量名:GRADLE_HOM ...

  5. html与html5

    HTML 是一种在 Web 上使用的通用标记语言.HTML 允许你格式化文本,添加图片,创建链接.输入表单.框架和表格等等,并可将之存为文本文件,浏览器即可读取和显示.HTML 的关键是标签,其作用是 ...

  6. 学习笔记之MVC级联及Ajax操作

    由于刚转型到MVC,MVC的架构模式很多不是很清楚,比如今天就想做个级联的操作,因为之前的ASP.NET的方式是通过:控件-->添加事件-->后台编写级联事件进行触发,但是这个MVC就不同 ...

  7. 前端制作动画的几种方式(css3,js)

    制作动态的网页是是前端工程师必备的技能,很好的实现动画能够极大的提高用户体验,增强交互效果,那么动画有多少实现方式,一直对此有选择恐惧症的我就总结一下,以便在开发的时候选择最好的实现方式. 1.css ...

  8. 初学seaJs模块化开发,利用grunt打包,减少http请求

    原文地址:初学seaJs模块化开发,利用grunt打包,减少http请求 未压缩合并的演示地址:demo2 学习seaJs的模块化开发,适合对seajs基础有所了解的同学看,目录结构 js — —di ...

  9. cocos2dx调用浏览器打开网址

    安卓端cocos2dx/platform/android路径下CCApplication.h: virtual void openURL(const char* pszUrl); CCApplicat ...

  10. SharePoint 2016 入门视频教程

    之前一直有朋友让自己录一些SharePoint的入门视频,之前没有太多时间,一个巧合的机会收到CSDN学院的邮件,可以在CSDN上发布视频教程,自己就录了一些.说起录视频也是蛮辛苦的,每天下班吃完饭要 ...