国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php - socket連接被重置是什么原因 ?
某草草
某草草 2017-05-16 13:00:37
0
1
639

自己寫http服務(wù)器, 服務(wù)端監(jiān)聽套接字, handle_request線程處理瀏覽器php動態(tài)請求.

    ...
    
    while(1){
        if(-1 == (client_fd = accept(sockfd, (struct sockaddr *) &client_sock, &sin_size))) err_exit("accept");
        if(pthread_create(&ntid, NULL, (void *)handle_request, &client_fd) != 0) err_exit("pthread_create");
    }
    close(sockfd);
    return 0;

在handle_request中與php-fpm通信之后, 獲取了執(zhí)行結(jié)果msg,  msg包含了兩行http響應(yīng)頭信息, 空行 以及響應(yīng)主體(php代碼執(zhí)行后的結(jié)果), 然后我只要添上一個響應(yīng)行, 就構(gòu)造了http響應(yīng)數(shù)據(jù)包, 最后發(fā)給客戶端.

    ...
    
    /* 發(fā)送響應(yīng) */
    sprintf(header, "%s 200 OK\r\n", hr->version);      
    
    //printf("%s%s\n", header, msg);

    send(client_fd, header, strlen(header), 0);
    send(client_fd, msg, contentLength, 0);

    free(msg);
    close(client_fd);

奇怪的是我在瀏覽器中訪問, php執(zhí)行結(jié)果一閃而過, 然后提示連接被重置

Firefox can’t establish a connection to the server at 127.0.0.1:8899.

在telnet測試, 能收到完整的http響應(yīng)信息

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
GET /index.php HTTP/1.1     
HTTP/1.1 200 OK
X-Powered-By: PHP/5.5.9-1ubuntu4.21
Content-type: text/html

hello worldConnection closed by foreign host.

php 程序

<?php
    echo "hello world";
某草草
某草草

全部回復(fù)(1)
世界只因有你

你這個設(shè)計(jì)問題很嚴(yán)重,你將&client_fd傳到pthread_create很可能會引起連接丟失,因?yàn)槟銦o法保證handle_request在下一個accpet成功之前一定先執(zhí)行,還有一個問題就是,你的這個設(shè)計(jì)很爛,不說別的,最起碼得整個thread pool吧……

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板