1 /* UnixDomainSocket.c */
2 /* J-BUDS version 1.0 */
3 /* Copyright (c) 2001; Echogent Systems, Inc. */
4 /* See COPYRIGHT file for license details */
6 /* Modified by Thomas Yan on 05/29/2003 to compile on cc on Solaris */
7 /* Modified by Thomas Yan on 08/02/2004 nativeClose should call close instead
8 of shutdown to close the socket since shutdown does not actually close
12 #include "UnixDomainSocket.h"
14 #include <sys/socket.h>
15 #include <sys/types.h>
17 #include <sys/unistd.h>
25 JNIEXPORT jint JNICALL
Java_UnixDomainSocket_nativeOpen(JNIEnv
*jEnv
, jclass jClass
, jstring jSocketFile
)
27 struct sockaddr_un serverAddress
;
28 int serverAddressLength
;
31 /* printf("open\n"); /\* debug *\/ */
33 /* Convert the Java socket file String to a C string */
34 const char *socketFile
= (*jEnv
)->GetStringUTFChars(jEnv
, jSocketFile
, 0);
36 /* Create the server address */
37 bzero((char *)&serverAddress
,sizeof(serverAddress
));
38 serverAddress
.sun_family
= AF_UNIX
;
39 strcpy(serverAddress
.sun_path
, socketFile
);
40 serverAddressLength
= strlen(serverAddress
.sun_path
) + sizeof(serverAddress
.sun_family
);
42 /* Create the socket */
43 if( (socketFileHandle
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
46 /* printf("ERRNO: %d\n", errno); */
51 if(connect(socketFileHandle
, (struct sockaddr
*)&serverAddress
, serverAddressLength
) <0)
54 /* printf("ERRNO: %d\n", errno); */
59 /* Release the C socket file string */
60 (*jEnv
)->ReleaseStringUTFChars(jEnv
, jSocketFile
, socketFile
);
62 /* Return the socket file handle */
63 return socketFileHandle
;
66 JNIEXPORT jint JNICALL
Java_UnixDomainSocket_nativeRead(JNIEnv
*jEnv
, jclass jClass
, jint jSocketFileHandle
)
68 /* Create the char buffer */
71 /* Read a byte from the socket into the buffer */
72 int result
= read(jSocketFileHandle
, buffer
, 1);
74 /* fprintf(stderr, "The return value is: %d\n", result); */
76 /* If the result is less than 0, return the error */
77 if(result
<=0) /** code modified here to also catch 0 **/
82 /* Otherwise, return the data read */
90 JNIEXPORT jint JNICALL
Java_UnixDomainSocket_nativeWrite(JNIEnv
*jEnv
, jclass jClass
, jint jSocketFileHandle
, jint jData
)
92 /* Create the char buffer and put the data in it */
93 /*char buffer[] = {jData}; */
98 /* Write a byte from the buffer to the socket */
99 result
= write(jSocketFileHandle
, buffer
, 1);
101 /* Return the result */
105 JNIEXPORT
void JNICALL
Java_UnixDomainSocket_nativeClose(JNIEnv
*jEnv
, jclass jClass
, jint jSocketFileHandle
)
107 /* Close the socket */
108 close(jSocketFileHandle
);
111 JNIEXPORT
void JNICALL
Java_UnixDomainSocket_nativeCloseInput(JNIEnv
*jEnv
, jclass jClass
, jint jSocketFileHandle
)
113 /* Close the socket input stream */
114 shutdown(jSocketFileHandle
, SHUT_RD
);
117 JNIEXPORT
void JNICALL
Java_UnixDomainSocket_nativeCloseOutput(JNIEnv
*jEnv
, jclass jClass
, jint jSocketFileHandle
)
119 /* Close the socket output stream */
120 shutdown(jSocketFileHandle
, SHUT_WR
);