Solved

JNI socket connection to C++ server

Posted on 2006-11-03
7
1,395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC APP to Win32 APP. 19 134
c++ syntax question 9 67
Using Diagnostic tools in VS2015: Unresoved allocations 19 148
Android development question 2 111
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 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…

752 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