java - how to establish connections between a servlet and a socket -
supposing have servlet reads , writes file socket , want establish connection between servlet , socket, how done?
well, socket, read somewhere 1 has this:
url url = new url("http://example.com/getfile"); urlconnection con = url.openconnection(); con.setdooutput(true);
how same servlet?
i wanted since sending , receiving files between servlet , scoket. also, how both of them know when other has sent file , should read it?
i have searched can't find site explains it.
i unsure mean 'socket'. if socket standard protocol (http/https/ftp) can use above piece of code in servlet use in stand alone program.
if socket not prescribe standard might want open direct connection using below piece of code:
socket socket = new socket(server,port); //get input stream socket bufferedreader inputstream = new bufferedreader(new inputstreamreader( socket.getinputstream())); //get output stream socket. note // stream autoflush. printwriter outputstream = new printwriter(new outputstreamwriter( socket.getoutputstream()),true); outputstream.println("send_ data"); system.out.println(inputstream.readline()); socket.close();
regarding file sending piece of code, if primary task involved file transfer can use ftp protocol send files between servlet , socket.
can have file watcher on socket end know when file transferred. refer filewatcher , ftp more information.
Comments
Post a Comment