Note that there are some explanatory texts on larger screens.

plurals
  1. POJava request file, send file (Client-server)
    primarykey
    data
    text
    <p>I'm making a Client-Server. I've gotten as far as that the server can send a hardcoded file, but not a client specified. I will have to send only text files. As far as I have understood: the clients firstly sends the file name and then, the server sends it, nothing complicated, but I'm getting all kinds of errors, this code is getting a connection reset/socket closed error. The main problem is, that hadn't got much time to research networking.</p> <p>Ill appreciate any help I can get.</p> <p><strong>EDIT.</strong> I found a work around, closing a stream causes the socket to close, why is that? It shouldn't happen, should it?</p> <p>Server Side:</p> <pre><code> InputStream sin=newCon.getInputStream(); DataInputStream sdata=new DataInputStream(sin); location=sdata.readUTF(); //sdata.close(); //sin.close(); File toSend=new File(location); byte[] array=new byte[(int)toSend.length()]; FileInputStream fromFile=new FileInputStream(toSend); BufferedInputStream toBuffer=new BufferedInputStream(fromFile); toBuffer.read(array,0,array.length); OutputStream out=newCon.getOutputStream(); //Socket-closed... out.write(array,0,array.length); out.flush(); toBuffer.close(); newCon.close(); </code></pre> <p>ClientSide:</p> <pre><code>int bytesRead; server=new Socket(host,port); OutputStream sout=server.getOutputStream(); DataOutputStream sdata=new DataOutputStream(sout); sdata.writeUTF(interestFile); //sdata.close(); //sout.close(); InputStream in=server.getInputStream(); //socket closed.. OutputStream out=new FileOutputStream("data.txt"); byte[] buffer=new byte[1024]; while((bytesRead=in.read(buffer))!=-1) { out.write(buffer,0,bytesRead); } out.close(); server.close(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload