5

I am trying to convert this scriptlet code to JSF class.

The view code

<f:view>
<h:form binding="#{jsfSocketClient.form}">
    <h:outputText binding="#{jsfSocketClient.text}"/>

</h:form>
</f:view>

and the java code

private HtmlForm form = new HtmlForm();
private HtmlOutputText text = new HtmlOutputText();

public HtmlForm getForm() 
{
    System.out.println("instance:  "+FacesContext.getCurrentInstance().getResponseWriter());
    ResponseWriter writer = (FacesContext.getCurrentInstance()).getResponseWriter();  
    try{

        int character;
        Socket socket = new Socket("127.0.0.1", 8765);

        InputStream inSocket = socket.getInputStream();
        OutputStream outSocket = socket.getOutputStream();

        String str = "Hello!\n";
        byte buffer[] = str.getBytes();
        outSocket.write(buffer);
        char characters = 0;
        while ((character = inSocket.read()) != -1) {
            text.setValue((char)character);
            //writer.write((char)character);
            //characters += (char)character;
        }
        //text.setValue(characters);
        if(str.equalsIgnoreCase("bye"))
                {
                    socket.close();
                }
    }
    catch(Exception e)
    {
        e.printStackTrace();
        text.setValue("You must first start the server application (YourServer.java) at the command prompt.");          
    }
    return form;
}

When I run scriptlet code, I am getting the answer as "The server got this: Hello! "

When I run the JSF code I am not getting this reply. Please correct my mistake

Thanks in advance

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
mvg
  • 1,574
  • 4
  • 37
  • 63
  • how do you _run_ the jsf code.. ? – Bozho Nov 26 '10 at 08:09
  • What are you getting instead? – Tomas Narros Nov 26 '10 at 08:11
  • @tomas some unknown character like this ঠ – mvg Nov 26 '10 at 08:55
  • @Bozho Sorry I couldn't get you – mvg Nov 26 '10 at 08:55
  • 1
    @mvg: this is caused because your code for the InputStream processing is wrong. You should not cast directly every byte to a char, as some special chars can be two bytes long. Also, you are overriding the text value for every byte, so at the end, text only contains one byte. Check @morja answer and it's InputStream processing. – Tomas Narros Nov 26 '10 at 09:19
  • @Bozho I just started YourServer.Java in console from Eclipse and then started this JSF page in Eclipse. – mvg Nov 26 '10 at 09:40
  • 1
    @Tomas You spotted the correct problem. I did not handle the inputstream correctly. Apart from trying morja's code, I modified the code, provided in the question. Using Stringbuffer I appended the input value and outside the loop I set the Stringbuffer to the HTMLOutputText. This also worked. – mvg Nov 26 '10 at 10:35

1 Answers1

3

I correct my answer. The problem is the processing of the input stream. Here is the fixed code:

    String response = "";
    try {

        Socket socket = new Socket("127.0.0.1", 8765);

        Reader reader = new InputStreamReader(socket.getInputStream());
        OutputStream outSocket = socket.getOutputStream();

        String str = "Hello!\n";
        byte buffer[] = str.getBytes();
        outSocket.write(buffer);

        CharArrayWriter result = new CharArrayWriter();
        char[] buf = new char[4096];
        int charsRead = 0;
        while ((charsRead = reader.read(buf)) != -1) {
            result.write(buf, 0, charsRead);
        }
        response = result.toString();

        if (str.equalsIgnoreCase("bye")) {
            socket.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        response = "You must first start the server application (YourServer.java) at the command prompt.";
    }
    text.setValue(response);
morja
  • 8,297
  • 2
  • 39
  • 59
  • I think that the main problem in fact was the input socket processing, in which he was setting for every byte the value field, with a direct conversion to a character. @morja code process the InputStream in a correct way, so my upvote for this answer. – Tomas Narros Nov 26 '10 at 09:16
  • 1
    True. I fixed the code (buffer/buf) mistake, thanks for the remark. – morja Nov 26 '10 at 09:19
  • 1
    @morja The error comes as "The method read(byte[]) in the type InputStream is not applicable for the arguments (char[])" when I use your updated code. I debugged your previous code and it receiving some input from the server, but only empty string returns as output – mvg Nov 26 '10 at 09:39
  • 1
    @mvg Yes, sorry, there was another thing I missed in the code, I changed it to use a Reader. – morja Nov 26 '10 at 09:54
  • @morja binding is not the problem – mvg Nov 26 '10 at 10:27