Results 1 to 1 of 1
- 03-10-2011, 07:15 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
Server not reciveing file from client
We the connection is not the problem that works fine, its when the server trys to recive the file or when the client trys to send it I think its on the server side thought bucause the connection is closed after the client says it is sent. I dont knw whats wrong here and dont know where to start to look for the solution thanks for any help.
here is the client code:
Java 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 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 code:
Java Code: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(); } }
Similar Threads
-
Sending file from client to server
By kaijeong in forum New To JavaReplies: 50Last Post: 07-29-2010, 07:01 PM -
How to upload a file from client to server
By john_thomas03 in forum New To JavaReplies: 4Last Post: 07-26-2010, 02:00 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
how to send mp3 file from server to client
By Jigga008 in forum NetworkingReplies: 0Last Post: 12-01-2009, 12:02 PM -
how to send a file from server to client and client saves the file?
By KoolCancer in forum New To JavaReplies: 3Last Post: 07-29-2009, 04:52 AM


LinkBack URL
About LinkBacks

Bookmarks