Results 1 to 3 of 3
  1. #1
    qwerty is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default Transferring Over Network

    Hello every one
    This is my first participation ,Im new in java , I have question , How I can send a file over network in a client- server paradigm , server send and client receive . the following is trying code

    //The client code Client.java:


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

    public class Client {

    ObjectInputStream Sinput;
    ObjectOutputStream Soutput;
    Socket socket;


    Client(int port) {


    try {
    socket = new Socket("localhost", port);
    FileInputStream test=new FileInputStream("file name");

    Soutput.writeObject(test);
    Soutput.flush();
    }
    catch(Exception e) {
    System.out.println("Error connectiong to server:" + e);
    return;
    }




    try{
    Sinput.close();
    Soutput.close();
    }
    catch(Exception e) {}
    }

    public static void main(String[] arg) {
    new Client(1500);
    }
    }


    //The server code Server.java:

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

    /
    public class Server {


    private ServerSocket serverSocket;

    Server(int port) {


    try
    {
    serverSocket = new ServerSocket(port);
    System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());

    while(true)
    {
    Socket socket = serverSocket.accept();
    System.out.println("New client asked for a connection");
    TcpThread t = new TcpThread(socket);
    System.out.println("Starting a thread for a new Client");
    t.start();
    }
    }
    catch (IOException e) {
    System.out.println("Exception on new ServerSocket: " + e);
    }
    }


    public static void main(String[] arg) {

    new Server(1500);
    }


    class TcpThread extends Thread implements Serializable {

    Socket socket;
    ObjectInputStream Sinput;
    ObjectOutputStream Soutput;

    TcpThread(Socket socket) {
    this.socket = socket;
    }
    public void run() {

    System.out.println("Thread trying to create Object Input/Output Streams");
    try
    {

    Soutput = new ObjectOutputStream(socket.getOutputStream());
    Soutput.flush();

    }
    catch (IOException e) {
    System.out.println("Exception creating new Input/output Streams: " + e);
    return;
    }
    System.out.println("Thread waiting for a String from the Client");

    try {
    byte[] msgArray=null;

    File file = (File)Sinput.readObject();
    FileInputStream fisSrc=new FileInputStream(file);
    FileOutputStream fosDes=new FileOutputStream("ClientRceivedTempFile");
    int n;
    while ((n = fisSrc.available()) > 0) {
    byte[] b = new byte[n];
    int result = fisSrc.read(b);
    if (result == -1) break;
    fosDes.write( b );}
    fisSrc.close();
    fosDes.close();


    }
    catch (IOException e) {
    System.out.println("Exception reading/writing Streams: " + e);
    return;
    }

    catch (ClassNotFoundException o) {
    }
    finally {
    try {
    Soutput.close();
    Sinput.close();
    }
    catch (Exception e) {
    }
    }
    }
    }
    }

    Im lost:o help me please :confused:Im appreciate your help :p

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Please note that the Tutorials section is currently off limits for questions specifically unrelated to the tutorial. I've taken the liberty of creating a thread in the proper place so your issue can be addressed.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    You cannot send the FileInputStream object over the network you need to send the data that is in the file.

    Look at it this way

    Sending Side:
    1)Open File for reading
    2)Read a chunk of data from file
    3)Write chunk of data to port
    4)Repeat from 2 until whole file has been sent
    5)Close file

    Receiving Side:
    1)Open file for writing
    2)Read chunk of data from port
    3)Write chunk of data to file
    4)Repeat from 2 until all data has been received
    5)Close file

Similar Threads

  1. sending file over network
    By qwerty in forum Networking
    Replies: 6
    Last Post: 04-25-2009, 02:55 AM
  2. database+network
    By hidar in forum JDBC
    Replies: 4
    Last Post: 06-30-2008, 10:04 AM
  3. best Java Network API to use?
    By San_Andreas in forum Networking
    Replies: 1
    Last Post: 04-30-2008, 09:42 PM
  4. How to get URL from network machine
    By Mir in forum Networking
    Replies: 1
    Last Post: 04-02-2008, 01:08 AM
  5. Non Blocking Network
    By mathias in forum Networking
    Replies: 1
    Last Post: 08-07-2007, 07:49 AM

Posting Permissions

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