2012-06-22 7 views
2

실제로, 나는 안드로이드 에뮬레이터 (ps : 그들은 sucesssfully 실행하고 안드로이드 genric 크로스 컴파일러와 함께 컴파일됩니다)에서 server.c 및 client.c와 함께 테스트를했습니다. 그리고 , 나는 더 나아가 jni를 통해 클라이언트 측을 다시 작성합니다. 그러나이 경우 클라이언트는 서버 측에 연결하지 못합니다. 그러나 새 클라이언트는 client.c와 매우 비슷합니다. 검색 한 후 누군가가 권한 문제에 대해 언급합니다. 그러나 <uses-permission android:name="android.permission.INTERNET" /> (ps :이 태그는 애플리케이션 태그 외부에 있음)을 추가해도 문제가 계속 발생합니다. 그리고 logcat이 보여 주듯이, 자바 코드는 실제로 c 메소드를 호출하지만, 왜 그것이 client.c와 똑같이 동작하지 않습니까? 어떤 생각이라도 나에게 많은 도움이 될 것입니다. 미리 감사드립니다! ndk를 통해 소켓 프로그래밍의 오류를 연결

server.c :

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

#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <sys/un.h> 
#include <unistd.h> 
#include <stdlib.h> 

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

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

    unlink("server_socket"); 
    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 

/* Name the socket. */ 

    server_address.sun_family = AF_UNIX; 
    strcpy(server_address.sun_path, "server_socket"); 
    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); 
    } 
} 

client.c :

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

#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <sys/un.h> 
#include <unistd.h> 
#include <stdlib.h> 

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

/* Create a socket for the client. */ 

    sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 

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

    address.sun_family = AF_UNIX; 
    strcpy(address.sun_path, "server_socket"); 
    len = sizeof(address); 

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

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

    if(result == -1) { 
     perror("oops: client1"); 
     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); 
} 

자바 코드 : 패키지 gz.kaiwii;

public class NSocket { 
    static{ 
     System.loadLibrary("NSocket"); 
    } 
    public native void start(); 
} 

네이티브 코드 :

/* DO NOT EDIT THIS FILE - it is machine generated */ 
#include <jni.h> 
#include <android/log.h> 
#include <android/bitmap.h> 

#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <sys/un.h> 
#include <unistd.h> 
#include <stdlib.h> 

#define LOG_TAG "NSocket" 
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) 
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) 



JNIEXPORT void JNICALL Java_gz_kaiwii_NSocket_start 
    (JNIEnv * env, jobject object){ 
    LOGI("JNICALL Java_gz_kaiwii_NSocket_start is called!"); 

    int sockfd; 
    int len; 
    struct sockaddr_un address; 
    int result; 
    char ch = 'A'; 

/* Create a socket for the client. */ 

    LOGI(" Create a socket for the client!"); 

    sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 
    if(sockfd==-1){ 
    LOGE("create socket error!!!!!"); 
} 

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

    address.sun_family = AF_UNIX; 
    strcpy(address.sun_path, "server_socket"); 
    len = sizeof(address); 

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

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

    LOGI(" Now connect our socket to the server's socket."); 

    if(result == -1) { 
     LOGE("connect error!"); 
     exit(1); 
    } 


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

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

로그 캣 : 나는 이름으로 추상적 인 네임 스페이스를 사용하는 경우 enter image description here

+0

나는 logcat.thx로 그것을 편집하고있다! –

+0

서버가 수신합니까? 연결 실패시 인쇄 오류 메시지는 어떻습니까? 그것을 위해 errno와 strerror를 사용하십시오. –

+0

@ Mārtiņš Možeiko, 서버가 오류 메시지를 표시합니다. 그런 파일이나 디렉토리가 없습니다. 클라이언트 프롬프트 연결이 거부되었습니다.하지만 jni를 사용하여 기본 코드를 호출하지 않으면 모든 행운이 제대로 실행됩니다. –

답변

0

은, 모든 알았지를 실행 한 가지주의해야한다는 길이입니다 !

+0

귀하의 질문은 나에게 매우 도움이되었습니다. jni 자체에서 서버와 클라이언트를 모두 가질 수 있는지 알려주시겠습니까 ??? 그렇다면 데이터를 송수신하기 위해 동시에 서버와 클라이언트를 시작하는 방법보다 ... – Zax