6 Replies - 3902 Views - Last Post: 09 March 2011 - 07:44 AM Rate Topic: ***-- 2 Votes

#1 millar_ninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-March 11

Send a file over a socket

Posted 07 March 2011 - 09:27 AM

How do you send a file from a client program to a server program. I have the connection and that works I just dont know how to send a file like file.xml to the server for processing. What outputStream shold I use or if a stream at all?
Thanks for any help
Is This A Good Question/Topic? 0
  • +

Replies To: Send a file over a socket

#2 ericzmeh   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Re: Send a file over a socket

Posted 07 March 2011 - 10:03 AM

I'm not sure what your code looks like, but maybe the File class would help?
Was This Post Helpful? 1
  • +
  • -

#3 millar_ninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-March 11

Re: Send a file over a socket

Posted 07 March 2011 - 10:17 AM

this is what my server looks like
import java.io.*;
import java.net.*;
import java.util.*;

public class Server2012 {
     public static void main(String[] args)
   {
         //handel request parsing and validation
         RequestParser rp = new RequestParser();
         ResultParser result = new ResultParser();
         
      try
      {
         // establish server socket
         ServerSocket s = new ServerSocket(8189);

         // wait for client connection
         Socket incoming = s.accept();
         try
         {
             boolean done = false;
             OutputStream outStream = incoming.getOutputStream();
             PrintWriter out = new PrintWriter(outStream);
             out.println("Connected to Gus Server 2012.");
             out.println("Enter Bye to disconnet");
             out.flush();
        while(done != true){
            

            System.out.println(incoming.getOutputStream());
            System.out.println(incoming.toString());
            FileInputStream fis = new FileInputStream(incoming.toString());
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println("From streams");
            System.out.println(ois);
           
            done = true;

            
             }

         }
         finally
         {
            incoming.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }

}


and this is my clinet looks like
import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 *
 * @author Kenshin
 */
public class Client2012 {
    public boolean matchCase = false;
    public boolean fullMatch = false;
    public static void main(String[] args) throws IOException
    {
     
     //defult server port 8189
     //sever information
     int port= 8189;
     String host = "localhost";

     //file that is the xml file
     File f = new File("file.xml");

  
     System.out.println("You will be connected with server at port " + port);
         
     //connect to server
    try{
        Socket client = new Socket(host, port);
        DataOutputStream socketOut = new DataOutputStream(client.getOutputStream());
        InputStream inStream = client.getInputStream();
        //this is for the server
        Scanner in = new Scanner(inStream);
  
        //welcome line to show connection
        String line = in.nextLine();
        System.out.println(line);
                     /*
                    //now send it to the server for validation
                    byte [] mybytearray = new byte [(int)f.length()];
                    FileInputStream fis = new FileInputStream(f);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    System.out.println("Sending...");
                    socketOut.write(mybytearray,0,mybytearray.length);
                    socketOut.flush();
                    */
        FileOutputStream fos = new FileOutputStream(f);
       ObjectOutputStream oos = new ObjectOutputStream(fos);
       oos.writeObject(f);
       oos.flush();
       oos.close();
       fos.flush();
       fos.close();

        socketOut.flush();
        
        client.close();
        }catch(UnknownHostException e)
      { System.err.println(": unknown host."); }



    }

}


Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Send a file over a socket

Posted 07 March 2011 - 07:43 PM

In the server side, if you read from the client, you need an inputStream not an outputStream
and read from it
Was This Post Helpful? 1
  • +
  • -

#5 millar_ninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-March 11

Re: Send a file over a socket

Posted 08 March 2011 - 08:40 AM

I have reworte this so its a little more percies and just deals with sending the file with output and input strams:
here is the client code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.net.*;
/**
 *
 * @author Kenshin
 */
public class Client {

 public static void main(String[] args) {

            BufferedInputStream bis;
            BufferedOutputStream bos;
            int num;
            byte[] byteArray;
            File f = new File("file.xml");

        Socket s = null ;
        try{
              s = new Socket("localhost",8189) ;
              InputStream inStream = s.getInputStream() ;
              OutputStream outStream = s.getOutputStream() ;
              System.out.println("Connected to : " + s);

              BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
              PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);

              for (String x : args) {
              
              out.println(f);
              long len = f.length();
              System.out.println(inm.readLine());
              System.out.println("Sent File length = " + len);

            //SENDFILE
            bis = new BufferedInputStream(new FileInputStream(f));
            bos = new BufferedOutputStream(s.getOutputStream( ));
            byteArray = new byte[8192];
            while ((num = bis.read(byteArray)) != -1){
                bos.write(byteArray,0,num);
            }

             bos.close();
             bis.close();

            System.out.println(inm.readLine());
              }
        }catch (Exception e) {
             System.out.println(e) ;
        }
    }

}





and here is the server code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


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

/**
 *
 * @author Kenshin
 */
public class Serv {

  public static void main(String[] args) {
            BufferedInputStream bis;
            BufferedOutputStream bos;
            int num;

         try {
            int i = 1;
            ServerSocket s = new ServerSocket(8189);
            Socket incoming = s.accept();
            System.out.println("Spawning " + i);

            try {
                  try{

                InputStream inStream = incoming.getInputStream();
                OutputStream outStream = incoming.getOutputStream();

                BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
                PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);

                
                
                out.println("File received ");

                //RECIEVE and WRITE FILE
                byte[] receivedData = new byte[8192];
                bis = new BufferedInputStream(incoming.getInputStream());
                bos = new BufferedOutputStream(new FileOutputStream("file.xml"));
                //length = bis.read(receivedData);
                while ((num = bis.read(receivedData)) != -1){
                bos.write(receivedData,0,num);
            }
            bos.close();
            bis.close();

            File receivedFile = new File("file.xml");
            long receivedLen = receivedFile.length();
            out.println("ACK: Length of received file = " + receivedLen);
            System.out.println("Length of received file = " + receivedLen);

                  } finally {
                incoming.close();
               }
            } catch (IOException e){
        e.printStackTrace();
        }
            } catch (IOException e1){
        e1.printStackTrace();
        }

}
}



the problem is here the file.xml never get writen it exsit in the folder, but there is noting in it. what did I do wrong here?

This post has been edited by millar_ninja: 08 March 2011 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Send a file over a socket

Posted 08 March 2011 - 07:19 PM

You do not need an input stream, from the socket, on the client side

And what are you doing with the lines arguments ? What is this liine for ?
for (String x : args) {
Was This Post Helpful? 1
  • +
  • -

#7 millar_ninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-March 11

Re: Send a file over a socket

Posted 09 March 2011 - 07:44 AM

I was trying to get the file lenght and use that for the for loop, but after looking at it it did not work. But now I got it to work I used a BufferedReader to read the file into the client side and then sent that to the printwirter and then sent that to the sever and that worked grat.
Thanks for all the help every one
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1