Dimitris Terzis wrote:
>
> Hi guys...
>
> Playing with native methods, I have created a very basic program that
> creates a TCP/IP socket from Java by using the corresponding C call (i.e.,
> socket()).
You are sending your output through two different mechanisms. The
System.out.print mechanism managed by Java and the stdio mechanism
managed by libc have different sets of output buffers that happen to
flush at different times. There's no reason to expect any sort of
synchronization between them. If you want some predictability in the
order of output, you can put in explicit flush calls -
System.out.flush() on the Java side and fflush(stdout) on the C side.
Nathan
>
> My code (excluding error handling parts) is something like:
>
> // File NativeSocketApplication.java
> public class NativeSocketApplication
> {
> private static NativeSocket socket;
>
> public static void main(String args[])
> {
> System.out.print("\n\nApplication started");
>
> socket = new NativeSocket();
>
> System.out.print("\n\nNativeSocket() returned
> successfully.");
> }
> }
>
> // File NativeSocket.java
> public class NativeSocket()
> {
> public NativeSocket()
> {
> System.out.print("\n\nCalling native method");
>
> nativeSocketCreate(AF_INET, SOCK_DGRAM, PF_INET);
> }
>
> private void nativeSocketCreate(int family, int type, int protocol)
> {
> // Calls Linux socket(family, type, protocol) function
> }
> }
>
> // File native_socket.c
> {
> // My nativeSocketCreate function with JNI syntactic sugar
> JNIEXPORT void JNICALL Java_NativeSocket_nativeSocketCreate
> (JNIEnv *env, jobject jthis, jint family, jint type, jint
> protocol)
> {
> printf("\n\nCreating native socket...");
> socket(family, protocol, data);
> }
> }
>
> Very simple, BUT: When I run the program under windows, as expected, I get
> the following output:
>
> Application started
>
> Calling native method
>
> Creating native socket...
>
> NativeSocket() returned successfully.
>
> which is reasonable (I use only one thread).
>
> In Linux, things are not doing that well. Using either JDK 1.1.7 or JDK 1.2,
> I get
>
> Application started
>
> Calling native method
>
> NativeSocket() returned successfully.
>
> ==> Creating native socket... (!!!)
>
> In JDK 1.1.7, the last message is even printed after the Unix prompt(!)...
>
> Is this just a problem related to interaction with the output (can't be that
> the order of execution is altered, surely!), or does it mean potential
> synchronisation problems?
>
> Thanks,
>
> Dimitris
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]