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
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: ok...."); 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 private 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(); }catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Server server = new Server(); } }
and here is the client code:
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 private void sendFile(){ try{ 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(); }catch(IOException e) {e.printStackTrace();} } public static void main(String[] args){ Client client = new Client(); } }