Solved

about recvfrom()?

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squid3 ntlm and itunes authentication 5 94
Linux CentOs 6.7 : i can't start service 8 98
Squid Connection Pools 3 100
CentOS 7 wireless 2 29
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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