Solved

JNI socket connection to C++ server

Posted on 2006-11-03
7
1,372 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

809 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