首先检查参数并创建一个可以连接到服务器的套接字,并定义了一个我可以编写HTTP请求的文件,当有数据时,我需要通过getline处理我们读过的行在此之后我应该检查流的结尾,在流结束时我需要关闭流并退出。但我不知道如何检查Endofline ......以及我写的格式是否正确
int main(int argc, char *argv[])
{
if(argc<2) //checking arguments
{
printf("expected a name for resolving");
exit(1);
}
int sock = createSocket(argv[1], argv[2]);
FILE *stream = getStream(sock);
printf("\n###### CLIENT IS SENDING THE FOLLOWING TO THE SERVER\n");
fputs("GET /HTTP/1.0\n Host:Google.com\n Connection:close \r\n\r\n",stream);
while(1)
{
char *string = NULL;
int len = getLine(&string, stream);
int numbytes=0;
printf("The length of string: %d\n",len);
printf("#####CLIENT RECEIVED THE FOLLOWING FROM SERVER \n%s", string);
while (fgetc(stream) != EOF)
++numbytes;
fclose(stream);
printf("##### Connection closed by server");
exit(EXIT_SUCCESS);
free(string);
}
}
答案 0 :(得分:-1)
我怀疑这是否会起作用...读取和写入套接字是通过send和recv命令执行的。
http客户端的示例代码保存在此处:
http://coding.debuntu.org/c-linux-socket-programming-tcp-simple-http-client