Hello there when I try and use these two lines of code in my connections method in my program it gives me the error “cannot find symbol method getOutputStream()” I have no idea what I am doing wrong, heres the code
socket = new ServerSocket(6000);
socket.accept();
ObjectInputStream inputStream;
ObjectOutputStream outputStream;
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
Is there a command I am trying to use that doesn’t exist?
Simple:
ServerSocketdoesn’t have that method. It makes no sense to write to or read from a simply “listening” socket – you need to use the streams associated with an accepted socket.You should be using:
Note how this is actually using the return value of
ServerSocket.accept(), which is aSocket– andSocketdoes have those methods.As a meta-comment, you said you had “no idea” what you were doing wrong: the compiler told you exactly what you were doing wrong – trying to call a
getOutputStreammethod onServerSocket. Your immediate first port of call after seeing that compiler error should have been the Javadoc forServerSocket– which would have allowed you to confirm that it really didn’t exist.