【问题标题】:how to check EOF of HTTP client Using fdopen() and getline()如何使用 fdopen() 和 getline() 检查 HTTP 客户端的 EOF
【发布时间】:2013-02-13 05:24:56
【问题描述】:

首先我检查参数并创建一个可以连接到服务器的套接字并定义一个文件,我可以在其中编写我的 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);

} 


}

【问题讨论】:

  • 我不明白问题或代码。为什么你只阅读一行,然后在 EOS 之前丢弃所有其他输入?这里的目的是什么?

标签: c sockets network-programming httpclient getline


【解决方案1】:

我怀疑这是否可行...通过 send 和 recv 命令执行对套接字的读取和写入。

http 客户端的示例代码保存在这里:

http://coding.debuntu.org/c-linux-socket-programming-tcp-simple-http-client

【讨论】:

  • 它可以通过stdio 完成,如图所示,尽管这是否是一个好主意是另一个问题。你还没有真正回答这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 2018-10-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多