Solved

about recvfrom()?

Posted on 1998-02-24
5
288 Views
Last Modified: 2008-03-06
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?
0
Comment
Question by:redsun
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:jcea
ID: 1587535
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
 

Author Comment

by:redsun
ID: 1587536
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
 
LVL 1

Expert Comment

by:jcea
ID: 1587537
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
 

Author Comment

by:redsun
ID: 1587538
Hi:
  jcea,yes,linux is my machine name and I can "ping linux" successfully.would you help me correct my error?

   thanx!
0
 
LVL 1

Accepted Solution

by:
jcea earned 50 total points
ID: 1587539
I insist. Try "ANY_ADDR", delete the "host" structure in your program and replace it entirely by "sour" :)).
0

Featured Post

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now