Solved

JNI socket connection to C++ server

Posted on 2006-11-03
7
1,347 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now