NDK TCP Server/Client Help

173 views
Skip to first unread message

dani maoz

unread,
Jun 12, 2013, 1:50:41 AM6/12/13
to android-d...@googlegroups.com, andro...@googlegroups.com
Hi
I would like to implement a TCP Server/client using NDK, can anyone post on example or an ndk project.
Another Question
i implemented an TCP server but when the client connect gethostbyaddr return NULL any idea
See below the code


childfd = accept(parentfd, (struct sockaddr *) &clientaddr, &clientlen);
if (childfd < 0) 
 printf("ERROR on accept");
/* 
         * gethostbyaddr: determine who sent the message 
         */
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
 sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL) {
 printf("ERROR on gethostbyaddr\n");
 return 0;
 
}
    
    printf("Test1\n");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
 printf("ERROR on inet_ntoa\n");
    
printf("server established connection with %s (%s)\n", 
  hostp->h_name, hostaddrp);
    
/* 
         * read: read input string from the client
         */
bzero(buf, BUFSIZE);
n = read(childfd, buf, BUFSIZE);
if (n < 0) 
 printf("ERROR reading from socket");
    
printf("server received %d bytes: %s", n, buf);
    
/* 
         * write: echo the input string back to the client 
         */
n = write(childfd, buf, strlen(buf));
if (n < 0) 
 printf("ERROR writing to socket");

dani maoz

unread,
Jun 12, 2013, 2:20:06 AM6/12/13
to android-d...@googlegroups.com, andro...@googlegroups.com

Solved A Casting needed
 if (connect(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) 
       perror("connect failed");
Reply all
Reply to author
Forward
0 new messages