|
|
- #include "myproxy.h"
- int
- main()
- {
- int sockfd;
- char ip[64];
- char port[16];
- struct sockaddr_in address;
- char request[256];
- char response[256];
- int ret;
- /*input the ip address to test*/
- printf("Pleae input the IP address:\n");
- scanf("%s",ip);
- printf("Pleae input the port:\n");
- scanf("%s",port);
- /*create the socket*/
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- printf("Failed in creating socket!\n");
- }
- /*connect to the server by socket*/
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = inet_addr(ip);
- address.sin_port = htons(atoi(port));
- ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
- if(ret == -1)
- {
- printf("Failed in connecting to server!\n");
- }
- /*send the http request*/
- sprintf(request,
- "%s", "GET /index.html HTTP/1.1\r\n",
- //"%s", "Host:www.google.com\r\n",
- //"%s", "Connection:close\r\n",
- //"%s", "User-agent:Mozilla/4.0\r\n",
- "%s", "\r\n");
- ret = strlen(request);
- ret = write(sockfd, request, strlen(request));
- if(ret == -1)
- {
- printf("Failed in writing data to socket!\n");
- }
- /*read the response from server*/
- /*here i donnot check the integrity of the data*/
- ret = 1;
- //while(ret == 1)
- {
- ret = read(sockfd, response, 256);
- }
- printf("%s", response);
-
- /*close the socket*/
- close(sockfd);
- }
复制代码
===========================
为什么在read那里总会滞留住呢?是不是还要对socket设置些什么参数呢? |
|