Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

about recvfrom()?

Posted on 1998-02-24
5
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 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