예제 #1
0
jobject Java_com_rprata_socketjni_socket_SocketResource_nativeStartLocalClient(JNIEnv* env, jobject obj, jstring name)
{

	int clientSocket = NewLocalSocket(env, obj);

	if (NULL == env->ExceptionOccurred())
	{
		const char * nameText = env->GetStringUTFChars(name, NULL);
		if (NULL == nameText)
			goto exit;

		// Connect to IP address and port
		ConnectToLocalAddress(env, obj, clientSocket, nameText);
		// Release the IP address
		env->ReleaseStringUTFChars(name, nameText);

		// If connection was successful
		if (NULL != env->ExceptionOccurred())
			goto exit;
 		
 		// jobject fdObject = jniCreateFileDescriptor(env, clientSocket);
		jclass class_fdesc = env->FindClass("java/io/FileDescriptor");
		
		jmethodID const_fdesc = env->GetMethodID(class_fdesc, "<init>", "()V");
		if (const_fdesc == NULL) return NULL;
		
		jobject fdObject = env->NewObject(class_fdesc, const_fdesc);
		
		jfieldID field_fd = env->GetFieldID(class_fdesc, "descriptor", "I");
		
		if (field_fd == NULL) return NULL;
		
		env->SetIntField(fdObject, field_fd, clientSocket);

		return fdObject;
	}
		
exit:
	if (clientSocket > 0)
	{
		close(clientSocket);
	}
 
 	return NULL;

}
예제 #2
0
파일: Echo.cpp 프로젝트: sheep3652014/Echo
void Java_com_apress_echo_LocalEchoActivity_nativeStartLocalServer(
		JNIEnv* env,
		jobject obj,
		jstring name)
{
	// Construct a new local UNIX socket.
	int serverSocket = NewLocalSocket(env, obj);
	if (NULL == env->ExceptionOccurred())
	{
		// Get name as C string
		const char* nameText = env->GetStringUTFChars(name, NULL);
		if (NULL == nameText)
			goto exit;

		// Bind socket to a port number
		BindLocalSocketToName(env, obj, serverSocket, nameText);

		// Release the name text
		env->ReleaseStringUTFChars(name, nameText);

		// If bind is failed
		if (NULL != env->ExceptionOccurred())
			goto exit;

		// Listen on socket with a backlog of 4 pending connections
		ListenOnSocket(env, obj, serverSocket, 4);
		if (NULL != env->ExceptionOccurred())
			goto exit;

		// Accept a client connection on socket
		int clientSocket = AcceptOnLocalSocket(env, obj, serverSocket);
		if (NULL != env->ExceptionOccurred())
			goto exit;

		char buffer[MAX_BUFFER_SIZE];
		ssize_t recvSize;
		ssize_t sentSize;

		// Receive and send back the data
		while (1)
		{
			// Receive from the socket
			recvSize = ReceiveFromSocket(env, obj, clientSocket,
					buffer, MAX_BUFFER_SIZE);

			if ((0 == recvSize) || (NULL != env->ExceptionOccurred()))
				break;

			// Send to the socket
			sentSize = SendToSocket(env, obj, clientSocket,
					buffer, (size_t) recvSize);

			if ((0 == sentSize) || (NULL != env->ExceptionOccurred()))
				break;
		}

		// Close the client socket
		close(clientSocket);
	}

exit:
	if (serverSocket > 0)
	{
		close(serverSocket);
	}
}
예제 #3
0
void Java_com_rprata_socketjni_socket_SocketResource_nativeStartLocalServer(JNIEnv* env, jobject obj, jstring name, jstring filename)
{
	// Construct a new local UNIX socket.
	int serverSocket = NewLocalSocket(env, obj);

	if (NULL == env->ExceptionOccurred())
	{
		// Get filename from String class
		const char * utf8 = env->GetStringUTFChars(filename, NULL);

	    FILE * file = fopen(utf8, "rb");
	    if (file == NULL)
	    {
	    	LOGI("Erro ao abrir o arquivo");
	    	goto exit;
	    }

	   	env->ReleaseStringUTFChars(filename, utf8);

		// Get name as C string
		const char * nameText = env->GetStringUTFChars(name, NULL);
		if (NULL == nameText)
			goto exit;

		// Bind socket to a port number
		BindLocalSocketToName(env, obj, serverSocket, nameText);

		// Release the name text
		env->ReleaseStringUTFChars(name, nameText);

		// If bind is failed
		if (NULL != env->ExceptionOccurred())
			goto exit;

		// Listen on socket with a backlog of 4 pending connections
		ListenOnSocket(env, obj, serverSocket, 4);
		if (NULL != env->ExceptionOccurred())
			goto exit;

		// Accept a client connection on socket
		int clientSocket = AcceptOnLocalSocket(env, obj, serverSocket);
		if (NULL != env->ExceptionOccurred())
			goto exit;

		char buffer[MAX_BUFFER_SIZE];
		ssize_t recvSize;
		ssize_t sentSize;

		size_t bytesRead;
		
		// Receive and send back the data
		while ((bytesRead = fread(videoBuffer, 1, BUFFER_SIZE, file)) > 0)
		{
			sentSize = SendToSocket(env, obj, clientSocket,
					videoBuffer, bytesRead);
		}

		// Close the client socket
		close(clientSocket);
	}

exit:
	if (serverSocket > 0)
	{
		close(serverSocket);
	}

}