導航:首頁 > IDC知識 > linuxchttp伺服器

linuxchttp伺服器

發布時間:2020-11-04 17:05:24

1、如何用C編寫伺服器HTTP響應頭部

響應時,已經copy有 SOCKET 句柄或者變數或者對象了

響應就是向SOCKET標識的句柄或者變數或者對象寫數據,寫字元串,按照 HTTP Header 定義,寫文本過去就是了

API 或者 相關Write函數中傳入 SOCKET 句柄 和 HTTP Header 文本

2、設計一個linux c語言,Http協議的伺服器,用socket收發消息,簡單點,如何實現。求高手解答

去看一下《Advanced Linux Programming》這本書吧,第11章講的就是怎麼用C語言實現一Http伺服器。
這里有下載地址(英文的):
http://www.advancedlinuxprogramming.com/alp-folder
英文看起來不順的話可以上網找找有沒有中文版的這本書,應該叫Linux高級編程吧~~~

3、C++ CHttpFile這個類怎麼獲取伺服器類型

提供向HTTP伺服器中請求和讀取的功能。
如果Internet會話要從一個HTTP伺服器中讀取數據,則必須內構造一個CHttpFile實例。
#include <afxinet.h>
CHttpFile類的成員容
CHttpFile::AddRequestHeaders
BOOL ADDRequestHeaders( LPCTSTR pstrHeaders, DWORD dwFlags = HTTP_ADDREQ_FLAG_ADD_IF_NEW, int dwHeadersLen = -1 );
BOOL ADDRequestHeaders( CString& str, DWORD dwFlags = HTTP_ADDREQ_FLAG_ADD_IF_NEW );

4、nginx+FastCGI+C/C++的搭建http伺服器的問題

建議:
1、使用短連接,nginx+fastcgi不太適合長連接應用
2、如果使用長連接,請設置並調整nginx.conf中fastcgi相關的參數,比如buffer之類

5、設計一個linux c語言,Http協議的伺服器,用socket收發消息,簡單點,求代碼and注釋。

OK
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>

int main(int argc,char *argv[])
{
int sockfd,new_socket;
int sock_value;
char buf[] = "hello! China!I Love You\n";

struct sockaddr_in client_;
struct sockaddr_in server_;

int SIZE = sizeof(struct sockaddr_in);

if(argc != 2){
fprintf(stderr,"The two number!\n");
exit(1);
}

if((sock_value = atoi(argv[1])) < 0){
fprintf(stderr,"socket error!\n");
exit(1);
}

if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){
perror("socket");
exit(1);
}

bzero(&server_,SIZE);

server_.sin_family = PF_INET;
server_.sin_port = htons(sock_value);
server_.sin_addr.s_addr = INADDR_ANY;

if(bind(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){
perror("bind");
exit(1);
}

if(listen(sockfd, 12) == -1){
perror("listen");
exit(1);
}

printf("Waiting ... ...\n");

while(1){
if((new_socket = accept(sockfd,(struct sockaddr *)(&client_),&SIZE)) == -1){
perror("accept");
exit(1);
}

printf("The client IP is %s\n",inet_ntoa(client_.sin_addr));
printf("The socket is %d\n",ntohs(client_.sin_port));

if(write(new_socket,buf,strlen(buf)) == -1){
perror("write");
exit(1);
}

int my;
char mybuf[1024];

if((my = read(new_socket, mybuf,1024)) == -1){
perror("read");
exit(1);
}

mybuf[my] = '\0';
printf("#++++#++++#:%s\n",mybuf);

close(new_socket);

}

close(sockfd);

return 0;
}

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
int sockfd;
int sock_value;
char buf[1024];
char mybuf[] = "Linux\n";
int read_count;

struct sockaddr_in client_;
struct sockaddr_in server_;

int SIZE = sizeof(struct sockaddr_in);

if(argc != 3){
fprintf(stderr,"The two number!\n");
exit(1);
}

if((sock_value = atoi(argv[2])) < 0){
fprintf(stderr,"socket error!\n");
exit(1);
}

if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){
perror("socket");
exit(1);
}

bzero(&client_,SIZE);
bzero(&server_,SIZE);

client_.sin_family = PF_INET;
client_.sin_port = htons(52252);
client_.sin_addr.s_addr = INADDR_ANY;

server_.sin_family = PF_INET;
server_.sin_port = htons(sock_value);
server_.sin_addr.s_addr = inet_addr(argv[1]);

if(connect(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){
perror("connect");
exit(1);
}

if((read_count = read(sockfd,buf,1024)) == -1){
perror("read");
exit(1);
}

buf[read_count] = '\0';
printf("#----#----#:%s\n",buf);

if(write(sockfd, mybuf,6) == -1){
perror("write");
exit(1);
}

close(sockfd);

exit(0);

return 0;
}

6、怎麼用C實現Http POST功能向Http伺服器上傳文件

super viewDidLoad];
webview.backgroundColor = [UIColor clearColor];
webview.scalesPageToFit =YES;
webview.delegate =self;
NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}

7、C語言連接HTTP伺服器後如何用POST交流

這個和具體的網頁有關系的,你可以用HttpWatch之類的抓包工具分析一個網頁的請求和返回。

然後就可以自己模仿相關的請求訪問該網頁了。

8、如何用c語言實現http伺服器

//服務端簡易代碼如下:
#include <stdio.h>  
#include <stdlib.h>  
  
#include <err.h>  
#include <event.h>  
#include <evhttp.h>  
  
void http_handle(struct evhttp_request *req, void *arg); /*  HTTP Request Handle  */  
  
int main(){  
    struct evhttp *httpd;  
    event_init();  
    httpd = evhttp_start("0.0.0.0", 2345);  
    if (httpd == NULL) {  
        fprintf(stderr, "Error: Unable to listen on %s:%d ");  
        exit(1);      
    }     
    evhttp_set_timeout(httpd, 2000);  
    evhttp_set_gencb(httpd, http_handle, NULL);  
    event_dispatch();  
    evhttp_free(httpd);  
  
    return 0;  
}  
  
void http_handle(struct evhttp_request *req, void *arg){  
    struct evbuffer *buf;  
    buf = evbuffer_new();  
  
    /*  Response the client  */  
    evhttp_send_reply(req, HTTP_OK, "OK", buf);  
  
    //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED");  
  
    /*  Release the memory  */  
    evbuffer_free(buf);  
    fprintf(stderr,"Send  ");  
}

編譯:編譯時把libevent的類庫中的.so文件和.h文件連接 進來。

9、怎麼用C實現Http POST功能向Http伺服器上傳文件

文件可以使用sendfile直接過去
比如剛開始是報文頭部結束\r\n\r\n直接write就可以
然後文件數據可以直接sendfile處理,
也可以
#define BUFSIZE 8196
while(read(fd,buf,BUFSIZE)>0){
write(...);
}
CONTENT-LENGTH是報文頭結束\r\n\r\n之後的位元組總數

與linuxchttp伺服器相關的知識