about recvfrom()?

I have make a app based on UDP,the server want to get some from a client,they do it by UDP.but my server always say "recvfrom: bad adddress" when I execute the recvfrom(...).
the server is only a common appliaction ,not a deamon.so,it should block itself when execute the recvfrom(),but it not.
What do I do it?
redsunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jceaCommented:
Obviously you are using an incorrectly formated socket or "from address". Check the following:

1. Create the socket using "socket call".
2. Do the Bind(). Make sure you using functions like "htonl()"
   and "htons()" to translate "endianness", especially if you
   have an Intel machine (or any other little endian CPU). This
   is a typical mistake.
3. Construct the "from" structure (for recvfrom) taking care
   of endianness, too. You can receive from any IP using
   ADDR_ANY.

Here you are.

I include a small example, using TCP:

        soc=socket(AF_INET,SOCK_STREAM,0);
        if(soc==-1) {
                perror("No se puede abrir el socket");
                exit(errno);
        }
        servidor=gethostbyname(IRCop_servidor);
        if(servidor==NULL) {
                perror("No se puede resolver el nombre del servidor");
                exit(errno);
        }
       
        conexion.sin_family=AF_INET;
        conexion.sin_addr=*(struct in_addr *)servidor->h_addr;
^^^^ Here I don't use htonl() because servidor->h_addr has
already the correct endianness
        conexion.sin_port=htons(IRCop_puerto);
^^^^ Look at this
        estado=connect(soc,(struct sockaddr *)&conexion,sizeof(conexion));
        if(estado==-1) {
                perror("Error en la conexión con el server. Reintentando...");
                close(soc);
                sleep(5);
                goto inicio;
        }


0
redsunAuthor Commented:
Hi:
   jcea.
   I do it by your idael,but it is still.here is my app:

//*******************///////

/* This is a server for chat            */
/*    1st edtion: Taiyuan 1998/02/23    */
/*    <Bill Sun>RedSun@Rocketmail.com   */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#define PORT 1234
#define HOST_ADDR "127.0.0.1"
main()
  {
     int s;         /* socket for send / receive info */
     static int width;  /* select  bit mask */
     int fromlen;
     fd_set rset;   /* for read  */    
/*   fd_set wset;   for write    */
     struct sockaddr_in sour,dest;
/*     struct sockaddr  dest; */
     static struct hostent *host;
     char *buf="111";    
     int ret=10;
     unsigned long addr;
     
    s=socket(AF_INET,SOCK_DGRAM,0);
    if(s<0)
     {
       perror("socket\n");
       exit(1);
        }
   
   width=s+1;
     FD_ZERO(&rset);
   /*  FD_ZERO(&wset);  */
     FD_SET(s,&rset);
   /*  FD_SET(s,&wset);  */

   bzero(&sour,sizeof(sour));
/*   addr=inet_addr(HOST_ADDR);
   host=gethostbyaddr((char *)&addr,4,AF_INET);
*/
   host=gethostbyname("linux");
   sour.sin_family=AF_INET;
   sour.sin_port=htons(PORT);
   bcopy(host->h_addr,(char *)&sour.sin_addr,host->h_length);
 
  if((ret=bind(s,(struct sockaddr *)&sour,sizeof(sour))<0))
     {
       perror("bind");
       exit(1);
    }
   fromlen=sizeof(dest);
   ret=recvfrom(s,buf,strlen(buf),0,(struct sockaddr *)&dest,&fromlen);
   if(ret<0)      
    {
     switch(errno){
  case  EINTR : ret=0; break;
  case  EFAULT: ret=1;break;
 
    }
    perror("recvfrom");
    exit(1);
    }
   else
     {
   printf("%s\n",buf);
   printf("%d",ret);
      }
   /*   while(1) */
   {
/*    if (select(width,&rset,NULL,NULL,NULL)<0)
     {
      perror("select");
      exit(1);
     }
*/
 /* watch if we can get some from the socket? */  
/*  if(FD_ISSET(s,&rset))
    {
    recvfrom(s,buf,strlen(buf),0,\
            (struct sockaddr *)&dest, &fromlen);
    printf("%s",buf);    
    exit(0);
    } */
  /* watch if we need send some data ?
  if(FD_ISSET(s,&wset))
    {

    }
   
   */
 


 };   /* while (1)*/    
    close(s);
     
}









/////******************/////



  the errno=EFAULT.



   thanx!
0
jceaCommented:
Before the bind, try to dup "host" onto "sour". Entire, not only the address. Use, also, "ANY_ADDR" instead "linux". Is "linux" the name of your machine?. Has a correct IP Translation?
0
redsunAuthor Commented:
Hi:
  jcea,yes,linux is my machine name and I can "ping linux" successfully.would you help me correct my error?

   thanx!
0
jceaCommented:
I insist. Try "ANY_ADDR", delete the "host" structure in your program and replace it entirely by "sour" :)).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Networking

From novice to tech pro — start learning today.