0

I sending strings to a server but the server get strange chars before the string i've send, I'v tried to flush before send, after send and after inicializating the outputstream variable but the result is the same.I Heard a lot about flush() and also search about it, but still didnt find how to solve my problem, maybe its simple but i cant get it.

please Help me!

Client Side

    InetAddress endereco = InetAddress.getByName(null); 
    socket= new Socket(endereco, SServer.PORTO);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new ObjectOutputStream(socket.getOutputStream()),true);

    while(true){                        
        System.out.println("Write your Thoughs.. ");
        Scanner scanner = new Scanner(System.in);
        String Msg = scanner.nextLine();
        System.out.println("I Said: ");
        System.out.println(Msg);
        out.println(Msg);
        out.flush();
        String s  = in.readLine();
        System.out.println("Echo: "+s);

}

//Serve Side

in = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    out.flush();
}

private void serve() throws IOException {
                    while(true){
        String s = in.readLine();
        System.out.println("Server Received: " + s);
        if(s.equals("END"))
            break;
        System.out.println("Server Send: " + s);
        out.println(s);     


    }
Aluado
  • 1
  • 1

1 Answers1

0

I think what is happening is that when you write your strings, you are pushing them through the ObjectOutputStream, which is attempting to serialize them (I think). You are making things more difficult than necessary. Try using

out = new PrintWriter(socket.getOutputStream());

That will just dump the text directly through the line without all the additional stuff.

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • It doesn't serialize the strings. But constructing the OOS sends the serialization header. From its documentation: "This constructor writes the serialization stream header to the underlying stream". The OP should also specify an encoding to read and write characters, instead of relyong on the default one, whic has a good chance to be different. – JB Nizet Jul 29 '13 at 17:55
  • good info! I didn't know that. Is it correct to assume the junk text he's getting is that serialization header? @JBNizet – Russell Uhl Jul 29 '13 at 17:56
  • I would assume so, yes. If you correct your answer, I'll upvote it. You caught the wrong usage of an OOS. – JB Nizet Jul 29 '13 at 17:56
  • Russell Uhl I've already tried that but the result is the same. @JBNizet and which encoding should i set the system UTF-8? My system uses cp-1252 and the strings i put only got english chars. And when should i set the encoding as in [http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding]here someone say that the "file.encoding property has to be specified as the JVM starts up". thanks in advance – Aluado Jul 29 '13 at 23:10