Jump to content

File not being sent to server correctly

- - - - -

  • Please log in to reply
2 replies to this topic

#1
OMGNinja

OMGNinja

    Newbie

  • Members
  • Pip
  • 3 posts
I have a client and server that are communicating quite nicely and the connection is good, but I does not send the file using the methods that I wrote. I really dont know what I did wrong here at all. Well here is the servercode
here is the 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", 4444);

        System.out.println("Client is connect");

        inm = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        outm = new PrintWriter(clientSocket.getOutputStream(), true);

        System.out.println("Socket Closed = " +clientSocket.isClosed());

        outm.println("msg 1: Client messageing open");

        System.out.println("from server__ " +inm.readLine());

        outm.println("msg 2: Sending file");

        System.out.println("from server__ " +inm.readLine());

        sendFile();

        System.out.println("Socket closed = " +clientSocket.isClosed());

        System.out.println("from server__ " +inm.readLine());

        

    }catch(IOException e)

    {

        e.printStackTrace();

    }

}

//make public to revers fix

public void sendFile(){

    try{

        BufferedReader bo = new BufferedReader(new FileReader("request.xml"));

        //bis = new BufferedInputStream(new FileInputStream("request.xml"));

        bos = new BufferedOutputStream(clientSocket.getOutputStream( ));

        /*

        byteArray = new byte[8192];

        while ((in = bis.read(byteArray)) != -1){

        bos.write(byteArray,0,in);

        }

        // bis.close();

         bos.flush();

         *

         */

        //PrintWriter out = new PrintWriter(clientSocket.getOutputStream());

         String str;

         System.out.println("This is the xml file");

              while((str = bo.readLine()) != null)

              {

                 

                  System.out.println(str);

                  outm.write(str);

              }


        }catch(IOException e)

        {e.printStackTrace();}

}

public static void main(String[] args){

Client client = new Client();


}

}




and here is the 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(4444);

        System.out.println("Server setup and listening...");

        socket = serverSocket.accept();

        System.out.println("Client connect");

        System.out.println("Socket is closed = " +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: Ready....");

        receiveFile();

        System.out.println("Socket closed = " +socket.isClosed());

        outm.println("ack 3: take the file");

        }catch (IOException e)

        {

            e.printStackTrace();

        }

    }


//make public to reverse fix

public void receiveFile(){

    try{

        /*

        byte[] receivedData = new byte[8192];

        bis = new BufferedInputStream(socket.getInputStream());

        bos = new BufferedOutputStream(new FileOutputStream("request.xml"));

        while ((in = bis.read(receivedData)) != -1){

        bos.write(receivedData,0,in);

        }

        bos.flush();

    bos.close();

         *

         * */

         char gus;

         BufferedInputStream buf = new BufferedInputStream(socket.getInputStream());

         BufferedWriter bwo = new BufferedWriter(new FileWriter("result.xml"));

         System.out.println("buf.avaliable = " +buf.available());

         System.out.println("Reading file");

                while(buf.available()>0)

                {

                    gus = (char) buf.read();

                    bwo.write(gus);


                }

                bwo.flush();

                bwo.close();

                buf.close();

    }catch (IOException e)

    {

        e.printStackTrace();

    }

    }

public static void main(String[] args)

{

        Server server = new Server();

}

}




#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
error / stacktrace?

#3
OMGNinja

OMGNinja

    Newbie

  • Members
  • Pip
  • 3 posts
I got tihs working I just had to add another printwirter to the clinet side I think I was sendint too much info in one inputstream
Thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users