0

In the below server socket program, byte stream object is used to read data from client, as mentioned - InputStream in = socket.getInputStream();

public class SingleThreadTCPServer{
    ServerSocket serverSocket;
    int serverPort = 3333;

    public SingleThreadTCPServer(){
        try{
            serverSocket = new ServerSocket(serverPort);
            System.out.println("ServerSocket: " + serverSocket);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    private void listen(){
        try{
            while(true){  //run until you terminate the program
                //Wait for connection. Block until a connection is made.
                Socket socket = serverSocket.accept();
                System.out.println("Socket: " + socket);
                InputStream in = socket.getInputStream();
                int byteRead;
                //Block until the client closes the connection (i.e., read() returns -1)
                while((byteRead = in.read()) != -1){
                    System.out.println((char)byteRead);
                }
            }
        }catch(IOException ex){
            ex.printStackTrace();
        }

    }

    public static void main(String[] args){
        new SingleThreadTCPServer().listen();
    }
}

I had been through the implementation of getInputStream() method but could not understand the actual subtype of InputStream object that is actually returned from getInputStream()

Among the below subtypes,

enter image description here

Which is the actual stream type that is being used by server to read data from client?

overexchange
  • 2,245
  • 2
  • 17
  • 47

2 Answers2

2

There's no inherent reason to believe that those are the only subtypes of InputStream. They are merely the only ones described by the Java language beforehand. The implementation of the socket can use any subtype. It is an implementation detail that is not specified.

DeadMG
  • 36,794
  • 8
  • 70
  • 139
  • If you see this [link](http://www.ntu.edu.sg/home/ehchua/programming/java/J6a_Networking.html), server uses byte based `abstract class InputStream` - `in =socket.getInputStream() ` where as client is using abstract `class PrintWriter` - `out = new PrintWriter(client. getOutputStream());` So, when you say any subtype, how can one use `ByteArrayInputStream` for example? – overexchange Dec 23 '14 at 12:05
1

That is a native object, it is (probably) implemented in C++. You can't write anything like it in Java since Java do not directly expose its external environment. You can however bind functions written in other languages by using the Java Native Interface.

Doing all of that yourself would be a lot of work, so luckily the Java Runtime Environment has already implemented these interfaces for you. This allows you to treat them as standard Java objects without having to worry about how they work in practice. If you really want to learn about them you need to learn C++.

Johan
  • 254
  • 2
  • 3
  • Despite the implementation internally is non-java, the return type is sub type of `InputStream`. So Without bothering the language, I would ask which type of stream is being used? type of stream cannot vary, it should be any one type of stream that is subtype of `InputStream`.I also understand that, this is implementation detail. – overexchange Dec 23 '14 at 11:52
  • No, you can return objects for interfaces and abstract classes without declaring a new class. Write something like: InputStream in= new InputStream(){public int read(){return 0;}};. The object returned from a socket implements the InputStream interface, that is all you know about it. – Johan Dec 23 '14 at 12:39