Android Socket学习unix_socket

Posted we1less

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Socket学习unix_socket相关的知识,希望对你有一定的参考价值。

查看socker命令

 netstat -an | grep "xx"

unix_socket_service.c

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#define MAXLINE 80

char *socket_path = "server-socket";

int main()
{
    struct sockaddr_un serun, cliun;
    socklen_t cliun_len;
    int listenfd, connfd, size;
    char buf[MAXLINE];
    int i, n;

    if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("socket error");
        exit(1);
    }

    memset(&serun, 0, sizeof(serun));
    //本地socket
    serun.sun_family = AF_UNIX;
    strncpy(serun.sun_path, socket_path, sizeof(serun.sun_path) - 1);
    //这个相当于把之前的地址要移除,不然上一个server没有结束,移除会报错already in use
    unlink(socket_path);

    if (bind(listenfd, (struct sockaddr *)&serun, sizeof(struct sockaddr_un)) < 0) {
        perror("bind error");
        exit(1);
    }
    printf("UNIX domain socket bound\\n");

    if (listen(listenfd, 20) < 0) {
        perror("listen error");
        exit(1);
    }
    printf("Accepting connections ...\\n");

    while(1) {
        cliun_len = sizeof(cliun);
        //判断是否有新的链接
        if ((connfd = accept(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0){
            perror("accept error");
            continue;
        }
        printf("new client connect to server,client sockaddr === %s \\n",((struct sockaddr *)&cliun)->sa_data);
        while(1) {
            memset(buf, 0, sizeof(buf));
            n = read(connfd, buf, sizeof(buf));
            if (n < 0) {
                perror("read error");
                break;
            } else if(n == 0) {
                printf("EOF\\n");
                break;
            }
            printf("received: %s\\n", buf);

            if(strncmp(buf,"exit",4) == 0) {
                printf("client disconnect \\n");
                break;
            }
            for(i = 0; i < n; i++) {
                buf[i] = toupper(buf[i]);
            }
            write(connfd, buf, n);
        }
        //关闭当前链接
        close(connfd);
    }
    close(listenfd);
    return 0;
}


unix_socket_client.c

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define MAXLINE 80

char *client_path = "client-socket";
char *server_path = "server-socket";

int main() {
        struct  sockaddr_un cliun, serun;
        int len;
        char buf[100];
        int sockfd, n;

        if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
                perror("client socket error");
                exit(1);
        }

    memset(&serun, 0, sizeof(serun));
    //本地socket
    serun.sun_family = AF_UNIX;
    strncpy(serun.sun_path,server_path ,
                   sizeof(serun.sun_path) - 1);
    //链接
    if (connect(sockfd, (struct sockaddr *)&serun, sizeof(struct sockaddr_un)) < 0){
        perror("connect error");
        exit(1);
    }
	
    printf("please input send char:");
    
    while(fgets(buf, MAXLINE, stdin) != NULL) {
	
         write(sockfd, buf, strlen(buf));
         n = read(sockfd, buf, MAXLINE);
         if (n < 0) {
            printf("the other side has been closed.\\n");
         } else if(n==0) {
            printf("exit.\\n");
            break;
         } else {
            printf("received from server: %s \\n",buf);
         }
         printf("please input send char:");
    }
    close(sockfd);
	
    return 0;
}



接下来在linux中可以用 gcc命令对其编译

gcc unix_socket_service.c -o unix_socket_service
gcc unix_socket_client.c -o unix_socket_client

编译成功后可以运行

 关于unix_socket的背景这里放一篇链接

Android Framework实战开发视频--跨进程通信之Socket通信_learnframework的博客-CSDN博客

放两篇吧  第二篇是ip的socket

Android Framework实战开发视频--跨进程通信之Socket通信_learnframework的博客-CSDN博客

本文代码也是基于本篇提供的基本代码编写的

epoll的写法参考这篇

Android Framework实战开发-跨进程通信之 epoll详细讲解_learnframework的博客-CSDN博客

以上是关于Android Socket学习unix_socket的主要内容,如果未能解决你的问题,请参考以下文章

Android - Socket 功能在 Service 中实现这才是实际的使用情况

Android Socket学习三方apk执行shell命令

Native Socket.IO and Android

Android Framework实战开发视频--跨进程通信之Socket通信

Android Framework实战开发视频--跨进程通信之Socket通信

15.Android-实现TCP客户端,支持读写