Exemplo n.º 1
0
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);
	}
}
Exemplo n.º 2
0
void Java_com_apress_echo_EchoServerActivity_nativeStartTcpServer(
		JNIEnv* env,
		jobject obj,
		jint port)
{
	// Construct a new TCP socket.
	int serverSocket = NewTcpSocket(env, obj);
	if (NULL == env->ExceptionOccurred())
	{
		// Bind socket to a port number
		BindSocketToPort(env, obj, serverSocket, (unsigned short) port);
		if (NULL != env->ExceptionOccurred())
			goto exit;

		// If random port number is requested
		if (0 == port)
		{
			// Get the port number socket is currently binded
			GetSocketPort(env, obj, serverSocket);
			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 = AcceptOnSocket(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);
	}
}
Exemplo n.º 3
0
JNIEXPORT void JNICALL Java_com_example_hellojni_Native_nativeStartTcpServer
(JNIEnv *env, jobject obj, jint port) {
// Construct a new TCP socket.
    int serverSocket = NewTcpSocket(env, obj);
    if (NULL == env->ExceptionOccurred())
    {
        // Bind socket to a port number
        BindSocketToPort(env, obj, serverSocket, (unsigned short) port);
        if (NULL != env->ExceptionOccurred())
            goto exit;

        // If random port number is requested
        if (0 == port)
        {
            // Get the port number socket is currently binded
            GetSocketPort(env, obj, serverSocket);
            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 = AcceptOnSocket(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
            char prefix[] = "Server: ";
            char toSend[MAX_BUFFER_SIZE + strlen(prefix)];
            strcpy(toSend, prefix);
            strcat(toSend, buffer);
            sentSize = SendToSocket(env, obj, clientSocket,
                                    toSend, (size_t) strlen(toSend));
            if ((0 == sentSize) || (NULL != env->ExceptionOccurred()))
                break;
        }

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

exit:
    if (serverSocket > 0)
    {
        close(serverSocket);
    }
}
Exemplo n.º 4
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);
	}

}
Exemplo n.º 5
0
JNIEXPORT void JNICALL Java_com_eric_ndkapp_ServerActivity_nativeStartServer
  (JNIEnv * env, jobject obj, jstring ip, jint port, jstring message)
{
	// Construct a new TCP socket.
	if (serverSocket > 0)
	{
		LogMessage(env, obj, "Server is running");
		return;
	}

	serverSocket = NewTcpSocket(env, obj);
	if (NULL == env->ExceptionOccurred())
	{
		// Get IP address as C string
		const char* ipAddress = env->GetStringUTFChars(ip, NULL);
		if (NULL == ipAddress)
			goto exit;

		// Bind socket to a port number
		BindSocketToPort(env, obj, serverSocket, ipAddress, (unsigned short) port);

		// Release the IP address
		env->ReleaseStringUTFChars(ip, ipAddress);

		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 socket = 0;

		if (clientSocket == 0)
		{
			clientSocket = AcceptOnSocket(env, obj, serverSocket);
		}

		if (env->ExceptionOccurred() == NULL)
		{
			// Receive and send back the data
			char buffer[MAX_BUFFER_SIZE];
			ssize_t recvSize;
			ssize_t sentSize;

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

			// Get the message size
			jsize messageSize = env->GetStringUTFLength(message);

			// Send to the socket
			sentSize = SendToSocket(env, obj, clientSocket, messageText, messageSize);

			// Release the message text
			env->ReleaseStringUTFChars(message, messageText);

			while ((recvSize = ReceiveFromSocket(env, obj, clientSocket, buffer, MAX_BUFFER_SIZE)) > 0)
			{
			}
		}
	}

exit:
	return;
}