Solved

about recvfrom()?

Posted on 1998-02-24
5
278 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

19 Experts available now in Live!

Get 1:1 Help Now