Solved

JNI socket connection to C++ server

Posted on 2006-11-03
7
1,422 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
Technology Partners: 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

617 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