Online Java Compiler By
JavaTpoint.com
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.IOException; import java.net.*; public class JavaSocketGetInputStreamExample3 { public static void main(String[] args) throws IOException { Socket socket = new Socket(); InetAddress inetAddress=InetAddress.getByName("localhost"); //the port should be greater or equal to 0, else it will throw an error int port=1085; //calling the constructor of the SocketAddress class SocketAddress socketAddress=new InetSocketAddress(inetAddress, port); //binding the socket with the inetAddress and port number socket.bind(socketAddress); //getInputStream() returns an input stream for the given socket //An Input/Output error occurs as this socket is not connected DataInputStream inputStream=new DataInputStream( new BufferedInputStream(socket.getInputStream())); System.out.println("Input Stream: "+inputStream); } }
Output