基本的思路是这样的:在服务器端,有两个机器,一个对外开启 TCP 监听,然后把监听到的请求内容送到后面的另外一台,或者多台轮询机器上,内网之间使用 UDP,然后等待业务逻辑机器处理完成,这个地方可以做成异步的,然后再返回到用户。
即是这样: client server_front server_back
下面来看代码,首先是 server_fro…… 阅读全文
Category Archives: Tech
Linux UDP 收发实例
首先我们需要一个 struct.h
#ifndef STRUCT_H
#define STRUCT_H
typedef struct _UDP_MSG {
int add1;
int add2;
int sum;
char str1[128];
char str2[128];
char cat[256];
} UDP_MSG;
#endif /* STRUCT_H */
然后是服务端的
#include <sys/types.h>
#include <…… 阅读全文
Javascript 执行一次 MD5 或 SHA1 需要多久
首先是 Javascript 算 MD5 的问题,看到这里,http://pajhome.org.uk/crypt/md…,于是就是加一个时间打点,然后跑了
<script>
/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.2 Copyright (C) Paul Joh…… 阅读全文
共享内存的读写
首先我们需要一个 shm_com.h
#ifndef _SHM_COM_H_
#define _SHM_COM_H_
#define TEXT_SZ 2048
struct shared_use_st {
int written_by_you;
char some_text[TEXT_SZ];
};
#endif /* _SHM_COM_H */
然后是 shm1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unis…… 阅读全文
[转]一份高质量的 Epoll 服务端代码
来自https://banu.com/blog/2/how-to…,由http://www.oschina.net/transla…提及
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <f…… 阅读全文
不同服务器端模型在并发请求下的表现差异
一般来说,服务器的模型有下面几种:
单进程单线程
多进程(一个请求对应一个进程,Apache 之类貌似就是这个)
多线程(一个请求对应一个线程,这个挺少见到的)
select / poll
epoll
它们的差别可以看这个,http://www.cnblogs.com/sharra/…
因为需要了解底层设备访问的原理,所以惯用高层应用语言的我,需要了…… 阅读全文
根据节气自动换壁纸
前些时候拿到一套壁纸,是周洁的 24 节气,觉得很是不错,于是决定用作壁纸,但是每次节气到了,都要收手工去换,自然是不想的,于是这种事情就想起了 C# 同学,操作步骤很简单,新建一个 C# 控制台项目,然后代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
…… 阅读全文
shell 俄罗斯方块
利用 bash 脚本在 shell 上跑一个俄罗斯方块的游戏,还是挺有意思的,视频讲解在这里,http://v.youku.com/v_show/id_X…,http://v.youku.com/v_show/id_X…,代码的话这里有一份,http://www.chinaunix.net/old_j…,原样照搬过来
#!/bin/bash
# Tetris Game
# 10.21.2003 xhchen<[email]xhch…… 阅读全文
二级指针删除单向链表
今晚在这里看到一篇文章,http://coolshell.cn/articles/8…,原文给的是代码片段,本着动手实践的原则,另外顺便复习一下链表,写了下代码
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int n;
struct _node *next;
} node;
typedef int (* remove_fn)(node const *…… 阅读全文
signal 在 c99 下的差异
在写一个 minishell 的时候,需要捕捉从键盘输入的 ctrl+c 产生的 SIGINT 信号,转入自己的处理函数,代码大致如下
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
int shell_loop() {
setup();
COMBINE_COMMAND cmd;
while (1) {
…… 阅读全文