Recvfrom Error Invalid Argument
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up recvfrom returns invalid argument when *from* is passed up vote 6 down vote favorite I am currently writing a small UDP server program in linux. The UDP server will receive packets from two different peers and will perform different operations based on from which peer it received the packet. I am trying to determine the source from where I receive the packet. However, when select returns and recvfrom is called, it returns with an error of Invalid Argument. If I pass NULL as the second last arguments, recvfrom succeeds. I have tried declaring fromAddr as struct sockaddr_storage, struct sockaddr_in, struct sockaddr without any success. Is their something wrong with this code? Is this the correct way to determine the source of the packet? The code snippet follows. ` /*TODO : update for TCP. use recv */ if((pkInfo->rcvLen=recvfrom(psInfo->sockFd, pkInfo->buffer, MAX_PKTSZ, 0, /* (struct sockaddr*)&fromAddr,*/ NULL, &(addrLen) )) < 0) { perror("RecvFrom failed\n"); } else { /*Apply Filter */ #if 0 struct sockaddr_in* tmpAddr; tmpAddr = (struct sockaddr_in* )&fromAddr; printf("Received Msg From %s\n",inet_ntoa(tmpAddr->sin_addr)); #endif printf("Packet Received of len = %d\n",pkInfo->rcvLen); } ` linux sockets share|improve this question asked Jun 8 '10 at 17:19 Aditya Sehgal 1,8471729 3 Are you setting addrLen to the byte size of fromAddr before calling recvfrom()? It needs to know how many bytes are available for it to fill in fromAddr with peer info. On output, it will return the number of bytes filled in. –Remy Lebeau Jun 8 '10 at 22:57 @Remy: Yes that was it. –Aditya Sehgal Jun 9 '10 at 4:34 add a comment| 2 Answers 2 active oldest votes up vote 17 down vote accepted You must initialise addrLen to sizeof fromAddr before calling recvfrom() - it is an input/output parameter. Using struct sockaddr_storage to declare fromAddr is correct. share|improve this answer answered Jun 9 '10
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http://stackoverflow.com/questions/2999639/recvfrom-returns-invalid-argument-when-from-is-passed Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up UDP sendto() error: invalid argument up vote 0 down vote favorite We are trying to apply a UDP based http://stackoverflow.com/questions/30941151/udp-sendto-error-invalid-argument protocol and having some problem with sendto() function. when we try to response to the write-request with ack we get "invalid argument" from the sendto() function this is our code: int sock; // Socket sockaddr_in_t echoServAddr; // Local address sockaddr_in_t echoClntAddr; // Client address unsigned int cliAddrLen; // Length of incoming message data_packet_t echoBuffer; wrq_packet_t wrqBuffer; unsigned short echoServPort; // Server port int recvMsgSize; // Size of received message ack_packet_t Ack; struct timeval timeout; fd_set fds; /* Create socket for sending/receiving datagrams */ if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) perror("TTFTPERROR: socket() failed"); /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); echoServAddr.sin_family = AF_INET; echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); echoServAddr.sin_port = htons(echoServPort); /*Bind to the local address */ if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) perror("TTFTPERROR: bind() failed"); FD_ZERO(&fds); FD_SET(sock, &fds); timeout.tv_sec = WAIT_FOR_PACKET_TIMEOUT; timeout.tv_usec = 0; while (1) { recvMsgSi
*buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); Description The recvfrom() and recvmsg() calls are used to receive messages https://linux.die.net/man/2/recvfrom from a socket, and may be used to receive data on a socket whether or not it is connection-oriented. If src_addr is not NULL, and the underlying protocol provides the source http://developerweb.net/viewtopic.php?id=6172 address, this source address is filled in. When src_addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL. The argument addrlen is recvfrom error a value-result argument, which the caller should initialize before the call to the size of the buffer associated with src_addr, and modified on return to indicate the actual size of the source address. The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call. The recv() call recvfrom error invalid is normally used only on a connected socket (see connect(2)) and is identical to recvfrom() with a NULL src_addr argument. All three routines return the length of the message on successful completion. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from. If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested. The select(2) or poll(2) call may be used to determine when more data arrives. The flags argument to a recv() call is formed by ORing one or more of the following values: MSG_CMSG_CLOEXEC (recvmsg() only; since Linux 2.6.23) Set the close-on-exec flag for the file descriptor received via a UNIX domain file descriptor using the SCM_RIGHTS operation (described in
#1 2009-10-19 07:21 AM frank2004 Member From: Beijing Registered: 2004-07-20 Posts: 93 Re: select returned error: invalid argument in the following code, select sometime returned error, anyone can point some bug? thanks(some variables are defined by myself): for (loop=0; loop