当前位置: 互联网>综合
本页文章导读:
▪搭建一个后台服务器--服务端代码(异步,大并发) #include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#i.........
▪在网络通信中如何对消息进行加密(一) 在网络通信中有时要对信息进行加密 ,可以直接用微软自带的CryptoAPI。加密方法大概可分为两种:
一 公钥加密技术:
加密和解密使用不同的密钥,分为公钥和私钥,私钥是不能让.........
▪巧用CSS文件愚人节恶搞 明天就是4月1日愚人节了,也就是那个可适度开玩笑、整蛊的日子了。如果你想和那些要上网的朋友或同事开个极客式玩笑,那就来试试这个国外网友Wes Bos分享的 CSS 文件吧。 一.........
[1]搭建一个后台服务器--服务端代码(异步,大并发)
来源: 互联网 发布时间: 2013-10-19
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#else
#include <WinSock2.h>
#define close( f ) closesocket( f )
#endif
struct my_event_s
{
int fd;
char recv[64];
char send[64];
int rc_pos;
int sd_pos;
};
int main( int argc, char** argv )
{
int port;
int flag;
int size;
int sock_server;
int sock_client;
time_t current;
time_t last;
int num;
int e_num;
int my_empty_index;
int i,j;
int event_flag;
#define EPOLL_MAX 51200
struct epoll_event wait_events[ EPOLL_MAX ];
struct my_event_s my_event[ EPOLL_MAX ];
struct my_event_s* tobe_myevent;
struct epoll_event tobe_event;
int epfd;
#define RECV_BUF_LEN 256
char buffer[ RECV_BUF_LEN ];
struct sockaddr_in addr_server;
struct sockaddr_in addr_client;
if( argc <= 1 )
{
printf( "please set your port\n" );
return 0;
}
printf( "your port:%s\n", argv[1] );
#ifdef WIN32
WSADATA wsadata;
flag = WSAStartup( 0x101, &wsadata );
if( flag )
{
printf( "your windows socket setup wrong\n" );
return 0;
}
#endif
port = atoi(argv[1]);
addr_server.sin_family = AF_INET;
addr_server.sin_port = htons( port );
addr_server.sin_addr.s_addr = htonl( INADDR_ANY );
sock_server = socket( AF_INET, SOCK_STREAM, 0 );
flag = fcntl( sock_server, F_GETFL, 0 );
fcntl( sock_server, F_SETFL, flag | O_NONBLOCK );
flag = bind( sock_server, ( struct sockaddr* )&addr_server, sizeof( struct sockaddr ) );
if( flag < 0 )
{
printf( "your bind is not ok\n" );
close( sock_server );
return 0;
}
flag = listen( sock_server, 1024 );
if( flag < 0 )
{
printf( "your listen is not ok\n");
close( sock_server );
return 0;
}
epfd = epoll_create( EPOLL_MAX );
if( epfd <= 0 )
{
printf( "event module could not be setup\n");
close( sock_server );
return 0;
}
tobe_event.events = EPOLLIN;
tobe_event.data.fd = sock_server;
epoll_ctl( epfd, EPOLL_CTL_ADD, sock_server, &tobe_event );
size = sizeof( addr_client );
num = 0;
last = 0;
my_empty_index = 0;
while(1)
{
#define WAIT_TIME_OUT 600
e_num = epoll_wait( epfd, wait_events, EPOLL_MAX, WAIT_TIME_OUT );
if( e_num <= 0 )
{
continue;
}
for( i = 0; i < e_num; ++i )
{
if( sock_server == wait_events[ i ].data.fd )
{
while(1)
{
sock_client = accept( sock_server, ( struct sockaddr* )&addr_client, ( socklen_t*)&size );
if( sock_client < 0 )
{
if( errno == EAGAIN )
{
break;
}
if( errno == EINTR )
{
continue;
}
break;
}
tobe_myevent = my_event + my_empty_index;
memset( tobe_myevent, 0, sizeof( struct my_event_s ) );
tobe_myevent->fd = sock_client;
flag = fcntl( sock_client, F_GETFL, 0 );
fcntl( sock_client, F_SETFL, flag | O_NONBLOCK );
tobe_event.events = EPOLLIN | EPOLLET;
tobe_event.data.u32 = my_empty_index;
epoll_ctl( epfd, EPOLL_CTL_ADD, sock_client, &tobe_event );
for( j = my_empty_index + 1; j < EPOLL_MAX; ++j )
{
if( !my_event[ j ].fd )
{
my_empty_index = j;
break;
}
}
if( my_event[j].fd )
{
for( j = 0; j < EPOLL_MAX; ++j )
{
if( !my_event[ j ].fd )
{
my_empty_index = j;
break;
}
}
if( my_event[ j ].fd )
{
printf( "your events has been none else\n");
close( sock_client );
close( sock_server );
return 0;
}
}
++num;
current = time( 0 );
if( current > last )
{
printf( "last sec qps:%d\n", num );
num = 0;
last = current;
}
memcpy( tobe_myevent->send, ¤t, sizeof(time_t) );
flag = recv( sock_client, tobe_myevent->recv, 64, 0 );
if( flag < 64 )
{
if( flag > 0 )
tobe_myevent->rc_pos += flag;
continue;
}
if( tobe_myevent->recv[31] || tobe_myevent->recv[63] )
{
printf( "your recv does follow the protocal\n");
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
flag = send( sock_client, tobe_myevent->send, sizeof( time_t ), 0 );
if( flag < sizeof( time_t ) )
{
tobe_event.events = EPOLLET | EPOLLOUT;
epoll_ctl( epfd, EPOLL_CTL_MOD, sock_client, &tobe_event );
if( flag > 0 )
tobe_myevent->sd_pos += flag;
continue;
}
tobe_myevent->fd = 0;
close( sock_client );
}
}
else
{
tobe_myevent = my_event + wait_events[ i ].data.u32;
sock_client = tobe_myevent->fd;
event_flag = wait_events[ i ].events;
if( event_flag | EPOLLHUP )
{
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
else if( event_flag | EPOLLERR )
{
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
else if( event_flag | EPOLLOUT )
{
if( tobe_myevent->rc_pos != 64 )
{
continue;
}
if( tobe_myevent->sd_pos >= sizeof( time_t ) )
{
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
flag = send( sock_client, tobe_myevent->send + tobe_myevent->sd_pos, sizeof( time_t ) - tobe_myevent->sd_pos, 0 );
if( flag < 0 )
{
if( errno == EAGAIN )
{
continue;
}
else if( errno == EINTR )
{
continue;
}
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
if( flag >0 )
{
tobe_myevent->sd_pos += flag;
if( tobe_myevent->sd_pos >= sizeof( time_t ) )
{
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
}
}
if( event_flag | EPOLLIN )
{
if( tobe_myevent->rc_pos < 64 )
{
flag = recv( sock_client, tobe_myevent->recv + tobe_myevent->rc_pos, 64 - tobe_myevent->rc_pos, 0 );
if( flag <= 0 )
{
continue;
}
tobe_myevent->rc_pos += flag;
if( tobe_myevent->rc_pos < 64 )
{
continue;
}
if( tobe_myevent->recv[31] || tobe_myevent->recv[63] )
{
printf( "your recv does follow the protocal\n");
tobe_myevent->fd = 0;
close( sock_client );
continue;
}
flag = send( sock_client, tobe_myevent->send, sizeof( time_t ), 0 );
if( flag < sizeof( time_t ) )
{
if( flag > 0 )
tobe_myevent->sd_pos += flag;
tobe_event.events = EPOLLET | EPOLLOUT;
tobe_event.data.u32 = wait_events[i].data.u32;
epoll_ctl( epfd, EPOLL_CTL_MOD, sock_client, &tobe_event );
continue;
}
tobe_myevent->fd = 0;
close( sock_client );
}
}
}
}
}
printf( "close server connection\n");
close( sock_server );
return 0;
}
作者:xiaofei_hah0000 发表于2013-3-31 20:05:04 原文链接
阅读:31 评论:0 查看评论
[2]在网络通信中如何对消息进行加密(一)
来源: 互联网 发布时间: 2013-10-19
在网络通信中有时要对信息进行加密 ,可以直接用微软自带的CryptoAPI。加密方法大概可分为两种:
一 公钥加密技术:
加密和解密使用不同的密钥,分为公钥和私钥,私钥是不能让别人看见的,而公钥可以公开,加密时用公钥进行加密,然后用公钥对应的私钥进行解密,公钥和私钥必须配对使用。这种技术安全性高,但效率低。
二 对称密钥加密技术
这种加密技术当中,加密密钥和解密密钥都是同一个,所以密钥必须只能让加密双方知道,否则就不安全,但是这种加密技术效率高。、
三 结合上面两种技术
用第一种加密技术加密对称加密的密钥,然后用对称加密技术加密通讯信息。
本章讲解的加密方法是第一种加密技术
首先在程序里面创建密钥器 使用函数 CryptAcquireContext 代码如下:
int AcqFlgs[] = { CRYPT_MACHINE_KEYSET, 0, CRYPT_NEWKEYSET|CRYPT_MACHINE_KEYSET, CRYPT_NEWKEYSET, -1 };
wsprintf(contName, "mytest.rsa%d.%s", keyBits,"Administrator");
hCsp=NULL;
for(int i=0;AcqFlgs[i]!=-1;i++)
{
hCsp=NULL;// 指向密钥容器的指针
if(CryptAcquireContext(&hCsp,contName,cspName,PROV_RSA_FULL,AcqFlgs[i]))//第二个参数是密钥容器的名称,第三个是密钥容器版本,可以为空,真阳就取默认值,第4个参数是密钥容器类型,不同的类型,支持不同的加密算法
break;
}然后创建密钥 用CryptGenKey 低吗如下:
CryptGenKey(hCsp,CALG_RSA_KEYX,(keyBits<<16)|CRYPT_EXPORTABLE,&hPrivKey)最后导出公钥块,将公钥块发送到客户端 客户端导入公钥块 就可获得公钥,然后加密信息 代码如下:
CryptExportKey(hPrivKey,0,PUBLICKEYBLOB,0,pubKeyBlob,(DWORD*)&pubKeyBlobLen)客户端加密过程如下:
CryptImportKey(target_csp, severKeyBlob, length-2, 0, 0, &hExKey);//导入服务器端的公钥块 生成公钥 BYTE message[1024]="Hello Server"; len=strlen((char*)message); CryptEncrypt(hExKey, 0, TRUE, 0, message, (DWORD *)&len, MAXBUF);//用此公钥对数据进行加密 memset(tempBuf,0,sizeof(tempBuf)); sprintf(tempBuf,"%d%s",SENDMESSAGE,":"); memcpy(tempBuf+2,message,len); send(clientSocket,tempBuf,len+2,0);
服务器端解密过程如下:
BYTE *tempByte=new BYTE[1024];
memcpy(tempByte,buf+2,lenght-2);
len=lenght-2;//加密数据的长度 很重要必须准确
CryptDecrypt(priKey.hKey, 0, TRUE, 0, tempByte, (DWORD *)&len);//用私钥进行解密
tempByte[len]=0;
printf("%s",(char*)tempByte);
delete []tempByte;示例程序下载地方
http://download.csdn.net/detail/xiaibiancheng/5206839
作者:XIAIBIANCHENG 发表于2013-3-31 20:04:45 原文链接
阅读:27 评论:0 查看评论
[3]巧用CSS文件愚人节恶搞
明天就是4月1日愚人节了,也就是那个可适度开玩笑、整蛊的日子了。如果你想和那些要上网的朋友或同事开个极客式玩笑,那就来试试这个国外网友Wes Bos分享的 CSS 文件吧。
一、打开浏览器的 Custom.css 文件本文以 Chrome 为例(CSS 修改后立即生效),进入同事或朋友的电脑,按下面方式打开 Custom.css 文件
- Mac:~/Library/Application Support/Google/Chrome/Default/User StyleSheets/Custom.css
- Windows XP:系统盘:\Documents and Settings\用户名\Local Settings\Application Data\Google\Chrome\User Data\Default\User StyleSheets\Custom.css(其他 Windows 系统类似,在个人账号中的找“应用数据” AppData……)
- Ubuntu (Chromium):~/.config/chromium/Default/User StyleSheets/Custom.css
1. 网页上下颠倒
/*
Turn every website upside down
*/
body {
-webkit-transform: rotate(180deg);
}
2. 网页旋转
/*
Spin every Website
*/
body {
/*-webkit-animation: spin 5s linear infinite;*/
}
3. 网页中所有图片上下颠倒
/*
Flip all images upside down
*/
img {
/*-webkit-transform: rotate(180deg);*/
}
4. 网页中所有图片都自转
/*
Spin all images
*/
img {
/*-webkit-animation: spin 1s linear infinite;*/
}
5. 网页倒在地上了(请用内容超过多屏的网页测试)
/*
Make every website fall over!
*/
/*
html, body {
height: 100%;
}
html {
-webkit-perspective: 1000;
}
body {
-webkit-transform-origin: bottom center;
-webkit-transform: rotateX(-90deg);
-webkit-animation: fall 1.5s ease-in;
}
*/
上面就列举这些了,其他恶搞内容,请参见下面这段 CSS 代码。
来源: 伯乐在线 翻译
评论《巧用CSS文件愚人节恶搞》的内容...
找不到相关文章,请发表留言
微博:新浪微博 - 腾讯微博
QQ群:186784064
月光博客投稿信箱:williamlong.info(at)gmail.com
Created by William Long www.williamlong.info
最新技术文章: