Solved

JNI socket connection to C++ server

Posted on 2006-11-03
7
1,378 Views
Last Modified: 2008-01-16
Dear experts,

I need to create a Java program that connect to a server via socket. The server is written in C++, and I have an API.lib and a header file to work with. The API.lib provides me a function void connect () to establish a socket connection. It is easy to build a C++ client using that function.
My question is: Can I create a Java client that connect dicrectly to C++ server using that function? (via JNI)

Does the socket connection is made if I using JNI call the C++ function connect ()? (I will have a JNI style C++ function wrap around that function, off course).


Thanks in advance
0
Comment
Question by:phongtran72
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17871998
>>My question is: Can I create a Java client that connect dicrectly to C++ server using that function? (via
>> JNI)

Once you handed over control to the C/C++ code, you can do anything that is within the capabilities of the language and the supported APIs, so the answer is "yes".

>>Does the socket connection is made if I using JNI call the C++ function connect ()? (I will have a JNI
>>style C++ function wrap around that function, off course).

The idea actually is to create a wrapper, since socket communcations are hardly ever handled within a single function call. You will need to store some data (socket handle etc.) at some place - where will depend on how you design the whole thing.
0
 

Author Comment

by:phongtran72
ID: 17874933

Thanks for your advice. Do you think is it better to write a C++ executable file to handle a socket connection and push data to Java apps using JNI. In this case, JNI functions/methods only handle a data streaming.

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 17882367
That would be an option, too. However, I am not sure whether the IPC overhead will be much different from the one introduced by JNI...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:phongtran72
ID: 17891810

What is wrong with my code. I am using VS2005 to build DLL library. (JNI interface)
The error of the code below are: (I'll split 250 points for this question) :)

error C2144: syntax error : 'void' should be preceded by ';'
missing type specifier - int assumed. Note: C++ does not support default-int
syntax error : missing ';' before identifier 'Java_MyInterface_displayConection'
'JNICALL' : illegal use of type 'void'
 error C2065: 'JNIEnv' : undeclared identifier

#include "stdafx.h"
#include <stdlib.h>                        
#include <stdio.h>                        


void getMessage()
{

      fprintf(stderr, "Some message");

}

JNIEXPORT void JNICALL Java_MyInterface_displayConection(JNIEnv *, jobject)
{
      getMessage();      
}
0
 

Author Comment

by:phongtran72
ID: 17891822
There is #include "jni.h" the the code above.
0
 

Author Comment

by:phongtran72
ID: 17892067
Well, this generates error:

#include "jni.h"
#include "stdafx.h"
#include <stdlib.h>                    
#include <stdio.h>      

And this is solution:

#include "stdafx.h"
#include <stdlib.h>                    
#include <stdio.h>
#include "jni.h"

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17892126
Sorry for not being here earlier, "meeting day". Yes, the header files need to be in a certain order sometimes...
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question