Results 1 to 16 of 16
  1. #1
    spasavvas is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default send file via client - server model

    Itry to send a file from client to server.
    I do it, but when the server receive the file send an ack msg, then the client give error:
    java.net.SocketException: socket closed
    i don't know why.
    my code is

    client:
    import java.net.*;
    import java.io.*;
    class Client{
    Socket clientSocket;
    byte[] byteArray;
    BufferedInputStream bis;
    BufferedOutputStream bos;
    int in;
    BufferedReader inm = null;
    PrintWriter outm = null;

    public Client(){
    try{
    clientSocket = new Socket("localhost", 9632);
    System.out.println("i am client & connect");
    inm = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    outm = new PrintWriter(clientSocket.getOutputStream(), true);
    System.out.println("------1--------" +clientSocket.isClosed());
    outm.println("msg 1: hi");
    System.out.println("from server__ " +inm.readLine());
    outm.println("msg 2: now i will send you a file");
    System.out.println("from server__ " +inm.readLine());
    sendFile();
    System.out.println("-------2-------" +clientSocket.isClosed());
    System.out.println("from server__ " +inm.readLine());
    System.out.println("-------3------nothing-" );
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    public void sendFile(){
    try{
    bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt"));
    bos = new BufferedOutputStream(clientSocket.getOutputStream( ));
    byteArray = new byte[8192];
    while ((in = bis.read(byteArray)) != -1){
    bos.write(byteArray,0,in);
    }
    bis.close();
    bos.close();
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    public static void main(String[] args){
    new Client();
    }
    }

    server:
    import java.net.*;
    import java.io.*;

    class Server{
    BufferedInputStream bis;
    BufferedOutputStream bos;
    byte[] data;
    Socket socket;
    ServerSocket serverSocket;
    int in;
    BufferedReader inm = null;
    PrintWriter outm = null;

    public Server(){
    try{
    serverSocket = new ServerSocket(9632);
    System.out.println("i am server & listening...");
    socket = serverSocket.accept();
    System.out.println("a client connect");
    System.out.println("------1--------" +socket.isClosed());
    inm = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    outm = new PrintWriter(socket.getOutputStream(), true);
    System.out.println("from client: " +inm.readLine());
    outm.println("ack 1: hi....");
    System.out.println("from client: " +inm.readLine());
    outm.println("ack 2: ok....");
    receiveFile();
    System.out.println("------2--------" +socket.isClosed());
    outm.println("ack 3: take the file");
    }
    catch (IOException e){
    e.printStackTrace();
    }
    }

    public void receiveFile(){
    try{
    byte[] receivedData = new byte[8192];
    bis = new BufferedInputStream(socket.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream("encryptAtmMsg22.txt"));
    while ((in = bis.read(receivedData)) != -1){
    bos.write(receivedData,0,in);
    }
    bos.close();
    }
    catch (IOException e){
    e.printStackTrace();
    }
    }
    public static void main(String[] args){
    new Server();
    }
    }

    any idea pls

  2. #2
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    In the client:
    bis.close();
    bos.close();

    bos.close() closes the underlying stream, not just the BufferedOutputStream. Comment that line out, and things should work for you.

    Don.
    Don MacVittie F5 Networks - DevCentral

  3. #3
    spasavvas is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default send file via client - server model

    when i do it the client didn't send the data

    thanks

  4. #4
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    Sorry, change it from bos.close() to bos.flush() and see if that takes care of it for you (you will eventually have to close bos though, or you'll slowly leak memory, but this will get you started to the solution).

    Don.
    Don MacVittie F5 Networks - DevCentral

  5. #5
    iliana is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default how to play an mp3 file?

    Hello ! I have a problem ...Can anybody help me?:confused: plz?

    I want to play an mp3 file and the code in eclipse is something like the following ... I see the interface but it can't be played...

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;

    public class AudioFilee extends JFrame {

    public AudioFilee() {

    setTitle("Music File");
    setSize(300, 400);

    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();


    JButton s= new JButton("STOP");
    JButton p= new JButton("PLAY");
    JLabel j1 = new JLabel("MusicFile");
    String[] musicfiles={"workspace/eclipse/Audio/asdf.mp3","asd.mp3"};
    JComboBox musiclist=new JComboBox(musicfiles);
    musiclist.setSelectedIndex(1);


    jp1.add(j1);
    jp1.add(musiclist);

    jp2.add(s);
    jp2.add(p);



    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
    true, jp1, jp2);

    splitPane.setOneTouchExpandable(true);
    getContentPane().add(splitPane);

    }
    public static void main(String[] args) {

    AudioFilee sp = new AudioFilee();
    sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sp.setVisible(true);

    }
    }

    Do you have any suggestion? Thanks

  6. #6
    iliana is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    Sorry the right title was how to play an mp3 file...

  7. #7
    devil_is is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default How Send File Using Java Socket.

    -in the site computersciencenotes.net section FAQ->Programming-> "send file with Java" explain how send a File using java socket.

    - in section Download->Java->JavaProgramming there is a Project divided in 2 part: client and Server, that you can use for send a file.

    Developed By: Enrico Chiacchiari and Paolo Loberto.

    www[dot]computersciencenotes[dot]net[slash]index.php?option=com_phocadownload&view=category&i d=13:java-programming&Itemid=2

    :)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default a post from the living dead

    Zombie alert... the post your answering is over 8 months old.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    devil_is is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Zombie alert... the post your answering is over 8 months old.

    CJSL
    ok... but now dont be shy

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I'm sure that your intentions are good, you just have to make sure that your answering a current post... so that the suggestion has benefits.
    ok... but now dont be shy
    huh... not sure what that meant... but welcome to the Java forums !!!

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have to second Cristobol's suggestion that we keep our posts relevant here.

  12. #12
    Belete is offline Member
    Join Date
    Dec 2009
    Location
    AddisAbaba
    Posts
    10
    Rep Power
    0

    Default

    hey can some body help me? i was trying to up load file from my laptop which i used it as a client to my desktop which is used as a server and save this file to my server but i donn know the code for doing this. please get me out of this mess!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Belete, welcome to the forum, but I ask you to please ask a new question in a new thread. Thanks for your cooperation.

  14. #14
    Belete is offline Member
    Join Date
    Dec 2009
    Location
    AddisAbaba
    Posts
    10
    Rep Power
    0

    Default

    can some body please me make me familiar with java.I tried to send file from client to server and more or less it worked but i want to save this file on my server, so please send me a sample code for doing so.

  15. #15
    RohitBhat is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi spasavvas,
    ___________________ deleted by mod ____________________
    Last edited by Fubarable; 08-13-2010 at 11:44 AM.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 06:15 PM
  2. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  3. Replies: 2
    Last Post: 10-05-2008, 06:13 PM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 01:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 04:05 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
  •