I have a ServerSocket and looking for help.
I want to identify the request from the client of type InputStream or OutputStream. I am stuck with this point and looking forward to your help
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
InputStream is = sock.getInputStream();
new FileServer().receiveFile(is);
sock.close();
}
Please tell me how can I put it in a conditional statement to provide the further execution
From the server side, the
Socket(created byServerSocketwhen a client connection is accepted) allows to read(receive) stream from the client with the input stream and to write(send) stream to the client with the output stream.From the client side, it works with the same logic.
The
Socketallows to read(receive) stream from the server with the input stream and to write(send) stream to the server with the output stream.In your actual code :
Here you get the input stream of the server.
With it you can read message from the client.
For example to read a byte sent by the client (of course to read lines, using a more featured reader such as
BufferedReaderis much better):To write to the client one byte, the server can do :
Here, same remark as for
InputStream:OutputStreamhas only raw methods to write bytes.Using a specific subclass could be better according to your requirements.
You have very good materials and examples on the Oracle site : http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html