当前位置: 技术问答>linux和unix
SOCKET多线程编程问题,大家帮忙看看
来源: 互联网 发布时间:2015-08-02
本文导语: //server.cpp #include "server.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int curThreadid; static int connnum=0; void * threadList[12000...
//server.cpp
#include "server.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int curThreadid;
static int connnum=0;
void * threadList[12000];
int sock[10240];
void InitSock(void)
{
int trycount=0;
memset(sock,0,sizeof(sock));
memset(threadList,0,sizeof(threadList));
int sockfd;
struct sockaddr_in dest;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
int opt = 1;
int len11 = sizeof(opt);
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(1054);
dest.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(dest.sin_zero),8);
bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));
listen(sockfd, 10240);
int clientfd;
struct sockaddr_in client_addr;
int addrlen = sizeof(client_addr);
char Buf[120];
int ret;
pthread_t id;
socklen_t mylen;
int Count=0;
while(true){
Count=Count+1;
printf("wait for client...rn");
try{
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen);
if(connnum>=10240)
connnum=0;
sock[connnum]=clientfd;
connnum++;
printf("client fd is %d.rn",clientfd);
}
catch(...){
printf("accept error,continue.");
close(sockfd);
continue;
}
printf("client is connect,socket id:%d.rn",clientfd);
try{
while(1)
{
ret=pthread_create(&id,NULL,RunService,((void *)&clientfd));
if(ret!=0){
printf("create client thread error,return value is not 0.rn");
try
{
sprintf(Buf,"error.rn");
break;
}
catch(...){;}
continue;
}
else
break;
}
curThreadid=id;
}
catch(...){
printf("create client thread error,unknow error,continue.rn");
continue;
}
printf("process client thread id is %d.rn",id);
threadList[connnum]=&id;
usleep(50);
printf("count : %d .rn",Count);
}
close(sockfd);
}
void *RunService(void *Psock_fd)
{
int Index=connnum;
int ThreadID=curThreadid;
int sock_fd;
sock_fd=sock[connnum-1];
char Buf[120];
int Count=0;
char cmd=0;
bool Isloop=true;
printf("proc thread id is %d.rn",ThreadID);
while(Isloop){
usleep(50);
cmd=0;
Count++;
memset(Buf,0,sizeof(Buf));
try
{
read(sock_fd,&cmd,1);
}
catch(...)
{
printf("rec error.rn");
}
switch(cmd){
case 1:
sprintf(Buf,"server is read %drn",Count);
try{
printf("case 1,command is:=%crn",cmd);
write(sock_fd,Buf,120);
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
case 110:
sprintf(Buf,"socket id is :%drn",sock_fd);
try{
printf("case 110,command is:=%crn",cmd);
write(sock_fd,Buf,120);
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
case 254:
sprintf(Buf,"rec quit command,quit thread :%d .rn",ThreadID);
try{
printf("case 254,command is:=%crn",cmd);
write(sock_fd,Buf,120);
Isloop=false;
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
}
}
printf("quiting thread id:%d.rn",ThreadID);
try
{
close(sock_fd);
}
catch(...)
{
printf("close socket error.rn");
}
printf("wait 2s.");
usleep(2000);
printf("thread id:%d id down.rn",ThreadID);
threadList[Index]=NULL;
pthread_exit(NULL);
return NULL;
}
int main()
{
InitSock();
printf("main thread exit.");
return 0;
}
#include "server.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int curThreadid;
static int connnum=0;
void * threadList[12000];
int sock[10240];
void InitSock(void)
{
int trycount=0;
memset(sock,0,sizeof(sock));
memset(threadList,0,sizeof(threadList));
int sockfd;
struct sockaddr_in dest;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
int opt = 1;
int len11 = sizeof(opt);
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(1054);
dest.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(dest.sin_zero),8);
bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));
listen(sockfd, 10240);
int clientfd;
struct sockaddr_in client_addr;
int addrlen = sizeof(client_addr);
char Buf[120];
int ret;
pthread_t id;
socklen_t mylen;
int Count=0;
while(true){
Count=Count+1;
printf("wait for client...rn");
try{
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen);
if(connnum>=10240)
connnum=0;
sock[connnum]=clientfd;
connnum++;
printf("client fd is %d.rn",clientfd);
}
catch(...){
printf("accept error,continue.");
close(sockfd);
continue;
}
printf("client is connect,socket id:%d.rn",clientfd);
try{
while(1)
{
ret=pthread_create(&id,NULL,RunService,((void *)&clientfd));
if(ret!=0){
printf("create client thread error,return value is not 0.rn");
try
{
sprintf(Buf,"error.rn");
break;
}
catch(...){;}
continue;
}
else
break;
}
curThreadid=id;
}
catch(...){
printf("create client thread error,unknow error,continue.rn");
continue;
}
printf("process client thread id is %d.rn",id);
threadList[connnum]=&id;
usleep(50);
printf("count : %d .rn",Count);
}
close(sockfd);
}
void *RunService(void *Psock_fd)
{
int Index=connnum;
int ThreadID=curThreadid;
int sock_fd;
sock_fd=sock[connnum-1];
char Buf[120];
int Count=0;
char cmd=0;
bool Isloop=true;
printf("proc thread id is %d.rn",ThreadID);
while(Isloop){
usleep(50);
cmd=0;
Count++;
memset(Buf,0,sizeof(Buf));
try
{
read(sock_fd,&cmd,1);
}
catch(...)
{
printf("rec error.rn");
}
switch(cmd){
case 1:
sprintf(Buf,"server is read %drn",Count);
try{
printf("case 1,command is:=%crn",cmd);
write(sock_fd,Buf,120);
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
case 110:
sprintf(Buf,"socket id is :%drn",sock_fd);
try{
printf("case 110,command is:=%crn",cmd);
write(sock_fd,Buf,120);
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
case 254:
sprintf(Buf,"rec quit command,quit thread :%d .rn",ThreadID);
try{
printf("case 254,command is:=%crn",cmd);
write(sock_fd,Buf,120);
Isloop=false;
}
catch(...){
printf("send data is error,exit proc thread id %d.rn",ThreadID);
Isloop=false;
}
break;
}
}
printf("quiting thread id:%d.rn",ThreadID);
try
{
close(sock_fd);
}
catch(...)
{
printf("close socket error.rn");
}
printf("wait 2s.");
usleep(2000);
printf("thread id:%d id down.rn",ThreadID);
threadList[Index]=NULL;
pthread_exit(NULL);
return NULL;
}
int main()
{
InitSock();
printf("main thread exit.");
return 0;
}
|
让thread detach
调用pthread_detach()
调用pthread_detach()
|
创造的线程没有释放,楼上说得对,用pthread_detach可以使线程变成可脱离的,线程终止后资源自动释放,不过一般要用pthread_join来等待线程结束,以确保程序运行的准确和稳定。