2016-07-19 82 views
0

我目前有一個套接字源。但有些事情對終端來說是錯誤的。當我寫的gcc http_client.c或使http_client我得到這個錯誤:C Socket:error:expected')'in'&'token in inet_aton();

http_client.c: In function ‘main’: 
http_client.c:24:25: error: expected ‘)’ before ‘&’ token 
int inet_aton(address, &remote_address.sin_addr.s_addr); 

In file included from http_client.c:9:0: 
/usr/include/arpa/inet.h:73:12: note: expected ‘struct in_addr *’ but 
argument is of type ‘in_addr_t * {aka unsigned int *}’ 
extern int inet_aton (const char *__cp, struct in_addr *__inp) __THROW; 

但由http_client和終端說我「分段錯誤(核心轉儲)」我曾嘗試很多方面,只是沒有。我該如何解決這個錯誤?

編輯:當我寫./http_client 216.239.38.120有一些命令:

Response from the server: HTTP/1.1 302 Found 
Cache-Control: private 
Content-Type: text/html; charset=UTF-8 
Location: http://www.google.com.tr/gfe_rd=cr&ei=z2WOV_WcHJTY8AeB85y4Cg 
Content-Length: 262 
Date: Tue, 19 Jul 2016 17:39:27 GMT 

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<TITLE>302 Moved</TITLE></HEAD><BODY> 
<H1>302 Moved</H1> 
The document has moved 
<A  HREF="http://www.google.com.tr/gfe_rd=cr&amp;ei=z2WOV_WcHJTY8AeB85y4Cg">here</A>. 
</BODY></HTML> 

我http_client資源

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <sys/types.h> 
#include <fcntl.h> // for open 
#include <unistd.h> // for close 
#include <resolv.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 

int main(int argc, char *argv[]) 
{ 
char *address; 
address = argv[1]; 

int client_socket; 
client_socket = socket(AF_INET, SOCK_STREAM, 0); 

// connect to an address 
struct sockaddr_in remote_address; 
remote_address.sin_family = AF_INET; 
remote_address.sin_port = htons(80); 
inet_aton(address, &remote_address.sin_addr.s_addr); 

connect(client_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)); 

char request[] = "GET/HTTP/1.1\r\n\r\n"; 
char response[4096]; 

send(client_socket, request, sizeof(request), 0); 
recv(client_socket, &response, sizeof(response), 0); 

printf("Response from the server: %s\n", response); 
close(client_socket); 

return 0; 
} 

原諒我的錯誤的語言。

回答

0

變化

inet_aton(address, &remote_address.sin_addr.s_addr); 

inet_aton(address, &remote_address.sin_addr); 

只有賽格故障我是當我運行應用程序沒有給出參數。

ETA - 查看inet_aton()的函數原型,它接受sockaddr_in的ppointer,而不是結構的單個成員。

#include <arpa/inet.h> 
int inet_aton(const char *cp, struct in_addr *addr); 

https://www.mkssoftware.com/docs/man3/inet_aton.3.asp

+0

我爲socket編程初學者我C.所以,這個地址,和罪惡命令讓我迷惑的任何時間。我沒有嘗試過。謝謝,它爲我工作。 –

+0

我們都必須從某個地方開始,很高興我能提供幫助。 – Nunchy

+0

我很抱歉,但是,使命令成功工作。 ./http_client 216.239.38.120正在工作。但我懷疑它,因爲當我運行這個命令時,終端必須給我expires,cache-control,server,x-xss,x-frame等命令。我錯了嗎?簡單的信息;這個客戶端使用我的http_server.c源代碼?在這個源文件中有一個char http_header [2048] =「HTTP/1.1 200 OK \ r \ n \ n」;和終端顯示來自服務器的響應:HTTP/1.1 302 Found。我非常困惑 –