Ola pessoal
estou precisando muito de fazer um programa em java q se comunique com c++, na realidade eu ja concegui enviar msg do java para o c++ porem no c++ mostra a mensagem como um ponto de interrogacao (?)
preciso enviar um byte do java para o c++ dai o c++ deve converter este byte para string, aguem ai sab alguma coisa? abaixo esta meu codigo:
java:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main {
private static Socket conexao;
public static void main(String[] args) throws UnknownHostException, IOException {
String msg = "108";
//byte[] bytes = msg.getBytes("ASCII"); //ascII
byte[] bytes = msg.getBytes("Charset");//unicode
System.out.println("Bytes: "+bytes);
System.out.print("Posicoes: ");
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]) ;
}
String strg = new String (bytes, "ISO-8859-1");
System.out.println("\nString: "+strg);
//byte[] b;
Socket s = new Socket("192.168.56.2",30000);
OutputStream saida1 = s.getOutputStream();
ObjectOutputStream saida = new ObjectOutputStream(saida1);
//b=intToFourBytes(msg, false);
//System.out.println("Byte: "+b[0]+" "+b[1]+" "+b[2]+" "+b[3]+" ");
//saida.write(bytes);
saida.write(bytes);
saida.flush();
saida.close();
}
C++:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include<unistd.h>
int main(int argc, char ** argv)
{
int sockfd, novo_fd;
struct sockaddr_in meu_end;
struct sockaddr_in outro_end;
int sin_size;
int bytesEnviados;
meu_end.sin_family = AF_INET;
meu_end.sin_port = htons(30000);
meu_end.sin_addr.s_addr = INADDR_ANY;
memset(&(meu_end.sin_zero), ‘\0’, 8);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
{
printf("\nErro na chamada socket()");
return 1;
}
if(bind(sockfd, (struct sockaddr *) &meu_end, sizeof(struct sockaddr)) == -1)
{
printf("\nErro na chamada de bind()");
return 1;
}
if(listen(sockfd, 10))
{
printf("\nErro na chamada de listen()");
return 1;
}
sin_size = sizeof(struct sockaddr_in);
printf("\nServidor: Aguardando conexao!");
novo_fd = accept(sockfd, (struct sockaddr *)&outro_end, (socklen_t*)&sin_size);
//char *msg = “Bem vindo, cliente”;
//printf("\n\nBytes tentando enviar: %d", sizeof(msg));
char buffer[20];
recv(novo_fd, (void *) buffer, 20, 0);
//bytesEnviados = send(novo_fd, msg, sizeof(msg), 0);
printf("\n\nBytes recebidos: %s", buffer);
return 0;
}
aguardo suas opcoes