2015-09-28 53 views
0

我正在實現使用UDP協議的雙向通信,intitially我發送一條消息HELO從客戶端到服務器,它成功顯示在服務器端,但是當我從服務器發送消息到客戶端的HELO回覆,所以它給我錯誤:地址家庭沒有由協議支持。爲什麼我得到不支持協議的地址系列錯誤?

這裏是我的服務器代碼:

#include <iostream> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <stdio.h> 



using namespace std; 
int main(void) 
    { 
    char write[100]; 
    int MAXBUFLEN=100; 
    char* buf; 
    char msg[100]; 
    char swp; 
    int l,x,y; 

    int conn_sock,n,err; 
    struct sockaddr_in server_addr,client_addr; 

    conn_sock=socket(AF_INET,SOCK_DGRAM,0); 
    if(conn_sock == -1) 
    { 
    perror("\n\nError in making socket and error is"); 

    cout<<"Error No:\t\n"<<errno; 
    exit(0); 
    } 

    server_addr.sin_family= AF_INET; 
    server_addr.sin_port = 1234; 
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 

    err=bind(conn_sock,(struct sockaddr *)&server_addr,sizeof(server_addr)); // binding... 

    if(err == -1) 
    { 

    perror("\n\nError in binding and error is:"); 
    cout<<"Error No:\t\n"<<errno; 
    exit(0); 
    } 
    int s=sizeof(client_addr); 

    n=recvfrom(conn_sock,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,(socklen_t*)s); // reciving HELO from client.. 


    cout<<msg<<endl; 

    cout<<"The messgae hasbeen recieved from client now enter a reply for HELO msg:"<<endl; 
    cin>> write; 

    // sending reply to client 

    int m=sendto(conn_sock,write,strlen(write),0,(sockaddr *)&client_addr,s); // sending reply to client on reply of helo... 
    if (m== -1){ 
    perror("talker: sendto"); 
    } 
    // now recieve mail fromm... 

    recvfrom(conn_sock,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,(socklen_t*)s); 

// sending rcpto client 
    sendto(conn_sock,write,strlen(write),0,(sockaddr *)&client_addr,s); 
    recvfrom(conn_sock,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,(socklen_t*)s); 
    cout<<"Recpt to: Nu.edu.pk"<<endl; 
    exit(0); 

} 

這是我的客戶端代碼:

#include <iostream> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <netdb.h> 

#define SERVERPORT "4950" // the port users will be connecting to 
#define MAXBUFLEN 100 
using namespace std; 

int main(int argc, char*argv[]) 

{ 

    // declarations 
    char msg3[]="DATA"; 
    int sockfd; 
     struct addrinfo hints, *servinfo, *p; 
     int rv; 
     int numbytes; 
     char buf[MAXBUFLEN]; 
     socklen_t addr_len; 
     struct sockaddr_storage their_addr; 
    char* bigarray; 
    char msg[]="HELO"; 
    char msg2[]="Mail from: [email protected]"; 
    int i=0; 

    char * adress; 
    char * subject; 
    char * name; 
    const char *delim="@"; 
    // getting commandline args into arrays. 
    adress=argv[i+1]; 
    char* host=strtok(adress,delim); 
    subject=argv[i+2]; 
    name=argv[i+3]; 


    // putting all in one array 

    bigarray=adress; 
    bigarray=subject; 
    bigarray=name; 




    if(argc>9){ 
    cout << "Just provide three arguments in commandline,please. " << endl; 
     } 

    // gets the name of the host: 

    int a=gethostname(bigarray, 100); 

    cout<<"The host of the client is:"<<a<<endl; 


    int conn_sock,n,m,err; 
    struct sockaddr_in server_addr; 
    conn_sock=socket(AF_INET,SOCK_DGRAM,0); 

    if(conn_sock ==-1) 
    { 
    perror("\n\nError in making socket and error is"); 

    cout<<"Error No:\t\n"<<errno; 
    exit(0); 
    } 


    server_addr.sin_family= AF_INET; 
    server_addr.sin_port = 1234; 
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 


    int pp=sizeof(server_addr); 
    n=sendto(conn_sock,msg,strlen(msg),0,(sockaddr *)&server_addr,pp);//sending HELO to server... 

    n=recvfrom(conn_sock,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,(socklen_t*)pp); 
    cout<<"Mail from: [email protected]"<<endl; 
    n=sendto(conn_sock,msg2,strlen(msg2),0,(sockaddr *)&server_addr,pp); 


     // sending file to server 

    // reieving from server rcpto command 

    recvfrom(conn_sock,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,(socklen_t*)pp); 

    // sending data command to the server.... 

    n=sendto(conn_sock,msg3,strlen(msg3),0,(sockaddr *)&server_addr,pp); 
     //Sending the filename to server... 
     if ((n = sendto(sockfd,name,strlen(name), 0,p->ai_addr, p->ai_addrlen)) == -1) { 
     //  perror("Error is sending"); 
       exit(1); 
               }    
     //  Get the size of the file server sy 

    addr_len = sizeof their_addr; 
     if ((n = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,(struct sockaddr *)&their_addr, &addr_len)) == -1) { 
     //  perror("Error in recieving file"); 
     exit(1); 
                 } 


     cout<<"client: recieved file size: %s\nNumbytes:%d\n"<<buf<<numbytes; 





    exit(0); 

    } 
+1

檢查您的'recvfrom'調用,並將它們與[手冊頁](http://man7.org/linux/man-pages/man2/recvfrom.2.html)進行比較。你實際上隱藏了一個非常嚴重的*未定義行爲*的情況。使用強制關閉編譯器警告實際上是解決警告的錯誤方法。 –

+0

它給了我警告只是這不是問題... –

+0

cout <<「從客戶端收到messgae現在輸入回覆HELO msg:」<< endl; cin >> write; //發送回覆給客戶端 int m = sendto(conn_sock,write,strlen(write),0,(sockaddr *)&client_addr,s); //如果(m == -1)perror(「talker:sendto」); }我這messgae不從服務器到客戶端做你知道爲什麼嗎?它給了我錯誤。請幫我 –

回答

1

的問題是最有可能的是recvfrom函數需要指針插座上的地址結構尺寸,而你提供的價值長度。這意味着編譯器認爲您設置的大小(sizeof(client_addr))被解釋爲一個指針,無論結構大小如何,它都不是有效的指針或指向遠程有效的東西。

這意味着recvfrom可能不會完全填充對等地址結構(client_addr),從而導致您的sendto發生故障。

嘗試例如取而代之的是:

n=recvfrom(conn_sock,msg,sizeof(msg),0, 
      (struct sockaddr *)&client_addr,&s); 
//          ^
//          | 
//   Note address-of operator here 
+0

它給了我錯誤:ssize_t recvfrom(int sockfd,void * buf,size_t len,int flags, struct sockaddr * src_addr,socklen_t * addrlen); –

+0

@MahNoor使用正確的類型開頭:'socklen_t s = sizeof(client_addr);'。 – molbdnilo

+0

我得到了解決,但我的消息從服務器發送到客戶端不顯示在服務器端客戶端:cout <<「從客戶端收到messgae現在輸入回覆HELO msg:」<< endl; \t cin >> write; (sendto(conn_sock,write,strlen(write),0,(sockaddr *)&client_addr,sizeof(client_addr))== -1){0128} –

相關問題