gpt4 book ai didi

c++ - 为什么我收到协议(protocol)不支持的地址族错误?

转载 作者:行者123 更新时间:2023-11-28 06:03:35 26 4
gpt4 key购买 nike

我正在使用 UDP 协议(protocol)实现双向通信,最初我从客户端向服务器发送消息 HELO,该消息成功显示在服务器端,但是当我从服务器向客户端发送消息以回复 HELO 时,它给我错误:地址系列协议(protocol)不支持。

这是我的服务器代码:

#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: Mahnoorfatima@gmail.com";
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: mahnoorfatima22@gmail.com"<<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);

}

最佳答案

问题很可能是 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

关于c++ - 为什么我收到协议(protocol)不支持的地址族错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32817396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com