USB共享網路:android手機通過USB與Ubuntu進行socket網路通訊

crazy_baoli發表於2019-01-10

測試平臺:三星S4,核心3.4.5

Ubuntu版本:14.04

===========================

 

1. 開啟手機移動熱點中USB網路共享,並將手機通過USB連線到PC

2. Ubuntu下使用命令ifconfig

2.1 inet addr:192.168.42.193就是我們客戶端程式connect使用的地址:

address.sin_family = AF_INET;

address.sin_addr.s_addr = inet_addr("192.168.42.193");

address.sin_port = htons(9734);

len = sizeof(address);

result = connect(sockfd, (struct sockaddr *)&address, len);

2.2 伺服器端也是使用192.168.42.193這個ip地址進行bind,呼叫bind()函式之後,為socket()函式建立的套接字關聯一個相應地址,傳送到這個地址的資料可以通過該套接字讀取與使用。

server_address.sin_family = AF_INET;

server_address.sin_addr.s_addr = inet_addr("192.168.42.193");

server_address.sin_port = htons(9734);

server_len = sizeof(server_address);

bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

或者使用: server_address.sin_addr.s_addr = htonl(INADDR_ANY); 意味著可以接收本系統所安裝的任何一個網路卡的資料包。

 

3. 編寫程式碼並編譯

arm-client3.c

/*  Make the necessary includes and set up the variables.  */

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char ch = 'A';

/*  Create a socket for the client.  */

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket, as agreed with the server.  */

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.42.138");
    address.sin_port = htons(9734);
    len = sizeof(address);

/*  Now connect our socket to the server's socket.  */

    result = connect(sockfd, (struct sockaddr *)&address, len);

    if(result == -1) {
        perror("oops: client3");
        exit(1);
    }

/*  We can now read/write via sockfd.  */

    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server = %c\n", ch);
    close(sockfd);
    exit(0);
}

server3.c

/*  Make the necessary includes and set up the variables.  */

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

/*  Remove any old socket and create an unnamed socket for the server.  */

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket.  */

    server_address.sin_family = AF_INET;
  //  server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_addr.s_addr = inet_addr("192.168.42.193");
    server_address.sin_port = htons(9734);
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

/*  Create a connection queue and wait for clients.  */

    listen(server_sockfd, 5);
    while(1) {
        char ch;

        printf("server waiting\n");

/*  Accept a connection.  */

        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd, 
            (struct sockaddr *)&client_address, &client_len);

/*  We can now read/write to client on client_sockfd.  */

        read(client_sockfd, &ch, 1);
        ch++;
        write(client_sockfd, &ch, 1);
        close(client_sockfd);
    }
}

伺服器編譯:gcc -o server3 server3.c

客戶端編譯:arm-none-linux-gnueabi-gcc -static -o arm-client3 arm-client3.c

 

4. 執行

先將arm-client3 push到手機:adb push ./arm-client3 /sdcard/

Ubuntu下: ./server3

adb shell下: ./arm-client3

相關文章