moved files out of trunk/ in prep to track this proj primarily on github
[javacyc.git] / UnixDomainSocket.c
blob066e24985923a2989ddb9279dfe6438360c5475b
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
9 a socket. */
11 #include <jni.h>
12 #include "UnixDomainSocket.h"
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <sys/un.h>
17 #include <sys/unistd.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <strings.h>
25 JNIEXPORT jint JNICALL Java_UnixDomainSocket_nativeOpen(JNIEnv *jEnv, jclass jClass, jstring jSocketFile)
27 struct sockaddr_un serverAddress;
28 int serverAddressLength;
29 int socketFileHandle;
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)
45 /* Return error */
46 /* printf("ERRNO: %d\n", errno); */
47 perror("open");
48 return -1;
51 if(connect(socketFileHandle, (struct sockaddr *)&serverAddress, serverAddressLength) <0)
53 /* Return error */
54 /* printf("ERRNO: %d\n", errno); */
55 perror("open");
56 return -1;
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 */
69 char buffer[1];
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 **/
79 return -1;
80 /* return result; */
82 /* Otherwise, return the data read */
83 else
85 return buffer[0];
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}; */
94 char buffer[1];
95 int result;
96 buffer[0] = jData;
98 /* Write a byte from the buffer to the socket */
99 result = write(jSocketFileHandle, buffer, 1);
101 /* Return the result */
102 return 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);