Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:48:12+00:00 2026-06-13T06:48:12+00:00

I have created one native application in android that is working fine. This application

  • 0

I have created one native application in android that is working fine. This application is based on socket programing. so for that first i have to open that port by which i want to process my data , then processing code after that need to close. but problem is that as i am new in ndk programing i am able to open and process data in same function but i want a modular approach. first i want to make one open function then senddata and close. for that i need to create some global variable so that i could use them in my other function. like bellow:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LOG_TAG "native"
int skt;          // i want to use skt and ifr values
struct ifreq ifr;

JNIEXPORT jint JNICALL Java_com_can_demo_NativeControls_Open(JNIEnv * env, jobject obj, jstring port)
{
    if ((skt = socket(PF_, SOCK_RAW, _RAW)) < 0) {
        LOGI( "socket not opend");
    }

    strcpy(ifr.ifr_name, port);
    if( ioctl(skt, SIOCGIFINDEX, &ifr) < 0)
    {
        LOGI( "interface not opend");
    }

    addr._family = AF_;
    addr._ifindex = ifr.ifr_ifindex;

    if(bind (skt, (struct sockaddr*)&addr, sizeof(addr)) <0 )
    {
        LOGI( "bind Error");
    }
}

JNIEXPORT jint JNICALL Java_com_can_demo_NativeControls_Send(JNIEnv * env, jobject obj, jstring path)
{
    const jbyte *str;

    frame._id = 0x123;
    str = (*env)->GetStringUTFChars(env, path, 0);
    strcpy( frame.data, str );
    frame.can_dlc = strlen( frame.data );
    int bytes_sent = write( skt, &frame, sizeof(frame) );
    return bytes_sent;
}

now i have two function: open and sendData
in open function i am initializing some value in skt and ifr. and want to access those data in my senddata function. But when i print my log that time open function is working properly but sendData function is not able to access skt value that is initializing in open function. default its coming “0”. please guide me how can i achieve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:48:14+00:00Added an answer on June 13, 2026 at 6:48 am

    First of all, 0 is a perfectly correct value for a socket. So, your global variable is accessed as expected.

    But your code requires improvement. Some technical issues I explained in a comment. But I would strongly suggest not to use global variables the way you do in this snippet. The recommended way to treat native handles (e.g. pointers) is to return the value to Java (it could be jlong) and pass it back from Java to native when your JNI function must use this value. There are techniques that allow the native code to access fields of the Java object to set/retrieve such information, but in the simple case like yours it’s an overshoot. Here is a simple example in Java:

    public class NativeControls {
    
        private long _nativeSocket = -1;
    
        private native long Open(String port);
    
        public void open(String port) {
            _nativesocket = Open(port);
        }
    
        private native int Send(long socket, String path);
        public void sendData(String path) {
            return Send(_nativesocket, port);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one service in my grails application. in that service 25 methods
I have created an android application that uses android-support-v4.jar . Now when I add
I have created one application in that I need two fragment, So i declared
i have created one image but problem is that when i save image from
i have created one login and register view in that view after login user
I have created one aspx Page from that i need to access the property
I have one application for showing the map but it is not working does
I have created a simple native iPhone app. This project contains with c,c++ source
I have created one thread for calling native function which are calling the MPEG2
I have used facebook application for one of my android application. But i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.