Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Server Module</p> <pre><code>import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerMain { public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(6789); File myFile = new File("ServerMain.java"); while (true) { Socket sock = servsock.accept(); byte[] mybytearray = new byte[1024]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); OutputStream os = sock.getOutputStream(); int count; while ((count = bis.read(mybytearray)) &gt;= 0) { os.write(mybytearray, 0, count); } os.flush(); sock.close(); } } } </code></pre> <p>Client Module: </p> <pre><code>public class ClientMain { public static void main(String[] argv) throws Exception { Socket sock = new Socket("127.0.0.1", 6789); byte[] mybytearray = new byte[1024]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("Demo1.java"); BufferedOutputStream bos = new BufferedOutputStream(fos); int count; while ((count = is.read(mybytearray)) &gt;= 0) { System.out.println(count); bos.write(mybytearray, 0, count); } bos.close(); sock.close(); } } </code></pre> <p>Here this works for me and I noticed from your code that you have taken </p> <p><strong><code>byte[] mybytearray = new byte[(int) file.length()];</code></strong> </p> <p>it should be </p> <p><strong><code>byte[] mybytearray = new byte[1024];</code></strong></p> <p>hope this will work for you.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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