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

    Default sending file 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 :(Im appreciate your help :D

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default

    I'd suggest that you do a search for sample code on the various forums to see how its down.

    You code and your comments don't agree.
    The client won't receive by using a writeObject() method.
    The server appears to be reading instead of sending

    Your code would be easier to read if you put in the CODE tags. Put CODE inside of [ ] and end with [/ ]

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Start with what @norm said.

    In general, over a network you send bytes, not objects.

    And your OP says you want to send a file. In nearly all cases, a file is a stream of bytes. So sending a file is well suited to the usual pattern of sending bytes.

    Do some more work, and ask again

  4. #4
    parimal is offline Member
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

    Default

    Client should write on the server socket,but u didn't do that..
    That is why server read nothing
    Parimal
    java heart

  5. #5
    HafHothLact is offline Member
    Join Date
    Mar 2009
    Location
    US
    Posts
    4
    Rep Power
    0

    Default Hi

    Hi Guys,

    Long time lurker thought i would eventually join up :) sorry if this is the wrong section mods!

    Jen

  6. #6
    rexicon is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Worked for me

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Stop raising zombies with your spam
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. best Java Network API to use?
    By San_Andreas in forum Networking
    Replies: 1
    Last Post: 04-30-2008, 08:42 PM
  2. Servlet sending the file (setting header)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 08:14 PM
  3. sending image file from JSP to Servlet
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 01-10-2008, 02:34 PM
  4. 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
  •