Problem reading string from client

I am having trouble sending the filename before i send the file and storing it to a string on the server. I always stores some gibberish string. Can someone please help

Server code

import java.io.IOException;

import java.net.ServerSocket;

publicclass Server{

/**

* @param args

* @throws IOException

*/

publicstaticvoid main(String[] args)throws IOException{

// TODO Auto-generated method stub

ServerSocket serverSocket =null;

boolean listening =true;

try{

serverSocket =new ServerSocket(12345);

}catch (IOException e){

System.err.println("Could not listen on port: 12345.");

System.exit(-1);

}

while (listening)

new ServerThreads(serverSocket.accept()).start();

serverSocket.close();

}

}

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.Socket;

publicclass ServerThreadsextends Thread{

publicstaticfinalint BUFFER_SIZE = 1024 * 50;

privatebyte[] buffer;

private Socket socket =null;

public ServerThreads(Socket socket){

// TODO Auto-generated constructor stub

this.socket = socket;

buffer =newbyte[BUFFER_SIZE];

}

publicvoid run(){

try{

String fileName;

System.out.println("accepted Socket");

BufferedReader rd =new BufferedReader(new InputStreamReader(socket.getInputStream()));

BufferedInputStream in =

new BufferedInputStream(socket.getInputStream());

fileName = rd.readLine();

System.out.println(fileName);

BufferedOutputStream out =

new BufferedOutputStream(new FileOutputStream("Test.Z"));

int len = 0;

while ((len = in.read(buffer)) > 0){

out.write(buffer, 0, len);

System.out.print("#");

}

fileName = rd.readLine();

System.out.println(fileName);

in.close();

out.flush();

out.close();

socket.close();

System.out.println("\nDone!");

}

catch(IOException e){

}

}

}

Client Code:

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.PrintWriter;

import java.net.Socket;

publicclass Network{

publicstaticfinalint BUFFER_SIZE = 1024 * 50;

privatebyte[] buffer;

public Network(){

// TODO Auto-generated constructor stub

buffer =newbyte[BUFFER_SIZE];

}

publicvoid startClient()throws Exception{

Socket socket =new Socket("localhost", 12345);

PrintWriter pout =new PrintWriter(socket.getOutputStream(),true);

String fname ="filename";

pout.println(fname);

BufferedInputStream in =

new BufferedInputStream(

new FileInputStream("file.Z"));

BufferedOutputStream out =

new BufferedOutputStream(socket.getOutputStream());

int len = 0;

while ((len = in.read(buffer)) > 0){

out.write(buffer, 0, len);

System.out.print("#");

}

in.close();

out.flush();

out.close();

socket.close();

System.out.println("\nDone!");

}

/**

* @param args

* @throws Exception

*/

publicstaticvoid main(String[] args)throws Exception{

// TODO Auto-generated method stub

Network nw =new Network();

nw.startClient();

}

}

[7673 byte] By [Mjacobsa] at [2007-11-27 8:10:27]
# 1

What gibberish are you getting? I tested this code and it seems to be working fine for how you have it written. It isn't sending the name of the file in the code that you have written. It is sending filename instead of the name of the file, and after it sends filename, it is sending the what is in the file.Z file to the server.

llemonsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 2
when i replace BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Test.Z"));with BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));it creates a file name with a bunch of symbols and shapes(no letters)
Mjacobsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 3
潗.^?c& is hat gets written as the filename.....i dont know if it is important or not...but i am running this code on aix
Mjacobsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 4
When you leave the print(filename) in there, what gets printed out? It should be the same thing as the filename. Actually, I tried that and it works fine here as well. Can you post that little snippet of code that you changed in the ServerThreads.java file in the run() method?
llemonsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 5

BufferedInputStream in =

new BufferedInputStream(socket.getInputStream());

fileName = rd.readLine();

System.out.println(fileName);

BufferedOutputStream out =

new BufferedOutputStream(new FileOutputStream(fileName));

Mjacobsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 6
And what is printed where you have the System.out.println(fileName);?
llemonsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 7

Well i have it working now....for some odd reason when i upload the file to the AIX box using filezilla it doesnt and the PrintWriter statement to the code. all of that is left out. so i used vi to edit the file to add it back in there and it worked.

Sorry to waste your time

Im still going to award you the duke points

Mjacobsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...
# 8
No problem. It was my pleasure to try to help you. It might be just that it compiled on one platform and ran on the AIX caused the problem. I've seen it with some Linux JVMs before, but they normally just did not run at all, though.Good luck!
llemonsa at 2007-7-12 19:54:02 > top of Java-index,Core,Core APIs...