Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

x.Hm am writing a client server program. Is there something that needs to be don

ID: 3615333 • Letter: X

Question

x.Hm am writing a client server program. Is there something that needs to be done to a socket for reading and writing data back and forth to a server. I checked Javadocs for Sockets but there didn't seem to be something like this. The client sends and recives find but when it tries to send a second message, nothing happens.

Basically this is what happens in program

Client -> sends query
Server returns result
Based on this result
Client -> send another query
Both the client and server say they are waiting for response.

Any ideas?














StringBuffer instr = new StringBuffer();

            Socket connection = null;
            try {
                InetAddress address = InetAddress.getByName(host);
                /** Establish a socket connetion */
                        System.out.println("Got net address");
                connection = new Socket(address, port);
                /** Instantiate a BufferedOutputStream object */
                        System.out.println("Got connection from port");

                        BufferedOutputStream bos= new BufferedOutputStream(connection.getOutputStream());
                        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
              
                /** Write across the socket connection and flush the buffer */
                osw.write(qprice);
                osw.flush();
                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
                InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

                /**Read the socket's InputStream and append to a StringBuffer */
                        System.out.println("Before reading input stream");
                int c;
                while ( (c = isr.read()) != 13)
                  instr.append( (char) c);
                       
                        //If we are going to buy the stock
                        double d, input;
                        input = 0; d = 1;
                      
                         try {
                              d = Double.valueOf(instr.toString().trim()).doubleValue();
                              input = Double.valueOf(ebPrice.getText().trim()).doubleValue();
                              System.out.println("double d = " + d);
                            } catch (NumberFormatException nfe) {
                             System.out.println("Server didn't return a price in price format: " + nfe.getMessage());
                             }
                        try {
                              Thread.sleep(10000);
                            }
                        catch (Exception e){}
                            //If we are going to buy the stock                                  
                            if(d <= input){
                                osw.write(exists);
                                osw.flush();
                           
                  &

Explanation / Answer

Dear..

When two applications want to communicate to each other, theyestablish a connection and send data back and forth over thatconnection, than, TCP guarantees that data sent from one end of theconnection actually gets to the other end and in the same order itwas sent. Otherwise, an error is reported.
TCP provides a point-to-point channel for applications that requirereliable communications. The Hypertext Transfer Protocol (HTTP),File Transfer Protocol (FTP), and Telnet are all examples ofapplications that require a reliable communication channel. Theorder in which the data is sent and received over the network iscritical to the success of these applications. When HTTP is used toread from a URL, the data must be received in the order in which itwas sent. Otherwise, you end up with a jumbled HTML file, a corruptzip file, or some other invalid information.

sample Java code for above program:

}