免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3186 | 回复: 0
打印 上一主题 下一主题

pthread_create(&pid, NULL, &handle_roution, NULL)更 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-01-06 17:50 |只看该作者 |倒序浏览
客户端与服务器建立了socket通信.当第一个客户端与服务器连接上后,便执行一些处理.此时,如果其它客户端要与服务器端连接、执行处理请求时,要在客户端显示服务器对第一个客户的处理还没完成.
  也就是说一次只处理一个客户的请求,但要通知其它客户服务器正在处理前一个客户的请求.

  编译都通过了,为什么执行到pthread_create(&pid, NULL, &handle_roution, NULL)这句时好象停住了,没有走进handle_roution(void *p)内部,我在handle_roution(void *p)中的printf一个都没有显示.



#include <stdio.h>;
#include <stdlib.h>;
#include <errno.h>;
#include <string.h>;
#include <sys/types.h>;
#include <netinet/in.h>;
#include <sys/socket.h>;
#include <sys/wait.h>;
#include <sys/pthread.h>;


#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]=%s\\n\",argv[1]);
        printf(\"argv[2]=%s\\n\",argv[2]);
        printf(\"argv[3]=%s\\n\",argv[3]);
        printf(\"argc=%d\\n\",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),;

        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 busying\\n\";
                        send(new_sock[0], \"I\'m busying!\", strlen(\"I\'m busying!\", 0);
                        close(new_sock);
                } else {
                        printf(\"@@@@@@@@  else\\n\";
                        new_sock[1] = new_sock[0];
                        flag = 1;
                        printf(\"@@@@@@@@  else 2\\n\";
                        pthread_create(&pid, NULL, &handle_roution, NULL);
                }
        }
}


        void * handle_roution(void *p) {
                printf(\"@@@@@@@@  in function 1\\n\";

                int numbytes;
                int new_fd = new_sock[1];
                char buf[MAXBUFLEN];

                if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1) {
                        printf(\"recv error\\n\";
                        perror(\"recv\");
                        close(new_sock[1]);
                        return NULL;
                }
                //printf(\"@@@@@@@@  in function 2\\n\");

                //_sleep(10);
                buf[numbytes] = \'\\0\';

                chshellname = \"liyuntest.sh\";
                doshell();
                //printf(\"@@@@@@@@  in function 3\\n\");

                if (send(new_fd,\"Hello,world!\\n\",14,0) == -1) {
                        //printf(\"@@@@@@@@  in function 4\\n\");
                        perror(\"send\");
                }
                //printf(\"@@@@@@@@  in function 5\\n\");

                close(new_sock[1]);
                flag = 0;
                return(NULL);
               
}

int check(char *s1, char *s2) {
        //printf(\"s1=%s\\n\",s1);
        //printf(\"s2=%s\\n\",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 2\\n\");
        if( (pp = popen(\"./liyuntest.sh\", \"r\")) == NULL ) {
                printf(\"popen() error!\\n\");
                exit(1);
        }
        //printf(\"send ok 3\\n\");
        while(fgets(buf, sizeof buf, pp)) {
                //printf(\"the string is:%s\", buf);
                //printf(\"the string is end\\n\");
        }
        pclose(pp);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP