Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Smile Sending a file through socket

    Hi everybody..........

    I'm trying to send a file from server to client through sockets.
    There is no problem in server side. In client side the program doesn't quits. I can't get the file fully written.

    Here is the code.... Anybody help me please...........

    Server:

    import java.io.*;
    import java.net.*;


    class ServerTest
    {
    public static void main(String args[]) throws IOException {
    Server a = new Server();
    a.start();
    }
    }

    class Server {

    BufferedInputStream bis;
    BufferedOutputStream bos;
    int in;
    public void start() throws Exception,Throwable
    {

    ServerSocket ss=new ServerSocket(1870);

    while (true) {
    try{

    System.out.println("Waiting for Client");
    Socket s=ss.accept();
    System.out.println("\nClient connected...");

    String filename="ex081111.log";

    System.out.println("Sending the file");

    try
    {
    bis=new BufferedInputStream(new FileInputStream(filename));
    bos=new BufferedOutputStream(s.getOutputStream());
    byte[] sendData=new byte[8192];
    while((in=bis.read(sendData))!=-1){
    bos.write(sendData, 0,in);
    }
    bis.close();
    bos.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    System.out.println("File Sent Successfully" );


    }
    catch(Exception e)
    {
    //Destroys the current instance
    this.finalize();
    }

    }

    }
    }


    Client:


    import java.net.*;
    import java.io.*;
    class ClientTest
    {

    public static void main(String args[]) throws Exception
    {
    try
    {
    Socket s=new Socket("localhost",
    System.out.println("Ready to receive");
    BufferedInputStream bis;
    BufferedOutputStream bos;
    int in;

    byte[] recievedData = new byte[8192];
    bis = new BufferedInputStream(s.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream("recievedfile.txt"));
    while ((in = bis.read(recievedData)) != -1)
    bos.write(recievedData,0,in);
    System.out.println("File Recieved Successfully");
    bos.close();
    bis.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();

    }
    }
    }

  2. #2
    strati1991 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    maybe use a PrintWriter server side an init it with PrintWriter(s.getOutputStream(),true);

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It would be helpful if you use "code" tags to make your code more readable. Obviously
    Java Code:
    Socket s=new Socket("localhost",
    is a line that isn't going to compile. Is that your problem?

Similar Threads

  1. sending file over network
    By qwerty in forum Networking
    Replies: 6
    Last Post: 04-25-2009, 01:55 AM
  2. problem in socket connection in sending images
    By vibhor in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 05:39 AM
  3. Replies: 1
    Last Post: 02-10-2009, 09:16 AM
  4. sending image file from JSP to Servlet
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 01-10-2008, 02:34 PM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •