views:

45

answers:

2

Hi, I'm trying to produce a simple server that will allow me test the Androids security features. I need to develop an application that will open a socket. I've produced something similar in C, but I am having no look with java. Here's the application in C

// simpleserver3.c


#define MY_PORT     9999
#define MAXBUF      99


void indata(int clientfd, struct sockaddr_in client_addr)
{
char buffer[12];
printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
recv(clientfd, buffer, MAXBUF, 0); //this is will overflow the buffer
printf("%X \n", &buffer);
}

int main(int Count, char *Strings[])
{
struct sockaddr_in self, client_addr;
int sockfd,clientfd;
/*---Create streaming socket---*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) //socketfd = handle for socket
{
    perror("Socket");
    exit(errno);
}

/*---Initialize address/port structure---*/
bzero(&self, sizeof(self));
self.sin_family = AF_INET;
self.sin_port = htons(MY_PORT);
self.sin_addr.s_addr = INADDR_ANY;

/*---Bind the structure to the socket handle ---*/
if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
{
    perror("socket--bind");
    exit(errno);
}

/*---Make it a "listening socket"---*/
if ( listen(sockfd, 20) != 0 )
{
    perror("socket--listen");
    exit(errno);
}

//set socklen_t to length of client address
socklen_t addrlen=sizeof(client_addr);
/*---accept a connection (creating a data pipe)---*/

clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen); //create handle for communicating
indata(clientfd, client_addr);
close(clientfd);
close(sockfd);
return;
}

Any sugguestion would be great, Aneel

A: 

It's been a while since I used C, so I can't comment on your C code, but you should probably take a look at the Android documentation for the Socket class:

http://developer.android.com/reference/java/net/Socket.html

Computerish