简体   繁体   中英

problem with tcp server and client

Hii all, I have already created some programs related to client and server. Today it was my sessional[practical exam] of Client-server technology.

Problem was: i was to add two no's at server sent from client and result back to client.

I tried this solution but some strange solution was there: Server.java

import java.net.*;
import java.io.*;

public class Server{
    public static void main(String args[]) throws Exception{
        ServerSocket s = new ServerSocket(7896);
        Socket cs = s.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
        bw.write(br.read() + br.read());
    }
}

Client.java

import java.io.*;
import java.net.*;

public class Client{
public static void main(String args[]) throws Exception{
    Socket s = new Socket("localhost", 7896);
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    bw.write(3);
    bw.write(4);
    System.out.println("Output is: " + br.read());
}
}

when i ran it on dos prompt i got two blank screens; one at client site and one at server site(which was a bit surprising). then i closed server then suddenly i got error msg at client something like connection closed.

Then i ran same program on my ubuntu linux with same jdk 1.6 as it was in windows, and here also blank screens but when i closed server i got:

Output is: -1

Although in exam i done this using DataOutputStream and DataInputStream.

But why the above code is not working.

Can Anyone explain what is happening in the code there.

Thanks

A few comments which might help you reach the correct answer:

  • The server code as it is can accept only a single client request since the accept() call isn't present in an infinite loop.
  • When using buffered streams, make sure you always flush the OuputStream/Writer to ensure that the data is actually written to the client instead of just lying around in the buffer.
  • DataInput/OutputStream is absolutely required (or any other approach which logically reads the "number" string and converts it to an appropriate representation which can be added) since just reading from the BufferedReader doesn't fetch you the "numbers".

But the question is, why flushing is required? To answer this question, one has to understand why "buffering" is used in the first place. Why bother wrapping your streams in another stream when you can directly write to your stream? The reason is that I/O operations are really expensive (at least when compared to accessing your RAM or CPU cache/registers). Frequent writes/reads accessing/writing small chunks of data to the HDD can choke up your HDD and bring down the overall performance of your application.

So what's the solution? Write data to the HDD less frequently. Two ways this can be achieved:

  1. Manually allocate a large byte array and fill up the entire array in a single read. This is way better than single byte reads. The byte array size does require tuning as per your application needs but for general purposes 8K should be good to go.
  2. Use a buffered stream. This has the advantage of not exposing the clients to the low level details. Your application can use the "stream" as it sees fit (continuously writing single bytes to it) and the stream would assume the complete responsibility of flushing the buffered data when it sees fit (this depends on the buffer size which you set when creating a buffered stream).

Though the explanation was HDD specific, the same applies to other types of stream like in your case socket.

TL;DR: Buffering improves IO throughput/performance. :)

write() will buffer input so it does not need to do lots of small writes. Since you are not writing much to the buffer, it is probably still there and not written out yet. Calling flush() will force it to write out the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM