void Java_com_apress_echo_EchoServerActivity_nativeStartUdpServer( JNIEnv* env, jobject obj, jint port) { // Construct a new UDP socket. int serverSocket = NewUdpSocket(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; } // Client address struct sockaddr_in address; memset(&address, 0, sizeof(address)); char buffer[MAX_BUFFER_SIZE]; ssize_t recvSize; ssize_t sentSize; // Receive from the socket recvSize = ReceiveDatagramFromSocket(env, obj, serverSocket, &address, buffer, MAX_BUFFER_SIZE); if ((0 == recvSize) || (NULL != env->ExceptionOccurred())) goto exit; // Send to the socket sentSize = SendDatagramToSocket(env, obj, serverSocket, &address, buffer, (size_t) recvSize); } exit: if (serverSocket > 0) { close(serverSocket); } }
void Java_com_apress_echo_EchoClientActivity_nativeStartUdpClient( JNIEnv* env, jobject obj, jstring ip, jint port, jstring message) { // Construct a new UDP socket. int clientSocket = NewUdpSocket(env, obj); if (NULL == env->ExceptionOccurred()) { struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = PF_INET; // Get IP address as C string const char* ipAddress = env->GetStringUTFChars(ip, NULL); if (NULL == ipAddress) goto exit; // Convert IP address string to Internet address int result = inet_aton(ipAddress, &(address.sin_addr)); // Release the IP address env->ReleaseStringUTFChars(ip, ipAddress); // If conversion is failed if (0 == result) { // Throw an exception with error number ThrowErrnoException(env, "java/io/IOException", errno); goto exit; } // Convert port to network byte order address.sin_port = htons(port); // 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 message to socket SendDatagramToSocket(env, obj, clientSocket, &address, messageText, messageSize); // Release the message text env->ReleaseStringUTFChars(message, messageText); // If send was not successful if (NULL != env->ExceptionOccurred()) goto exit; char buffer[MAX_BUFFER_SIZE]; // Clear address memset(&address, 0, sizeof(address)); // Receive from the socket ReceiveDatagramFromSocket(env, obj, clientSocket, &address, buffer, MAX_BUFFER_SIZE); } exit: if (clientSocket > 0) { close(clientSocket); } }
JNIEXPORT void JNICALL Java_com_example_hellojni_Native_nativeStartUdpServer (JNIEnv *env, jobject obj, jint port) { // Construct a new UDP socket. int serverSocket = NewUdpSocket(env, obj); if (NULL == env->ExceptionOccurred()) { LOGD("Create UDP Socket..."); // 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; } struct sockaddr_in serv; char serv_ip[20]; socklen_t serv_len = sizeof(serv); getsockname(serverSocket, (struct sockaddr *)&serv, &serv_len); inet_ntop(AF_INET, &serv.sin_addr, serv_ip, sizeof(serv_ip)); LogMessage(env, obj, "Server on %s:%d\n", serv_ip, ntohs(serv.sin_port)); // LogMessage(env, obj, "Server on %s:%d\n", serv_ip, ntohs(serv.sin_port)); while (1) { // Client address struct sockaddr_in address; memset(&address, 0, sizeof(address)); char buffer[MAX_BUFFER_SIZE]; ssize_t recvSize; ssize_t sentSize; // Receive from the socket recvSize = ReceiveDatagramFromSocket(env, obj, serverSocket, &address, buffer, MAX_BUFFER_SIZE); LOGD("Received from %s:%hu." , inet_ntoa(address.sin_addr), ntohs(address.sin_port)); LOGD("Received %hu bytes: %s", recvSize, buffer); // LogMessage(env, obj, "Received from %s:%hu" // , inet_ntoa(address.sin_addr), ntohs(address.sin_port)); // LogMessage(env, obj, "Received %hu bytes: %s", recvSize, buffer); if ((0 == recvSize) || (NULL != env->ExceptionOccurred())) goto exit; // Send to the socket // combine data char prefix[] = "Server: "; char buffer2[MAX_BUFFER_SIZE + strlen(prefix)]; strcpy(buffer2, prefix); strcat(buffer2, buffer); sentSize = SendDatagramToSocket(env, obj, serverSocket, &address, buffer2, (size_t) recvSize); } } exit: if (serverSocket > 0) { close(serverSocket); } }