예제 #1
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);
	}
}
예제 #2
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);
	}

}