Hello, Android Developers

I programmed a udpserver sample program wich Android NDK (version
android-ndk-r4b).
When, I call recvfrom function, but "struct sockaddr *addr" parameter
returns NULL value.

This is output of below sample code..

./udpserver
read from 0.0.0.0:0 ..

It works well when I was static compile with GNU libc without Bionic-C
libc.

Anybody solved this problem ?


==================================================================================
Sample code is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


#if 1
#define DEBUG(x, args ...) printf(x, ## args)
#else
#define DEBUG(x, args ...)
#endif

/*---------------------------------------------------------------------
*/
int udp_open(int port);
int udp_close(int socket_no);
int udp_write(int socket_no, char *str, int len, struct sockaddr
*addr);
int udp_read(int socket_no, char *str, int len, struct sockaddr
*addr);

/*---------------------------------------------------------------------
*/
int udp_open(int port)
{
        struct  sockaddr_in     srv_addr;
        int     socket_no, ret;
        unsigned char   opt;

        if((socket_no=socket(AF_INET, SOCK_DGRAM, 0)) < 0) return -1;

        opt = 1;
        setsockopt(socket_no, SOL_SOCKET, SO_BROADCAST, (const void *)&opt,
sizeof(opt));

        memset(&srv_addr, 0, sizeof(struct sockaddr_in));

        srv_addr.sin_family = AF_INET;
        srv_addr.sin_port   = htons(port);
        srv_addr.sin_addr.s_addr = INADDR_ANY;

        ret = bind(socket_no, (struct sockaddr *)&srv_addr,
sizeof(srv_addr));

        if(ret<0) return -1;

        return(socket_no);

}

/*---------------------------------------------------------------------
*/
int udp_close(int socket_no)
{
   return close(socket_no);
}

/*---------------------------------------------------------------------
*/
int udp_write(int socket_no, char *str, int len, struct sockaddr
*addr)
{
        int ret;
        int i=0;

        while(1)
        {
                ret = sendto(socket_no, &str[i], len-i, 0, addr, sizeof(struct
sockaddr));
                i+=ret;
                if((i>=len)||(ret<0)) break;
        }

        if(ret<0) return -1;

        return ret;

}

/*---------------------------------------------------------------------
*/
int udp_read(int socket_no, char *str, int len, struct sockaddr
*addr)
{
        int read;

        return recvfrom(socket_no, str, len, 0, addr, &read);
}


/*---------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
        int sock;
        struct sockaddr_in addr;

        fd_set rfds;
        struct timeval tv;
        char buffer[1024];
        int len, ret;


        if((sock = udp_open(12345)) < 0)
        {
                DEBUG("Socket(%d) Open Fail.\n", 12345);
                return 0;
        }

        while(1)
        {
                FD_ZERO(&rfds);
                FD_SET(sock, &rfds);

                tv.tv_sec = 5;
                tv.tv_usec = 0;

                if((ret = select(sock+1, &rfds, NULL, NULL, &tv)) < 0)
                        break;

                if(ret > 0)
                {
                        if( FD_ISSET(sock, &rfds) )
                        {
                                len = udp_read(sock, buffer, 1024, (struct 
sockaddr *)&addr);
                                buffer[len] = 0;

                                DEBUG("read from %s:%d .. \n", 
inet_ntoa(addr.sin_addr),
ntohs(addr.sin_port));

                        }
                }
                else
                {
                        // timeout ...

                }
        }

        udp_close(sock);

        return 0;
}

==================================================================================
Makefile :

TARGET = udpserver

ANDROID_NDK_PATH=/home/hisona/android-ndk-r4b
CROSS  = $(ANDROID_NDK_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/
bin/arm-eabi-
INCDIR = $(ANDROID_NDK_PATH)/build/platforms/android-8/arch-arm/usr/
include
LIBDIR = $(ANDROID_NDK_PATH)/build/platforms/android-8/arch-arm/usr/
lib

CC      = $(CROSS)gcc
AR      = $(CROSS)ar
LD      = $(CROSS)ld
NM      = $(CROSS)nm
RANLIB  = $(CROSS)ranlib
STRIP   = $(CROSS)strip

INCLUDE = -I. -I$(INCDIR)

CFLAGS = -Wall
LDFLAGS = -nostdlib -Wl,--dynamic-linker,"/system/bin/linker" -L$
(LIBDIR) -lc -lm -lstdc++ -ldl

# crt
CRTOBJ = $(LIBDIR)/crtbegin_dynamic.o

# application file
APPSOURCES = main.c
APPOBJS = $(APPSOURCES:.c=.o)

# define the rule
.SUFFIXES:.c .o

.c.o:
        @echo Compiling: $<
        $(CC) -c $(CFLAGS)  $(INCLUDE) -o $@ $<

all: app

app: $(APPOBJS)
        @echo Linking: $(TARGET)
        $(CC) -o $(TARGET) $(APPOBJS) $(CRTOBJ) $(LDFLAGS)
        $(STRIP) -s $(TARGET)

clean:
        @rm -vf $(APPOBJS) $(TARGET)





-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to