2013-10-31 82 views
0

我正在嘗試將客戶端連接到服務器,因此客戶端可以向服務器發送消息。 這裏是我的代碼嘗試在連接到服務器之前編譯客戶端時出錯

服務器

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <time.h> 
#include <stdio.h> 
#include <strings.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, char **argv) 
{ 
    int connfd, listenfd, tmp, sin_size; 
    struct sockaddr_in servaddr, client; 
    char message[100]; 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = INADDR_ANY; 
    servaddr.sin_port = htons(13); 
    bzero(&servaddr, sizeof(servaddr)); 

    listenfd = socket(AF_INET,SOCK_STREAM,0); 
    if(listenfd == -1) 
    { 
    printf("Error create socket\n"); 
    exit(1); 
    } 

    tmp = bind(listenfd, (struct sockaddr*)&servaddr, sizeof(struct sockaddr)); 
    if(tmp == -1) 
    { 
    printf("Error binding\n"); 
    exit(1); 
    } 

    listen(listenfd,2); 

    sin_size = sizeof(client); 
    connfd = accept(listenfd, (struct sockaddr*)&client, &sin_size); 
    if(connfd == -1) 
    { 
    printf("Error accept\n"); 
    exit(1); 
    } 
    bzero(&message, sizeof(message)); 
    read(connfd, &message, sizeof(message)); 
    write(connfd, message, sizeof(message)); 
    close(connfd); 
    close(listenfd); 

} 

客戶

#include <stdio.h> 
#include <stdlib.h> 
#include <strings.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#include <string.h> 

int main(int argc, char **argv) 
{ 
    int connfd, listenfd; 
    int port = atoi(argv[2]); 
    char message[100]; 

    listenfd = socket(AF_INET,SOCK_STREAM,0); 
    if(listenfd == -1) 
    { 
    printf("Error create socket\n"); 
    exit(1); 
    } 
    struct sockaddr_in servaddr; 

    bzero(&servaddr, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_port = htons(port); 
    servaddr.sin_addr.s_addr = inet_addr(argv[1]); 

    connfd = accept(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)); 
    if(connfd == -1) 
    { 
    printf("Error accept\n"); 
    exit(1); 
    } 
    bzero(&message, sizeof(message)); 
    printf("Input kata: "); 
    scanf("%[^\n]",message); 
    getchar(); 
    write(listenfd, &message, sizeof(message)); 
    read(listenfd, &message, sizeof(message)); 

    close(listenfd); 
} 

我編譯和運行服務器(./server 8000),它在等待客戶端連接。 當我試圖編譯有一個錯誤,該部件

connfd =接受(listenfd,(結構sockaddr *)& servaddr, 的sizeof(servaddr));

它說

warning: passing argument 3 of ‘accept’ makes pointer from integer without a cast [enabled by default] 
/usr/include/x86_64-linux-gnu/sys/socket.h:214:12: note: expected ‘socklen_t * __restrict__’ but argument is of type ‘long unsigned int’ 

我應該怎麼做來解決它?

回答

3

作爲警告建議你應該傳遞長度參數的地址而不是實際的長度。

socklen_t szaddr = sizeof(servaddr); 
connfd = accept(listenfd, (struct sockaddr*)&servaddr, &szaddr); 
+0

謝謝你的工作。但還有另一個問題。當我試圖運行./client(IP)8000時,它說錯誤接受。爲什麼它不能接受IP和端口? – greenthunder

+2

@greenthunder,客戶端應該'connect()'而不是'accept()'。 – Rohan

+0

是的,你是對的。但它仍然錯誤連接雖然我已經將代碼更改爲{connfd = connect(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));}不使用綁定當然 – greenthunder

相關問題