how return a jstring using callbacks?
hello again everyone, i'm still with the proyect in JNI...
now in the callback method i want to return a jstring (or at least a char*), i'm doing this, and i don't know if i'm right....
in my Server_TCP.cpp:
JNIEXPORTvoid JNICALL Java_SocketC_conectaServidor(JNIEnv *env, jobject obj)
//void main()
{
buffer[0]='\0';
clase = env->FindClass("SocketC");
mid = env->GetMethodID(clase,"recibeBuffer","(Ljava/lang/String;)Ljava/lang/String;");
if (mid == 0)
return;
WSAStartup(MAKEWORD(2,0),&wsa);//MAKEWORD dit qu'on utilise la version 2 de winsock
printf("TCP conexion Sockets\n\n");
system("TITLE TCP Conexion Sockets (Version server)");
int port;
printf("Port : ");//On demande juste le port, pas besoin d'ip on est sur un server
scanf("%i",&port);
sinserv.sin_family=AF_INET;//Je ne connais pas d'autres familles
sinserv.sin_addr.s_addr=INADDR_ANY;//Pas besoin d'ip pour le server
sinserv.sin_port=htons(port);
server=socket(AF_INET,SOCK_STREAM,0);//On construit le server
//SOCK_STREAM pour le TCP
bind(server,(SOCKADDR*)&sinserv,sizeof(sinserv));
//On lie les parametres du socket avec le socket lui meme
listen(server,SOMAXCONN);//On se met ?閏outer avec server, 0 pour n'accepter qu'une seule connection
printf(" Servidor conectado.");
while(1)
{
sinsize=sizeof(sin);
if((sock=accept(server,(SOCKADDR*)&sin,&sinsize))!=INVALID_SOCKET)
{//accept : acepta cualquier conexion
if (hReadThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)
servicio, 0, 0, &dwThreadID))
{
while(true)
{
//printf("\nbuff: %s",buffer);
while(buffer[0]!='\0')
{
jstring buf = env->NewStringUTF(buffer);
/*CHECK FROM HERE */
jstring respuesta = (jstring)malloc(sizeof(char)*256);
respuesta = (jstring)env->CallStaticObjectMethod(clase, mid, buf);//llama a Java
printf("JAVA respondio: %s",respuesta);
buffer[0]='\0';
/*TO HERE*/
}
}
GetExitCodeThread(hReadThread,&dwExitCode);
CloseHandle (hReadThread);
}
else
{
// Could not create the read thread.
printf("No se pudo crear");
exit(0);
}
}
}
}
and this is my SocketC.java:
publicclass SocketC
{
privatenativevoid conectaServidor();
private String recibeBuffer(String s)
{
System.out.println("desde JAVA "+s);
return"JAVAOK";
}
publicstaticvoid main(String args[])
{
SocketC SC =new SocketC();
SC.conectaServidor();
}
static{
System.loadLibrary("Server_TCP");
}
}
what i want is when i call the method in java i can return a string to C++ to handle it, the problem is that doing this i get this on response:
>java SocketC
TCP conexion Sockets
Port : 5000
Servidor conectado.
Cliente aceptado!!!!! // when the client is connected
Mensaje del cliente: ey //message from the client
desde JAVA ey //this message is from JAVA means that the callback is ok
JAVA respondio: └G?#38;?p'?►'?#38;?8&?Φ%?@%?Φ$?8$?#?H#?H!?
α ?p ?@ ?x1? // this should be the return :s
as you can see, i get trash on the string i want to get returned
anyone can help me?

