Menu Home

http

linux c 使用socket处理HTTP请求

#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/select.h> #define BUFSIZE 4096 int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; charhttpRequest[BUFSIZE], buf[BUFSIZE]; bzero(httpRequest, sizeof(httpRequest)); //构建请求信息,这里请求http://192.168.2.184:8090/test.flv,这是一个由ffserver搭建的流媒体服务 strcat(httpRequest, “GET /test.flv HTTP/1.1\n”); strcat(httpRequest, “Host: 192.168.2.184:8090\n”); strcat(httpRequest, “User-Agent: Mozilla/5.0 (X11; […]