① 如何用c語言實現http伺服器
//服務端簡易代碼如下:
#include<stdio.h>
#include<stdlib.h>
#include<err.h>
#include<event.h>
#include<evhttp.h>
voidhttp_handle(structevhttp_request*req,void*arg);/*HTTPRequestHandle*/
intmain(){
structevhttp*httpd;
event_init();
httpd=evhttp_start("0.0.0.0",2345);
if(httpd==NULL){
fprintf(stderr,"Error:Unabletolistenon%s:%d ");
exit(1);
}
evhttp_set_timeout(httpd,2000);
evhttp_set_gencb(httpd,http_handle,NULL);
event_dispatch();
evhttp_free(httpd);
return0;
}
voidhttp_handle(structevhttp_request*req,void*arg){
structevbuffer*buf;
buf=evbuffer_new();
/*Responsetheclient*/
evhttp_send_reply(req,HTTP_OK,"OK",buf);
//evbuffer_add_printf(buf,"%s","HTTPSQS_AUTH_FAILED");
/*Releasethememory*/
evbuffer_free(buf);
fprintf(stderr,"Send ");
}
編譯:編譯時把libevent的類庫中的.so文件和.h文件連接進來。
② linux下的靜態庫和動態庫
linux下的靜態庫和動態庫1.製作自己的動態庫和靜態庫linux下動態庫以.so結尾,靜態庫以.a結尾,它們都以lib開頭,比如一個庫名為net,那麼它的全名應該是libnet.so或者libnet.a。我們有兩個文件,hello.c和test.c,下面是兩個文件的內容//hello.c
www.shiwu.com
#include
<stdio.h>void
my_lib_func(){printf(Library
routine
called/r/n);}//test.c#include
<stdio.h>
www.shiwu.com
int
main(){my_lib_func();return
1;}test.c調用了hello.c的方法,我們把hello.c封裝成庫文件。無論是靜態庫還是動態庫,都是由.o文件組成,我們先把gcc
-c
hello.c生成.o文件製作靜態庫ar
crv
libmyhello.a
hello.o,ar是生成靜態庫的命令,libmyhello.a是我的靜態庫名。下一步就是在我的程序中使用靜態庫
可以看到已經有了Library
routine
called的結果,說明調用成功了。下面我們刪除libmyhello.a,看看程序是否還是運行正常
我們發現程序依然運行正常,說明靜態庫已經連接進入我們的程序中製作動態庫
www.shiwu.com
我們看見動態庫libmyhello.so已經生成,下面繼續使用
找不到庫文件,這個時候我們把so文件拷貝到/usr/lib下面
運行成功2.動態庫和靜態庫同時存在的調用規則我們可以發現,不論是動態庫還是靜態庫,程序編譯連接的時候都是加的參數-l,那麼當他們同時存在的時候,程序會選擇動態庫還是靜態庫呢。我們做個嘗試。
我們同時存在libmyhello.a和libmyhello.so,我們發現運行的時候,出現找不到動態庫的錯誤,由此,我們可以得出結論,同時存在動態庫和靜態庫的時候,gcc會優先選擇動態庫作者
梨樹陽光
③ linux c++中要如何調用一個http介面
可以使用libcurl 庫
https://curl.haxx.se/libcurl/
#include<stdio.h>
#include<curl/curl.h>
intmain(void)
{
CURL*curl;
CURLcoderes;
curl=curl_easy_init();
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,"curl.haxx.se");
res=curl_easy_perform(curl);
/*alwayscleanup*/
curl_easy_cleanup(curl);
}
return0;
}
更多的例子在這里https://curl.haxx.se/libcurl/c/example.html
④ C/C++ 怎麼獲取網頁內容
解析不到的,網頁內容是在伺服器已經生成後再傳送到客戶端,瀏覽器只是將接收到的內容顯示出來而已
⑤ 在LINUX下如何利用C語言實現HTTP的get和post方法
下載wget的源碼看看就知道了
⑥ 大神們,常用的linux c/c++ http開源庫有哪些,給個推薦吧
客戶端庫有libcurl
服務端庫有基於libevent的libevhtp
⑦ linux C 下想把pcap文件中的http數據包里的gzip壓縮的內容解出來!有沒有人做過!或是有什麼辦法嗎
兄弟,找出解決辦法,告訴我一聲,我最近也在找相關的東西,頭都大了。。。。
⑧ 設計一個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;
}
⑨ 有沒有用C或c++寫的web伺服器
cpp-net lib cpp-netlib: The C++ Network Library,號稱是要進入標準的,但是感覺還不stable;
facebook做了一個HTTP庫 facebook/proxygen · GitHub,只對Linux系統比較版友好;
另外還有權一個叫pion的HTTP庫 splunk/pion · GitHub
C/C++好的網路庫有很多,像asio, libevent, libuv等的性能都是極好的,可以在這個基礎上加上HTTP協議解析,比如用joyent的http_parser,然後就是處理HTTP協議本身了,但這個時候問題就來了,是支持到1.1還是2.0?要不要支持SPDY、WebSocket?
沒有GC的語言處理字元串是很虐心的,如果一定要強求用C++,那我只能安慰題主:node也是C++寫的,你就當node的框架是C++ Web伺服器咯~
更好的選擇是用nginx,靠譜,實用。
⑩ linux下C語言怎麼讀取http文件內容
http是協議
不是文件
你這個說法就有問題了。
如果你想用C讀網頁 可以考慮使用socket 不過還是有些麻煩的。