当前位置: 技术问答>linux和unix
线程控制,但是pthread_create(&pid, NULL, &handle_roution, NULL)更本没走进去????????????
来源: 互联网 发布时间:2015-05-31
本文导语: 客户端与服务器建立了socket通信.当第一个客户端与服务器连接上后,便执行一些处理.此时,如果其它客户端要与服务器端连接、执行处理请求时,要在客户端显示服务器对第一个客户的处理还没完成. 也就是说一次只...
客户端与服务器建立了socket通信.当第一个客户端与服务器连接上后,便执行一些处理.此时,如果其它客户端要与服务器端连接、执行处理请求时,要在客户端显示服务器对第一个客户的处理还没完成.
也就是说一次只处理一个客户的请求,但要通知其它客户服务器正在处理前一个客户的请求.
我产生了线程来控制,但是pthread_create(&pid, NULL, &handle_roution, NULL)更本没走进去.我是用printf来跟踪的.
帮我看看??????
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490
#define PARAMATERNUM 4
#define BACKLOG 10
#define MAXBUFLEN 100
char *chshellname;
int check(char *s1, char *s2);
void doshell();
void * handle_roution(void *p);
int connectcount = 0;
int flag = 0;
pthread_t pid;
int new_sock[2];
int main(int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in their_addr;
int sin_size;
char buf[MAXBUFLEN];
int addr_len;
printf("argv[1]=%sn",argv[1]);
printf("argv[2]=%sn",argv[2]);
printf("argv[3]=%sn",argv[3]);
printf("argc=%dn",argc);
if (argc != PARAMATERNUM) {
printf("please input the right command!n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) == -1) {
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MYPORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd,BACKLOG) == -1) {
perror("bind");
exit(1);
}
while (1)
{
new_sock[0] = accept(sockfd, 0, 0);
if (flag) {
printf("@@@@@@@@ send busyingn");
send(new_sock[0], "I'm busying!", strlen("I'm busying!"), 0);
close(new_sock);
} else {
new_sock[1] = new_sock[0];
flag = 1;
pthread_create(&pid, NULL, &handle_roution, NULL);
}
}
}
void * handle_roution(void *p) {
int numbytes;
int new_fd = new_sock[1];
char buf[MAXBUFLEN];
if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1) {
printf("recv errorn");
perror("recv");
close(new_sock[1]);
return NULL;
}
//_sleep(10);
buf[numbytes] = '';
chshellname = "liyuntest.sh";
doshell();
if (send(new_fd,"Hello,world!n",14,0) == -1) {
perror("send");
}
close(new_sock[1]);
flag = 0;
return(NULL);
}
int check(char *s1, char *s2) {
//printf("s1=%sn",s1);
//printf("s2=%sn",s2);
if (strcmp(s1, s2) == 0) {
return 0;
} else {
return -1;
}
}
void doshell(char *argShellname) {
system("chmod +x liyuntest.sh");
system("./liyuntest.sh");
char buf[128];
FILE *pp;
//printf("send ok 2n");
if( (pp = popen("./liyuntest.sh", "r")) == NULL ) {
printf("popen() error!n");
exit(1);
}
//printf("send ok 3n");
while(fgets(buf, sizeof buf, pp)) {
//printf("the string is:%s", buf);
//printf("the string is endn");
}
pclose(pp);
}
也就是说一次只处理一个客户的请求,但要通知其它客户服务器正在处理前一个客户的请求.
我产生了线程来控制,但是pthread_create(&pid, NULL, &handle_roution, NULL)更本没走进去.我是用printf来跟踪的.
帮我看看??????
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490
#define PARAMATERNUM 4
#define BACKLOG 10
#define MAXBUFLEN 100
char *chshellname;
int check(char *s1, char *s2);
void doshell();
void * handle_roution(void *p);
int connectcount = 0;
int flag = 0;
pthread_t pid;
int new_sock[2];
int main(int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in their_addr;
int sin_size;
char buf[MAXBUFLEN];
int addr_len;
printf("argv[1]=%sn",argv[1]);
printf("argv[2]=%sn",argv[2]);
printf("argv[3]=%sn",argv[3]);
printf("argc=%dn",argc);
if (argc != PARAMATERNUM) {
printf("please input the right command!n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) == -1) {
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MYPORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd,BACKLOG) == -1) {
perror("bind");
exit(1);
}
while (1)
{
new_sock[0] = accept(sockfd, 0, 0);
if (flag) {
printf("@@@@@@@@ send busyingn");
send(new_sock[0], "I'm busying!", strlen("I'm busying!"), 0);
close(new_sock);
} else {
new_sock[1] = new_sock[0];
flag = 1;
pthread_create(&pid, NULL, &handle_roution, NULL);
}
}
}
void * handle_roution(void *p) {
int numbytes;
int new_fd = new_sock[1];
char buf[MAXBUFLEN];
if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1) {
printf("recv errorn");
perror("recv");
close(new_sock[1]);
return NULL;
}
//_sleep(10);
buf[numbytes] = '';
chshellname = "liyuntest.sh";
doshell();
if (send(new_fd,"Hello,world!n",14,0) == -1) {
perror("send");
}
close(new_sock[1]);
flag = 0;
return(NULL);
}
int check(char *s1, char *s2) {
//printf("s1=%sn",s1);
//printf("s2=%sn",s2);
if (strcmp(s1, s2) == 0) {
return 0;
} else {
return -1;
}
}
void doshell(char *argShellname) {
system("chmod +x liyuntest.sh");
system("./liyuntest.sh");
char buf[128];
FILE *pp;
//printf("send ok 2n");
if( (pp = popen("./liyuntest.sh", "r")) == NULL ) {
printf("popen() error!n");
exit(1);
}
//printf("send ok 3n");
while(fgets(buf, sizeof buf, pp)) {
//printf("the string is:%s", buf);
//printf("the string is endn");
}
pclose(pp);
}
|
我assert(pthread_create(...) == 0),结果coredump
编译又没有问题,估计就出在link上面,试一下不就出来了~~
编译又没有问题,估计就出在link上面,试一下不就出来了~~
|
void * handle_roution(void *p) {
int numbytes;
int new_fd = new_sock[1];
char buf[MAXBUFLEN];
if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1) {
printf("recv errorn");
perror("recv");
close(new_sock[1]);
return NULL;
}
//_sleep(10);
buf[numbytes] = '';
chshellname = "liyuntest.sh";
doshell();
if (send(new_fd,"Hello,world!n",14,0) == -1) {
perror("send");
}
close(new_sock[1]);
flag = 0;
return(NULL);
}
假设进去了 假设都正确 哪里有打印?除非这个liyuntest.sh
你看呢?
int numbytes;
int new_fd = new_sock[1];
char buf[MAXBUFLEN];
if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1) {
printf("recv errorn");
perror("recv");
close(new_sock[1]);
return NULL;
}
//_sleep(10);
buf[numbytes] = '';
chshellname = "liyuntest.sh";
doshell();
if (send(new_fd,"Hello,world!n",14,0) == -1) {
perror("send");
}
close(new_sock[1]);
flag = 0;
return(NULL);
}
假设进去了 假设都正确 哪里有打印?除非这个liyuntest.sh
你看呢?
|
不要用printf跟踪,那是有延迟的。
建议:采用写文件实时跟踪。或者使用fprintf跟踪。
你的线程应该已经起来了,可能是中间有一步让你block或者segmentation了。
建议:采用写文件实时跟踪。或者使用fprintf跟踪。
你的线程应该已经起来了,可能是中间有一步让你block或者segmentation了。
|
在recv前加输出看是否进入了。如果进入了,就是client的问题了,没有发送数据,导致recv阻塞。
|
同学啊,不是告诉你不要用printf来输出吗?用写文件的办法!能很快定位到错误的地方。
|
pthread_create(&pid, NULL, &handle_roution, NULL);
>>
pthread_create(&pid, NULL, handle_roution, NULL);
>>
pthread_create(&pid, NULL, handle_roution, NULL);