因为公司有个项目需做socket server 和 client,客户端已经有了,差个集中管控的服务端,就写了个,但是想看一下服务器并发效果,公司内又没有那么多client设备,只好自己写个测试程序,不多说,上代码。
模拟客户client程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "tl_md5.h"
#define PORT 8098
#define DESIP "127.0.0.1"
//#define DESIP "192.168.165.1"
#define MAXSIZE (8 * 4096)
static char ip_addr[32];
static pthread_mutex_t ha_lock = PTHREAD_MUTEX_INITIALIZER;
void ha_lock_lock(void)
{
pthread_mutex_lock(&ha_lock);
}
void ha_lock_unlock(void)
{
pthread_mutex_unlock(&ha_lock);
}
char *get_md5_checkcode(char *pszdevtype, char *pszdevid, char *pszcode){
//static char szcode[36]={0};
char szdatabuf[62]={0};
unsigned char sign_val[16]={0};
MD5_CTX md5;
int idx, nlen=0;
if(strlen(pszcode) < 32){
snprintf(szdatabuf,sizeof(szdatabuf)-1,"%s%sYHWLAN",pszdevtype,pszdevid);
MD5Init(&md5);
MD5Update(&md5,(unsigned char *)szdatabuf,strlen(szdatabuf));
MD5Final(&md5,sign_val);
for(idx = 0; idx < 16;idx++)
{
sprintf(&pszcode[nlen],"%02x",sign_val[idx]);
nlen = strlen(pszcode);
}
}
// printf("%s: md5 %s\n", __func__, pszcode);
return pszcode;
}
int web_child(int count)
{
time_t ticks;
char buff[2024];
char *lpbuf = NULL;
char *pjson = NULL;
char devid[10];
char md5_check[32];
int request_kind = 0;
int ret = 0;
int is_registered = 0;
int sockfd;
struct sockaddr_in des_addr;
char sendmsg[4096];
char recvmsg[4096];
int switch_a =0;
char mac[12];
sprintf(mac,"%06X",count);
// printf("%s --count %d-- \n",__func__, count);
ha_lock_lock();
get_md5_checkcode("AR9xxx", &mac[0], &md5_check[0]);
ha_lock_unlock();
int rand2 = rand();
int nsleep = rand()%5;
printf("count is %d rand is %d nsleep is %d\n",count,rand2,nsleep);
sleep(nsleep);
while(1){
printf("count %d --begin while \n",count);
sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建socket
if (sockfd < 0) {
printf("count is %d socket error\n",count);
return -1;
}
/* 设置连接目的地址 */
des_addr.sin_family = AF_INET;
des_addr.sin_port = htons(PORT);
des_addr.sin_addr.s_addr = inet_addr(ip_addr);
bzero(&(des_addr.sin_zero), 8);
/* 发送连接请求 */
if (connect(sockfd, (struct sockaddr *)&des_addr, sizeof(struct sockaddr)) < 0) {
printf("count is %d connect failed\n",count);
return -1;
}
if(switch_a == 0){
sprintf(sendmsg,"POST /ThinkPHP/index.php/Home/Wlan/regDevice HTTP/1.1\nContent-Type: application/json;charset=UTF-8\nmyjson:{\"code\":\"reg\",\"myDevtype\":\"AR9xxx\",\"myDevid\":\"%s\",\"myDevsoftver\":\"1.00\",\"myDevhardver\":\"V1.0\",\"myDevcfgver\":\"V1.6\",\"myDevmode\":\"routed\",\"myCheck\":\"%s\"}\nHOST: 202.10.79.172:8098\nUser-Agent: Younghead iWBS 1.0\nCache-Control: no-cache\nContent-Type: text/html;charset=UTF-8\nAccept: */*\nContent-Length: 0\n",mac,md5_check);
switch_a = 1;
}else{
sprintf(sendmsg,"POST /ThinkPHP/index.php/Home/Wlan/hbDevice HTTP/1.1\nContent-Type: application/json;charset=UTF-8\nmyjson:{\"code\":\"reg\",\"myDevid\":\"%s\",\"myMsg\":\"V1.0\",}\nHOST: 202.10.79.172:8098\nUser-Agent: Younghead iWBS 1.0\nCache-Control: no-cache\nContent-Type: text/html;charset=UTF-8\nAccept: */*\nContent-Length: 0\n",mac);
switch_a = 0;
}
if (send(sockfd, sendmsg, strlen(sendmsg) + 1, 0) < 0) {//发送信息
printf("count is %d send msg failed\n",count);
close(sockfd);
return -1;
}
memset(recvmsg, 0, sizeof(recvmsg));
if (recv(sockfd, recvmsg, MAXSIZE, 0) < 0) {//接收信息
printf("count is %d server has no reply\n",count);
close(sockfd);
}else{
printf("count is %d recv is %s\n",count,recvmsg);
}
close(sockfd);
if(switch_a){
sleep(rand()%1);
}else{
//sleep(290);
sleep(rand()%1);
}
}
}
void *doit(void *arg)
{
//void web_child(int);
pthread_detach(pthread_self());
web_child((int) arg);
//close((int) arg);
return(NULL);
}
int main(int argc, char *argv[])
{
int sockfd;
char abc[20] = "hello";
if(argc != 2){
printf("need a ip addr\n");
exit(0);
}
strcpy(ip_addr,argv[1]);
char recvmsg[MAXSIZE];
pthread_t tid;
int COUNT = 1;
for(COUNT=1; COUNT<=64; COUNT++){
pthread_create(&tid, NULL, &doit, (void *) COUNT);
}
// char szcode[33];
// get_md5_checkcode("YH-YB04", "454545454545", szcode);
// printf("md5 is %s \n", szcode);
while(1){
sleep(20);
printf("%s ---- %d \n",__func__, __LINE__);
}
return 0;
}
这是服务server程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "tl_md5.h"
#define MAXN 16384 /* max #bytes that a client can request */
#define SA struct sockaddr
#define mzero(ptr,n) memset(ptr, 0, n)
#define MAXFORK 25
int max_fork_num = 0;
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
void my_lock_init(char *pathname)
{
}
void my_lock_wait()
{
pthread_mutex_lock(&mylock);
}
void my_lock_release()
{
pthread_mutex_unlock(&mylock);
}
int increase_pthreads_coounts()
{
if(max_fork_num >= MAXFORK){
printf("ac server sock is full !!!!!\n");
return -1;
}else{
max_fork_num++;
}
return 1;
}
int decrease_pthreads_counts()
{
if(max_fork_num > 0)
max_fork_num--;
else
printf("counter error \n");
return 0;
}
void *doit(void *arg)
{
void web_child(int);
char buff[4096];
pthread_detach(pthread_self());
//web_child((int) arg);
printf("sockfd is %d\n",(int) arg);
recv((int) arg, buff, 4096, 0);
char sockfd[10]= "aaa";
sprintf(sockfd,"%d\n",(int) arg);
write((int) arg,sockfd,sizeof(sockfd));
close((int) arg);
my_lock_wait();
decrease_pthreads_counts();
my_lock_release();
return(NULL);
}
void sig_int(int signo)
{
printf("get sigint \n");
exit(0);
}
void sig_chld(int signo)
{
pid_t pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) {
printf("sig_child pid %d terminated\n", pid);
}
return;
}
int main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
// struct sockaddr sockaddr;
char buff[1024];
time_t ticks;
const int on = 1;
pid_t childpid;
unsigned short port;
pthread_t tid;
if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("socket error \n");
return(listenfd);
}
port = 8098;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
// inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
// setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if((setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0)
{
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) < 0){
perror("bind error :");
return -1;
}
if (listen(listenfd, 30) < 0){
printf("listen error \n");
}
signal(SIGCHLD, sig_chld);
signal(SIGINT, sig_int);
my_lock_init("aa");
for ( ; ; ) {
// printf("%s ---- %d \n",__func__, __LINE__);
if ( (connfd = accept(listenfd, (SA *) NULL, NULL)) < 0) {
if (errno == EINTR){
perror("accept: ");
continue; /* back to for() */
}else{
printf("accept error");
}
}
my_lock_wait();
if(increase_pthreads_coounts() < 0){
close(connfd);
my_lock_release();
continue; /* back to for() */
}
my_lock_release();
pthread_create(&tid, NULL, &doit, (void *) connfd);
}
}