C で基本的なプロキシ サーバーを作成するという最後の大きなハードルに直面しています。
サーバーがブラウザからリクエストを正常に受信し、ホストに正常に送信しました。そして、ホストからの応答を正常に受信しています! ただし、サーバー経由で Google に接続しようとすると、次のようになります。
Rcvd message from server:
----
HTTP/1.1 200 OK
Date: Thu, 15 Mar 2012 20:35:11 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=83a7c2e6675a9a9f:FF=0:TM=1331843711:LM=1331843711:S=7I7RIVV1B-HxhWJR; expires=Sat, 15-Mar-2014 20:35:11 GMT; path=/; domain=.google.com
Set-Cookie: NID=57=KvqnXtYNkJZBryXL5zzhG5eH8Or2_PDWDqT_kU35PvOro_mAFiLiTSjPHOnWWxxm3R0vKYnzEeVkAPFKK366lZiNZGpjhO2-II5OeZQnWe09H-jZdePsrN-SnBdQ2ENT; expires=Fri, 14-Sep-2012 20:35:11 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
1000
<!doctype html><html itemscope itemtype="http://schema.org/WebPage"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="description" content="Search the world's information, including webpages, images, videos and more. Goo
切れるのがわかりますか?「Transfer-Encoding: chunked」のためです。サーバーが残りのチャンクを送信し続けるようにするにはどうすればよいでしょうか? その最初のものの後、それは止まるからです。read() を while ループの中に入れて、何か読み取るものがある限り、読み取りとクライアントへの転送を続行する必要がありますか?
編集:
さて、これが私の現在のコードです。これにより、サーバーの応答が最初に 1 回読み取られ ("sock" という名前のソケットで)、それがクライアントに書き込まれ ("newsock")、while ループに入り、さらにサーバーの応答を読み取ってクライアントに送信し続けます。この形式のコードはまだテストしていません。エラーチェックのギャップ以外に明らかな問題はありますか?
/*WRITING SERVER RESPONSE TO CLIENT*/
char buffer2[1024];
n = read(sock, buffer2, 1024 );
if ( n < 1 )
{
perror( "read() failed" );
return EXIT_FAILURE;
}
else
{
buffer2[n] = '\0';
printf( "Rcvd message from server: \n\n----\n\n%s\n\n----\n\n", buffer2 );
}
n = write( newsock, buffer2, strlen( buffer2 ) );
while((n = read(sock, buffer2, 1024 )) >= 1)
{
buffer2[n] = '\0';
printf( "Rcvd message from server: \n\n----\n\n%s\n\n----\n\n", buffer2 );
n = write( newsock, buffer2, strlen( buffer2 ) );
}