scoket应用
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <cutils/sockets.h>
#include <private/android_filesystem_config.h>
int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid)
{
struct sockaddr_un addr;
int fd, ret;
fd = socket(PF_UNIX, type, 0);
if (fd < 0) {
printf("Failed to open socket '%s': %s\n", name, strerror(errno));
return -1;
}
memset(&addr, 0 , sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
name);
ret = unlink(addr.sun_path);
if (ret != 0 && errno != ENOENT) {
printf("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
goto out_close;
}
ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
if (ret) {
printf("Failed to bind socket '%s': %s\n", name, strerror(errno));
goto out_unlink;
}
chown(addr.sun_path, uid, gid);
chmod(addr.sun_path, perm);
printf("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
addr.sun_path, perm, uid, gid);
return fd;
out_unlink:
unlink(addr.sun_path);
out_close:
close(fd);
return -1;
}
///////////////////////////////////////////////////
#if 1
#define LOG_TAG "AT"
#define LOGE(...) fprintf(stderr, "I:" __VA_ARGS__)
#define SOCKET_NAME "test"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <cutils/sockets.h>
//#include <cutils/log.h>
#include <fcntl.h> //open(), read(), write()
//#include <utils/log.h>
#define BUFSIZE 100
//#include <android/log.h>
//#define LOGD(...) __android_log_print(ANDROID_LOG_ERROR, "myserver", __VA_ARGS__)
int main(){
#if 1
int connect_number = 4;
int fdListen = -1, new_fd = -1;
int fd, ret;
char buf[BUFSIZE];
/*char *msg = "";// "ttyGS0 test";
fd = open("/dev/ttyGS0", O_RDWR);
printf("envi serial test program\n");
if(fd < 0)
{
printf("Error: open /dev/ttyGS0 error!\n");
return 1;
}*/
/*ret = write(fd, msg, strlen(msg));
if(ret < 0)
{
printf("Error: write msg error!\n");
return 1;
}*/
/*while(buf[0] == NULL){
memset(buf, 0, sizeof(buf));
ret = read(fd, buf, BUFSIZE);
if(ret < 0)
{
printf("Error: read device error!\n");
return 1;
}
if(buf[0] != '\0')
{
;//printf("%s\n", buf);
}
}*/
//struct sockaddr_un peeraddr;
struct sockaddr peeraddr;
socklen_t socklen = sizeof (peeraddr);
int numbytes ;
char buff[256];
fdListen = create_socket(SOCKET_NAME,SOCK_STREAM,0666,AID_SYSTEM,AID_SYSTEM);//android_get_control_socket(SOCKET_NAME);
if (fdListen < 0) {
LOGE("Failed to get socket '" SOCKET_NAME "' errno:%d", errno);
perror("android_get_control_socket");
printf("error:%s\n",strerror(errno));
exit(-1);
}
while(1){
ret = listen(fdListen, connect_number);
if (ret < 0) {
perror("listen");
exit(-1);
}
//It block until client connected.accept(socket_id, &peeraddr, &socklen);
#if 1
new_fd = accept(fdListen, &peeraddr, &socklen);
if (new_fd < 0 ) {
LOGE("Error on accept() errno:%d", errno);
perror("accept");
//exit(-1);
}
//send at command from ttyGS0
//while(send(new_fd,"AT+MMAT", 7, 0) > 0);
if(send(new_fd, "AT+MMAT", 7, 0) < 0){
LOGE("send()");
}
//read from app device testing result
if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1)
{
LOGE("recv");
perror("recv");
exit(-1);
}
printf("%s",buff);
//
if(send(new_fd,buff,strlen(buff),0)==-1)
{
LOGE("send_");
perror("send");
close(new_fd);
exit(0);
}
//ret = write(fd, buff, strlen(buff));
//if(ret < 0)
//{
// printf("Error: write msg error!\n");
// return 1;
//}
#endif
}
// close(fd);
close(new_fd);
close(fdListen);
#endif
return 0;
}
#endif
|
|