recvfrom return Invalid argument?
I have set the last two parameters to nullptr
, but this still happens
the general process is as follows:
class socketTCP
{
public:
//...
private:
int fdTCP;
int fdUDP;
};
bind
int socketTCP::Bind(const char* ip, int port)
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
int res = bind(fdTCP, (struct sockaddr*)&addr, sizeof(addr));
res = bind(fdUDP, (struct sockaddr*)&addr, sizeof(addr));
return 0;
}
udp sockfd
void socketBus::inform(const char* ip, int port, int* tmp)
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
sendto(sockfd, (void *)&buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, sizeof(addr));
close(sockfd);
}
//bindudp sockfd
//getMysockfd(1) return fdUDP
void socketBus::recvFrom(int* buf, int length)
{
int udpfd = getMysockfd(1);
int ret = recvfrom(udpfd, (void *)buf, length, 0, nullptr, nullptr);
}
however, Invalid argument
error
is thrown in this recvfrom. Why